ping_sent doesn't have to be global

This commit is contained in:
jesopo 2020-04-26 15:14:13 +01:00
parent ba57d06a56
commit 60dfda48e0

View file

@ -43,7 +43,6 @@ class Server(IServer):
self.sasl_state = SASLResult.NONE self.sasl_state = SASLResult.NONE
self.last_read = -1.0 self.last_read = -1.0
self._ping_sent = False
self._sent_count: int = 0 self._sent_count: int = 0
self._write_queue: PriorityQueue[SentLine] = PriorityQueue() self._write_queue: PriorityQueue[SentLine] = PriorityQueue()
@ -178,21 +177,21 @@ class Server(IServer):
if self._read_queue: if self._read_queue:
both = self._read_queue.popleft() both = self._read_queue.popleft()
else: else:
ping_sent = False
while True: while True:
try: try:
async with timeout(PING_TIMEOUT): async with timeout(PING_TIMEOUT):
data = await self._reader.read(1024) data = await self._reader.read(1024)
except asyncio.exceptions.TimeoutError: except asyncio.exceptions.TimeoutError:
if self._ping_sent: if ping_sent:
data = b"" # empty data means the socket disconnected data = b"" # empty data means the socket disconnected
else: else:
self._ping_sent = True ping_sent = True
await self.send(build("PING", ["hello"])) await self.send(build("PING", ["hello"]))
continue continue
self.last_read = monotonic() self.last_read = monotonic()
self._ping_sent = False ping_sent = False
try: try:
lines = self.recv(data) lines = self.recv(data)