forked from goodboy/tractor
Store array `maxlen` in state singleton
The `collections.deque` takes care of array length truncation of values for us implicitly but in the future we'll likely want this value exposed to alternate array implementations. This patch is to provide for that as well as make `mypy` happy since the `dequeu.maxlen` can also be `None`.tokio_backup
parent
c3665801a5
commit
a7e7c9d1c0
|
@ -74,6 +74,7 @@ class BroadcastState:
|
||||||
|
|
||||||
'''
|
'''
|
||||||
queue: deque
|
queue: deque
|
||||||
|
maxlen: int
|
||||||
|
|
||||||
# map of underlying instance id keys to receiver instances which
|
# map of underlying instance id keys to receiver instances which
|
||||||
# must be provided as a singleton per broadcaster set.
|
# must be provided as a singleton per broadcaster set.
|
||||||
|
@ -81,7 +82,7 @@ class BroadcastState:
|
||||||
|
|
||||||
# broadcast event to wake up all sleeping consumer tasks
|
# broadcast event to wake up all sleeping consumer tasks
|
||||||
# on a newly produced value from the sender.
|
# on a newly produced value from the sender.
|
||||||
recv_ready: Optional[tuple[str, trio.Event]] = None
|
recv_ready: Optional[tuple[int, trio.Event]] = None
|
||||||
|
|
||||||
|
|
||||||
class BroadcastReceiver(ReceiveChannel):
|
class BroadcastReceiver(ReceiveChannel):
|
||||||
|
@ -150,7 +151,7 @@ class BroadcastReceiver(ReceiveChannel):
|
||||||
# decrement to the last value and expect
|
# decrement to the last value and expect
|
||||||
# consumer to either handle the ``Lagged`` and come back
|
# consumer to either handle the ``Lagged`` and come back
|
||||||
# or bail out on its own (thus un-subscribing)
|
# or bail out on its own (thus un-subscribing)
|
||||||
state.subs[key] = state.queue.maxlen - 1
|
state.subs[key] = state.maxlen - 1
|
||||||
|
|
||||||
# this task was overrun by the producer side
|
# this task was overrun by the producer side
|
||||||
task: Task = current_task()
|
task: Task = current_task()
|
||||||
|
@ -174,7 +175,12 @@ class BroadcastReceiver(ReceiveChannel):
|
||||||
# right?
|
# right?
|
||||||
try:
|
try:
|
||||||
value = await self._recv()
|
value = await self._recv()
|
||||||
|
|
||||||
# items with lower indices are "newer"
|
# items with lower indices are "newer"
|
||||||
|
# NOTE: ``collections.deque`` implicitly takes care of
|
||||||
|
# trucating values outside our ``state.maxlen``. In the
|
||||||
|
# alt-backend-array-case we'll need to make sure this is
|
||||||
|
# implemented in similar ringer-buffer-ish style.
|
||||||
state.queue.appendleft(value)
|
state.queue.appendleft(value)
|
||||||
|
|
||||||
# broadcast new value to all subscribers by increasing
|
# broadcast new value to all subscribers by increasing
|
||||||
|
@ -295,6 +301,7 @@ def broadcast_receiver(
|
||||||
recv_chan,
|
recv_chan,
|
||||||
state=BroadcastState(
|
state=BroadcastState(
|
||||||
queue=deque(maxlen=max_buffer_size),
|
queue=deque(maxlen=max_buffer_size),
|
||||||
|
maxlen=max_buffer_size,
|
||||||
subs={},
|
subs={},
|
||||||
),
|
),
|
||||||
**kwargs,
|
**kwargs,
|
||||||
|
|
Loading…
Reference in New Issue