|
25 | 25 |
|
26 | 26 | AUTH_TIMEOUT_SECONDS: Final = 2.0 |
27 | 27 | CONNECT_TIMEOUT_SECONDS: Final = 5.0 |
| 28 | +DBN_QUEUE_CAPACITY: Final = 2**20 |
28 | 29 | DEFAULT_REMOTE_PORT: Final = 13000 |
29 | 30 |
|
30 | 31 |
|
31 | | -class DBNQueue(queue.Queue): # type: ignore [type-arg] |
| 32 | +class DBNQueue(queue.SimpleQueue): # type: ignore [type-arg] |
32 | 33 | """ |
33 | 34 | Queue for DBNRecords that can only be pushed to when enabled. |
34 | | -
|
35 | | - Parameters |
36 | | - ---------- |
37 | | - maxsize : int |
38 | | - The `maxsize` for the Queue. |
39 | | -
|
40 | 35 | """ |
41 | 36 |
|
42 | | - def __init__(self, maxsize: int) -> None: |
43 | | - super().__init__(maxsize) |
| 37 | + def __init__(self) -> None: |
| 38 | + super().__init__() |
44 | 39 | self._enabled = threading.Event() |
45 | 40 |
|
46 | | - @property |
47 | | - def enabled(self) -> bool: |
| 41 | + def is_enabled(self) -> bool: |
48 | 42 | """ |
49 | | - True if the Queue will allow pushing. |
| 43 | + Return True if the Queue will allow pushing; False otherwise. |
50 | 44 |
|
51 | 45 | A queue should only be enabled when it has a consumer. |
52 | 46 |
|
53 | 47 | """ |
54 | 48 | return self._enabled.is_set() |
55 | 49 |
|
56 | | - def half_full(self) -> bool: |
| 50 | + def is_full(self) -> bool: |
| 51 | + """ |
| 52 | + Return True when the queue has reached capacity; False otherwise. |
| 53 | + """ |
| 54 | + return self.qsize() > DBN_QUEUE_CAPACITY |
| 55 | + |
| 56 | + def enable(self) -> None: |
| 57 | + """ |
| 58 | + Enable the DBN queue for pushing. |
| 59 | + """ |
| 60 | + self._enabled.set() |
| 61 | + |
| 62 | + def disable(self) -> None: |
| 63 | + """ |
| 64 | + Disable the DBN queue for pushing. |
| 65 | + """ |
| 66 | + self._enabled.clear() |
| 67 | + |
| 68 | + def put(self, item: DBNRecord, block: bool = True, timeout: float | None = None) -> None: |
| 69 | + """ |
| 70 | + Put an item on the queue if the queue is enabled. |
| 71 | +
|
| 72 | + Parameters |
| 73 | + ---------- |
| 74 | + item: DBNRecord |
| 75 | + The DBNRecord to put into the queue |
| 76 | + block: bool, default True |
| 77 | + Block if necessary until a free slot is available or the `timeout` is reached |
| 78 | + timeout: float | None, default None |
| 79 | + The maximum amount of time to block, when `block` is True, for the queue to become enabled. |
| 80 | +
|
| 81 | + Raises |
| 82 | + ------ |
| 83 | + BentoError |
| 84 | + If the queue is not enabled. |
| 85 | + If the queue is not enabled within `timeout` seconds. |
| 86 | +
|
| 87 | + See Also |
| 88 | + -------- |
| 89 | + queue.SimpleQueue.put |
| 90 | +
|
57 | 91 | """ |
58 | | - Return True when the queue has reached half capacity. |
| 92 | + if self._enabled.wait(timeout): |
| 93 | + return super().put(item, block, timeout) |
| 94 | + if timeout is not None: |
| 95 | + raise BentoError(f"queue is not enabled after {timeout} second(s)") |
| 96 | + raise BentoError("queue is not enabled") |
| 97 | + |
| 98 | + def put_nowait(self, item: DBNRecord) -> None: |
59 | 99 | """ |
60 | | - with self.mutex: |
61 | | - return self._qsize() > self.maxsize // 2 |
| 100 | + Put an item on the queue, if the queue is enabled, without blocking. |
62 | 101 |
|
| 102 | + Parameters |
| 103 | + ---------- |
| 104 | + item: DBNRecord |
| 105 | + The DBNRecord to put into the queue |
| 106 | +
|
| 107 | + Raises |
| 108 | + ------ |
| 109 | + BentoError |
| 110 | + If the queue is not enabled. |
| 111 | +
|
| 112 | + See Also |
| 113 | + -------- |
| 114 | + queue.SimpleQueue.put_nowait |
| 115 | +
|
| 116 | + """ |
| 117 | + if self.is_enabled(): |
| 118 | + return super().put_nowait(item) |
| 119 | + raise BentoError("queue is not enabled") |
63 | 120 |
|
64 | 121 | @dataclasses.dataclass |
65 | 122 | class SessionMetadata: |
@@ -173,22 +230,16 @@ def received_record(self, record: DBNRecord) -> None: |
173 | 230 | if exc_callback is not None: |
174 | 231 | exc_callback(exc) |
175 | 232 |
|
176 | | - if self._dbn_queue.enabled: |
177 | | - try: |
178 | | - self._dbn_queue.put_nowait(record) |
179 | | - except queue.Full: |
180 | | - logger.error( |
181 | | - "record queue is full; dropped %s record ts_event=%s", |
182 | | - type(record).__name__, |
183 | | - record.ts_event, |
| 233 | + if self._dbn_queue.is_enabled(): |
| 234 | + self._dbn_queue.put(record) |
| 235 | + |
| 236 | + # DBNQueue has no max size; so check if it's above capacity, and if so, pause reading |
| 237 | + if self._dbn_queue.is_full(): |
| 238 | + logger.warning( |
| 239 | + "record queue is full; %d record(s) to be processed", |
| 240 | + self._dbn_queue.qsize(), |
184 | 241 | ) |
185 | | - else: |
186 | | - if self._dbn_queue.half_full(): |
187 | | - logger.warning( |
188 | | - "record queue is full; %d record(s) to be processed", |
189 | | - self._dbn_queue._qsize(), |
190 | | - ) |
191 | | - self.transport.pause_reading() |
| 242 | + self.transport.pause_reading() |
192 | 243 |
|
193 | 244 | return super().received_record(record) |
194 | 245 |
|
|
0 commit comments