Fix latency being able to be negative
This commit is contained in:
parent
6d57ddf11b
commit
bd467085bf
@ -62,13 +62,14 @@ class KeepAliveHandler(threading.Thread):
|
|||||||
self.shard_id = shard_id
|
self.shard_id = shard_id
|
||||||
self.msg = 'Keeping websocket alive with sequence %s.'
|
self.msg = 'Keeping websocket alive with sequence %s.'
|
||||||
self._stop_ev = threading.Event()
|
self._stop_ev = threading.Event()
|
||||||
self._last_ack = time.monotonic()
|
self._last_ack = time.perf_counter()
|
||||||
self._last_send = time.monotonic()
|
self._last_send = time.perf_counter()
|
||||||
|
self.latency = float('inf')
|
||||||
self.heartbeat_timeout = ws._max_heartbeat_timeout
|
self.heartbeat_timeout = ws._max_heartbeat_timeout
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
while not self._stop_ev.wait(self.interval):
|
while not self._stop_ev.wait(self.interval):
|
||||||
if self._last_ack + self.heartbeat_timeout < time.monotonic():
|
if self._last_ack + self.heartbeat_timeout < time.perf_counter():
|
||||||
log.warning("Shard ID %s has stopped responding to the gateway. Closing and restarting." % self.shard_id)
|
log.warning("Shard ID %s has stopped responding to the gateway. Closing and restarting." % self.shard_id)
|
||||||
coro = self.ws.close(4000)
|
coro = self.ws.close(4000)
|
||||||
f = asyncio.run_coroutine_threadsafe(coro, loop=self.ws.loop)
|
f = asyncio.run_coroutine_threadsafe(coro, loop=self.ws.loop)
|
||||||
@ -91,7 +92,7 @@ class KeepAliveHandler(threading.Thread):
|
|||||||
except Exception:
|
except Exception:
|
||||||
self.stop()
|
self.stop()
|
||||||
else:
|
else:
|
||||||
self._last_send = time.monotonic()
|
self._last_send = time.perf_counter()
|
||||||
|
|
||||||
def get_payload(self):
|
def get_payload(self):
|
||||||
return {
|
return {
|
||||||
@ -103,7 +104,9 @@ class KeepAliveHandler(threading.Thread):
|
|||||||
self._stop_ev.set()
|
self._stop_ev.set()
|
||||||
|
|
||||||
def ack(self):
|
def ack(self):
|
||||||
self._last_ack = time.monotonic()
|
ack_time = time.perf_counter()
|
||||||
|
self._last_ack = ack_time
|
||||||
|
self.latency = ack_time - self._last_send
|
||||||
|
|
||||||
class VoiceKeepAliveHandler(KeepAliveHandler):
|
class VoiceKeepAliveHandler(KeepAliveHandler):
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
@ -426,7 +429,7 @@ class DiscordWebSocket(websockets.client.WebSocketClientProtocol):
|
|||||||
def latency(self):
|
def latency(self):
|
||||||
""":obj:`float`: Measures latency between a HEARTBEAT and a HEARTBEAT_ACK in seconds."""
|
""":obj:`float`: Measures latency between a HEARTBEAT and a HEARTBEAT_ACK in seconds."""
|
||||||
heartbeat = self._keep_alive
|
heartbeat = self._keep_alive
|
||||||
return float('inf') if heartbeat is None else heartbeat._last_ack - heartbeat._last_send
|
return float('inf') if heartbeat is None else heartbeat.latency
|
||||||
|
|
||||||
def _can_handle_close(self, code):
|
def _can_handle_close(self, code):
|
||||||
return code not in (1000, 4004, 4010, 4011)
|
return code not in (1000, 4004, 4010, 4011)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user