Tweak timeouts, reduce stop time

This commit is contained in:
Cyber MacGeddon 2025-04-29 21:43:37 +01:00
parent 027b52cd7c
commit 215665ba16
2 changed files with 22 additions and 7 deletions

View file

@ -21,6 +21,7 @@ class Publisher:
async def stop(self):
self.running = False
await self.task
async def join(self):
await self.stop()
@ -42,7 +43,7 @@ class Publisher:
try:
id, item = await asyncio.wait_for(
self.q.get(),
timeout=0.5
timeout=0.25
)
except asyncio.TimeoutError:
continue
@ -57,8 +58,11 @@ class Publisher:
except Exception as e:
print("Exception:", e, flush=True)
if not self.running:
return
# If handler drops out, sleep a retry
time.sleep(2)
await asyncio.sleep(1)
async def send(self, id, item):
await self.q.put((id, item))

View file

@ -28,6 +28,7 @@ class Subscriber:
async def stop(self):
self.running = False
await self.task
async def join(self):
await self.stop()
@ -35,6 +36,8 @@ class Subscriber:
async def run(self):
consumer = None
while self.running:
if self.metrics:
@ -59,7 +62,7 @@ class Subscriber:
try:
msg = await asyncio.to_thread(
consumer.receive,
timeout_millis=2000
timeout_millis=250
)
except _pulsar.Timeout:
continue
@ -91,7 +94,7 @@ class Subscriber:
# FIXME: Timeout means data goes missing
await asyncio.wait_for(
self.q[id].put(value),
timeout=2
timeout=1
)
except Exception as e:
@ -103,7 +106,7 @@ class Subscriber:
# FIXME: Timeout means data goes missing
await asyncio.wait_for(
q.put(value),
timeout=2
timeout=1
)
except Exception as e:
self.metrics.dropped()
@ -112,13 +115,21 @@ class Subscriber:
except Exception as e:
print("Subscriber exception:", e, flush=True)
consumer.close()
finally:
if consumer:
consumer.close()
consumer = None
if self.metrics:
self.metrics.state("stopped")
if not self.running:
return
# If handler drops out, sleep a retry
time.sleep(2)
await asyncio.sleep(1)
async def subscribe(self, id):