Skip to content

Commit ae96c3a

Browse files
committed
Remove periodic ping
Servers are already re-pinged on update.
1 parent d816450 commit ae96c3a

File tree

2 files changed

+1
-80
lines changed

2 files changed

+1
-80
lines changed

server_list/ping.py

Lines changed: 0 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
import asyncio
21
import time
3-
import random
42
import socket
53

64
from .app import app
@@ -37,61 +35,6 @@ def get_ping_reply(data):
3735
return b"\x4f\x45\x74\x03" + peer_id + b"\x00\x00\x03"
3836

3937

40-
class MinetestProtocol:
41-
def connection_made(self, transport):
42-
self.transport = transport
43-
44-
def send_original(self):
45-
self.transport.sendto(PING_PACKET)
46-
47-
self.start = time.time()
48-
49-
def datagram_received(self, data, addr):
50-
end = time.time()
51-
self.transport.sendto(get_ping_reply(data), addr)
52-
53-
self.future.set_result(end - self.start)
54-
self.transport.close()
55-
56-
def connection_lost(self, exc):
57-
if not self.future.done():
58-
self.future.set_result(None)
59-
60-
def error_received(self, exc):
61-
self.future.set_result(None)
62-
63-
64-
async def ping_server_async(address, sock=None):
65-
loop = asyncio.get_event_loop()
66-
transport, protocol = await loop.create_datagram_endpoint(
67-
MinetestProtocol,
68-
remote_addr=address,
69-
sock=sock)
70-
attempts = 0
71-
pings = []
72-
while len(pings) < 3 and attempts - len(pings) < 3:
73-
attempts += 1
74-
protocol.future = loop.create_future()
75-
try:
76-
# Sleep a bit to spread requests out
77-
await asyncio.sleep(random.random())
78-
protocol.send_original()
79-
ping = await asyncio.wait_for(asyncio.shield(protocol.future), 2)
80-
if ping is not None:
81-
pings.append(ping)
82-
except asyncio.TimeoutError:
83-
pass
84-
85-
if len(pings) != 0:
86-
return min(pings)
87-
88-
return None
89-
90-
91-
async def ping_servers_async(addresses):
92-
return await asyncio.gather(*[ping_server_async(a) for a in addresses])
93-
94-
9538
def ping_server_addresses(address, port):
9639
pings = []
9740
addr_info = get_addr_info(address, port)

server_list/tasks.py

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
import asyncio
21
import json
32
import os
43
from datetime import datetime
54

65
from .app import app, celery, db
76
from .models import Server, Stats
8-
from .ping import ping_servers_async, ping_server_addresses
7+
from .ping import ping_server_addresses
98
from .util import get_geo_continent, server_ranking
109

1110

@@ -75,27 +74,6 @@ def update_list():
7574
db.session.commit()
7675

7776

78-
@celery.task
79-
def update_ping():
80-
servers = Server.query.filter_by(online=True).all()
81-
82-
addresses = [(s.address, s.port) for s in servers]
83-
pings = []
84-
85-
async def do_ping():
86-
pings.extend(await ping_servers_async(addresses))
87-
asyncio.run(do_ping())
88-
89-
for i, server in enumerate(servers):
90-
if pings[i] is None:
91-
server.set_offline()
92-
else:
93-
server.ping = pings[i]
94-
95-
db.session.commit()
96-
97-
9877
@celery.on_after_configure.connect
9978
def setup_periodic_tasks(sender, **kwargs):
10079
sender.add_periodic_task(60, update_list.s(), name='Update server list')
101-
sender.add_periodic_task(5*60, update_ping.s(), name='Update server ping')

0 commit comments

Comments
 (0)