Skip to content

Commit

Permalink
Sort servers by their ping
Browse files Browse the repository at this point in the history
  • Loading branch information
Ketok4321 committed Aug 16, 2023
1 parent 386c82c commit 5dc3ae0
Showing 1 changed file with 31 additions and 2 deletions.
33 changes: 31 additions & 2 deletions src/speedtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,19 @@ def __init__(self, name, server, pingURL, dlURL, ulURL, **_):
self.uploadURL = urljoin(server, ulURL)

async def get_servers():
print("Fetching servers...")
async with aiohttp.ClientSession() as session:
async with session.get("https://librespeed.org/backend-servers/servers.php") as response:
result = await response.json()
return list(map(lambda x: Server(**x), result))
servers = await response.json()
servers = list(map(lambda x: Server(**x), servers))

await asyncio.gather(*[check_server(s) for s in servers])

servers = list(filter(lambda s: s.ping != -1, servers))

servers.sort(key=lambda s: s.ping)

return servers

class GarbageReader(io.IOBase):
def __init__(self, read_callback=None):
Expand Down Expand Up @@ -66,6 +75,26 @@ def read(self, size=None):

return garbage[old_pos:self.pos]

async def check_server(server):
async with aiohttp.ClientSession() as session:
try:
start = time.time()
task = asyncio.create_task(session.get(server.pingURL, headers=headers))

while not task.done():
if time.time() - start > 1:
task.cancel()
server.ping = -1
return
await asyncio.sleep(0)

task.result().close()
server.ping = time.time() - start
return
except aiohttp.ClientError:
server.ping = -1
return

async def ping(server): #TODO: jitter and other stuff
async with aiohttp.ClientSession() as session:
pings = []
Expand Down

0 comments on commit 5dc3ae0

Please sign in to comment.