pondsocket 0.0.7__tar.gz → 0.0.8__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {pondsocket-0.0.7 → pondsocket-0.0.8}/PKG-INFO +2 -2
- {pondsocket-0.0.7 → pondsocket-0.0.8}/pyproject.toml +1 -1
- {pondsocket-0.0.7 → pondsocket-0.0.8}/src/pondsocket/__init__.py +2 -0
- {pondsocket-0.0.7 → pondsocket-0.0.8}/src/pondsocket/channel.py +88 -40
- {pondsocket-0.0.7 → pondsocket-0.0.8}/src/pondsocket/contexts/join_context.py +14 -8
- {pondsocket-0.0.7 → pondsocket-0.0.8}/src/pondsocket/endpoint.py +24 -9
- {pondsocket-0.0.7 → pondsocket-0.0.8}/src/pondsocket/heartbeat.py +7 -6
- {pondsocket-0.0.7 → pondsocket-0.0.8}/src/pondsocket/in_memory_transport.py +23 -5
- {pondsocket-0.0.7 → pondsocket-0.0.8}/src/pondsocket/lobby.py +35 -13
- {pondsocket-0.0.7 → pondsocket-0.0.8}/src/pondsocket/presence.py +4 -0
- {pondsocket-0.0.7 → pondsocket-0.0.8}/src/pondsocket/pubsub.py +6 -0
- {pondsocket-0.0.7 → pondsocket-0.0.8}/src/pondsocket/pubsub_local.py +10 -6
- {pondsocket-0.0.7 → pondsocket-0.0.8}/src/pondsocket/sse_transport.py +48 -3
- {pondsocket-0.0.7 → pondsocket-0.0.8}/src/pondsocket/types.py +32 -0
- {pondsocket-0.0.7 → pondsocket-0.0.8}/tests/test_pondsocket_channel.py +200 -2
- {pondsocket-0.0.7 → pondsocket-0.0.8}/tests/test_pondsocket_channel_lifecycle_adversarial.py +79 -0
- {pondsocket-0.0.7 → pondsocket-0.0.8}/tests/test_pondsocket_distributed_protocol.py +53 -0
- {pondsocket-0.0.7 → pondsocket-0.0.8}/tests/test_pondsocket_integration.py +22 -0
- pondsocket-0.0.8/tests/test_pondsocket_options.py +32 -0
- {pondsocket-0.0.7 → pondsocket-0.0.8}/tests/test_pondsocket_pubsub.py +10 -4
- {pondsocket-0.0.7 → pondsocket-0.0.8}/tests/test_pondsocket_sse_transport.py +16 -0
- {pondsocket-0.0.7 → pondsocket-0.0.8}/.gitignore +0 -0
- {pondsocket-0.0.7 → pondsocket-0.0.8}/README.md +0 -0
- {pondsocket-0.0.7 → pondsocket-0.0.8}/src/pondsocket/contexts/__init__.py +0 -0
- {pondsocket-0.0.7 → pondsocket-0.0.8}/src/pondsocket/contexts/connection_context.py +0 -0
- {pondsocket-0.0.7 → pondsocket-0.0.8}/src/pondsocket/contexts/event_context.py +0 -0
- {pondsocket-0.0.7 → pondsocket-0.0.8}/src/pondsocket/contexts/leave_context.py +0 -0
- {pondsocket-0.0.7 → pondsocket-0.0.8}/src/pondsocket/contexts/outgoing_context.py +0 -0
- {pondsocket-0.0.7 → pondsocket-0.0.8}/src/pondsocket/distributed/__init__.py +0 -0
- {pondsocket-0.0.7 → pondsocket-0.0.8}/src/pondsocket/distributed/redis_pubsub.py +0 -0
- {pondsocket-0.0.7 → pondsocket-0.0.8}/src/pondsocket/distributed_wire.py +0 -0
- {pondsocket-0.0.7 → pondsocket-0.0.8}/src/pondsocket/errors.py +0 -0
- {pondsocket-0.0.7 → pondsocket-0.0.8}/src/pondsocket/hooks.py +0 -0
- {pondsocket-0.0.7 → pondsocket-0.0.8}/src/pondsocket/middleware.py +0 -0
- {pondsocket-0.0.7 → pondsocket-0.0.8}/src/pondsocket/parser.py +0 -0
- {pondsocket-0.0.7 → pondsocket-0.0.8}/src/pondsocket/py.typed +0 -0
- {pondsocket-0.0.7 → pondsocket-0.0.8}/src/pondsocket/server.py +0 -0
- {pondsocket-0.0.7 → pondsocket-0.0.8}/src/pondsocket/store.py +0 -0
- {pondsocket-0.0.7 → pondsocket-0.0.8}/src/pondsocket/transport.py +0 -0
- {pondsocket-0.0.7 → pondsocket-0.0.8}/src/pondsocket/typed.py +0 -0
- {pondsocket-0.0.7 → pondsocket-0.0.8}/src/pondsocket/wire.py +0 -0
- {pondsocket-0.0.7 → pondsocket-0.0.8}/tests/test_pondsocket_distributed_local.py +0 -0
- {pondsocket-0.0.7 → pondsocket-0.0.8}/tests/test_pondsocket_errors.py +0 -0
- {pondsocket-0.0.7 → pondsocket-0.0.8}/tests/test_pondsocket_hooks_integration.py +0 -0
- {pondsocket-0.0.7 → pondsocket-0.0.8}/tests/test_pondsocket_hooks_unit.py +0 -0
- {pondsocket-0.0.7 → pondsocket-0.0.8}/tests/test_pondsocket_in_memory_transport.py +0 -0
- {pondsocket-0.0.7 → pondsocket-0.0.8}/tests/test_pondsocket_join_autodecline_adversarial.py +0 -0
- {pondsocket-0.0.7 → pondsocket-0.0.8}/tests/test_pondsocket_leave_event_adversarial.py +0 -0
- {pondsocket-0.0.7 → pondsocket-0.0.8}/tests/test_pondsocket_middleware.py +0 -0
- {pondsocket-0.0.7 → pondsocket-0.0.8}/tests/test_pondsocket_parser.py +0 -0
- {pondsocket-0.0.7 → pondsocket-0.0.8}/tests/test_pondsocket_presence.py +0 -0
- {pondsocket-0.0.7 → pondsocket-0.0.8}/tests/test_pondsocket_redis_pubsub.py +0 -0
- {pondsocket-0.0.7 → pondsocket-0.0.8}/tests/test_pondsocket_smoke.py +0 -0
- {pondsocket-0.0.7 → pondsocket-0.0.8}/tests/test_pondsocket_sse_invalid_message_adversarial.py +0 -0
- {pondsocket-0.0.7 → pondsocket-0.0.8}/tests/test_pondsocket_store.py +0 -0
|
@@ -66,6 +66,7 @@ from .parser import parse
|
|
|
66
66
|
from .presence import PresenceClient
|
|
67
67
|
from .pubsub import (
|
|
68
68
|
PubSub,
|
|
69
|
+
PubSubBackpressureError,
|
|
69
70
|
PubSubClosedError,
|
|
70
71
|
format_heartbeat_topic,
|
|
71
72
|
format_message_topic,
|
|
@@ -174,6 +175,7 @@ __all__ = [
|
|
|
174
175
|
"PresenceClient",
|
|
175
176
|
"PresenceEventTypes",
|
|
176
177
|
"PubSub",
|
|
178
|
+
"PubSubBackpressureError",
|
|
177
179
|
"PubSubClosedError",
|
|
178
180
|
"PushableTransport",
|
|
179
181
|
"RateLimiter",
|
|
@@ -144,10 +144,14 @@ class Channel:
|
|
|
144
144
|
self._close_task = None
|
|
145
145
|
self._active = True
|
|
146
146
|
self._dispatch_task = asyncio.create_task(self._dispatch_loop())
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
147
|
+
try:
|
|
148
|
+
await self._subscribe_to_pubsub()
|
|
149
|
+
await self._start_liveness()
|
|
150
|
+
await self._request_channel_state()
|
|
151
|
+
self._fire_metric_channel_created()
|
|
152
|
+
except BaseException:
|
|
153
|
+
await self.close()
|
|
154
|
+
raise
|
|
151
155
|
|
|
152
156
|
async def close(self) -> None:
|
|
153
157
|
async with self._lifecycle_lock:
|
|
@@ -175,6 +179,12 @@ class Channel:
|
|
|
175
179
|
dispatch_task.cancel()
|
|
176
180
|
with contextlib.suppress(asyncio.CancelledError):
|
|
177
181
|
await dispatch_task
|
|
182
|
+
while not self._dispatch_queue.empty():
|
|
183
|
+
with contextlib.suppress(asyncio.QueueEmpty):
|
|
184
|
+
self._dispatch_queue.get_nowait()
|
|
185
|
+
await self._connections.clear()
|
|
186
|
+
await self._assigns.clear()
|
|
187
|
+
await self._presence.clear()
|
|
178
188
|
await self._unsubscribe_from_pubsub()
|
|
179
189
|
self._stop_liveness()
|
|
180
190
|
self._node_users.clear()
|
|
@@ -225,12 +235,17 @@ class Channel:
|
|
|
225
235
|
user_id = transport.get_id()
|
|
226
236
|
assigns = dict(transport.clone_assigns())
|
|
227
237
|
await self._connections.create(user_id, transport)
|
|
228
|
-
|
|
238
|
+
try:
|
|
239
|
+
await self._assigns.upsert(user_id, assigns)
|
|
240
|
+
await self._publish_distributed(
|
|
241
|
+
DistributedMessageType.USER_JOINED.value,
|
|
242
|
+
{"userId": user_id, "presence": {}, "assigns": assigns},
|
|
243
|
+
)
|
|
244
|
+
except BaseException:
|
|
245
|
+
await self._assigns.discard(user_id)
|
|
246
|
+
await self._connections.discard(user_id)
|
|
247
|
+
raise
|
|
229
248
|
self._fire_metric_channel_joined(user_id)
|
|
230
|
-
await self._publish_distributed(
|
|
231
|
-
DistributedMessageType.USER_JOINED.value,
|
|
232
|
-
{"userId": user_id, "presence": {}, "assigns": assigns},
|
|
233
|
-
)
|
|
234
249
|
|
|
235
250
|
async def remove_user(
|
|
236
251
|
self,
|
|
@@ -248,20 +263,36 @@ class Channel:
|
|
|
248
263
|
presence = await self._presence.get(user_id)
|
|
249
264
|
user = User(id=user_id, assigns=dict(assigns), presence=presence)
|
|
250
265
|
|
|
266
|
+
first_error: BaseException | None = None
|
|
267
|
+
|
|
251
268
|
await self._assigns.discard(user_id)
|
|
252
|
-
|
|
269
|
+
try:
|
|
270
|
+
await self._presence.untrack(user_id)
|
|
271
|
+
except BaseException as exc:
|
|
272
|
+
first_error = exc
|
|
273
|
+
await self._presence.discard(user_id)
|
|
253
274
|
self._fire_metric_channel_left(user_id)
|
|
254
275
|
|
|
255
276
|
if not skip_distributed:
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
277
|
+
try:
|
|
278
|
+
await self._publish_distributed(
|
|
279
|
+
DistributedMessageType.USER_LEFT.value,
|
|
280
|
+
{"userId": user_id},
|
|
281
|
+
)
|
|
282
|
+
except BaseException as exc:
|
|
283
|
+
if first_error is None:
|
|
284
|
+
first_error = exc
|
|
260
285
|
|
|
261
286
|
await self._fire_leave_handler(user, reason)
|
|
262
287
|
|
|
263
288
|
if await self._connections.len() == 0:
|
|
264
|
-
|
|
289
|
+
try:
|
|
290
|
+
await self.close()
|
|
291
|
+
except BaseException as exc:
|
|
292
|
+
if first_error is None:
|
|
293
|
+
first_error = exc
|
|
294
|
+
if first_error is not None:
|
|
295
|
+
raise first_error
|
|
265
296
|
return True
|
|
266
297
|
|
|
267
298
|
async def _fire_leave_handler(self, user: User, reason: str) -> None:
|
|
@@ -391,12 +422,15 @@ class Channel:
|
|
|
391
422
|
await self._send_direct(user_ids, ev)
|
|
392
423
|
|
|
393
424
|
async def send_acknowledge(self, request_id: str, user_id: str) -> None:
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
425
|
+
ev = Event(
|
|
426
|
+
action=ServerActions.SYSTEM.value,
|
|
427
|
+
channel_name=self.name,
|
|
397
428
|
request_id=request_id,
|
|
398
|
-
|
|
429
|
+
event=SystemEvents.ACKNOWLEDGE.value,
|
|
430
|
+
payload={},
|
|
431
|
+
node_id=self.node_id,
|
|
399
432
|
)
|
|
433
|
+
await self._send_required(user_id, ev)
|
|
400
434
|
|
|
401
435
|
async def send_unauthorized(
|
|
402
436
|
self,
|
|
@@ -422,10 +456,30 @@ class Channel:
|
|
|
422
456
|
if transport is None or not transport.is_active():
|
|
423
457
|
continue
|
|
424
458
|
try:
|
|
425
|
-
await
|
|
459
|
+
await asyncio.wait_for(
|
|
460
|
+
transport.send_event(event),
|
|
461
|
+
timeout=self._options.send_timeout,
|
|
462
|
+
)
|
|
463
|
+
except TimeoutError:
|
|
464
|
+
with contextlib.suppress(Exception):
|
|
465
|
+
await transport.close()
|
|
426
466
|
except Exception:
|
|
427
467
|
continue
|
|
428
468
|
|
|
469
|
+
async def _send_required(self, user_id: str, event: Event) -> None:
|
|
470
|
+
transport = await self._connections.get(user_id)
|
|
471
|
+
if transport is None or not transport.is_active():
|
|
472
|
+
raise internal(self.name, f"User {user_id} has no active transport")
|
|
473
|
+
try:
|
|
474
|
+
await asyncio.wait_for(
|
|
475
|
+
transport.send_event(event),
|
|
476
|
+
timeout=self._options.send_timeout,
|
|
477
|
+
)
|
|
478
|
+
except TimeoutError:
|
|
479
|
+
with contextlib.suppress(Exception):
|
|
480
|
+
await transport.close()
|
|
481
|
+
raise
|
|
482
|
+
|
|
429
483
|
async def handle_incoming_event(self, event: Event, sender_id: str) -> None:
|
|
430
484
|
if not await self._connections.has(sender_id):
|
|
431
485
|
return
|
|
@@ -515,10 +569,7 @@ class Channel:
|
|
|
515
569
|
return
|
|
516
570
|
item = _DispatchItem(event=event, recipient_ids=recipient_ids)
|
|
517
571
|
timeout = self._options.internal_queue_timeout
|
|
518
|
-
|
|
519
|
-
await asyncio.wait_for(self._dispatch_queue.put(item), timeout=timeout)
|
|
520
|
-
except TimeoutError:
|
|
521
|
-
return
|
|
572
|
+
await asyncio.wait_for(self._dispatch_queue.put(item), timeout=timeout)
|
|
522
573
|
|
|
523
574
|
async def _dispatch_loop(self) -> None:
|
|
524
575
|
while True:
|
|
@@ -574,7 +625,13 @@ class Channel:
|
|
|
574
625
|
if ctx.is_blocked():
|
|
575
626
|
return
|
|
576
627
|
try:
|
|
577
|
-
await
|
|
628
|
+
await asyncio.wait_for(
|
|
629
|
+
transport.send_event(ctx.event),
|
|
630
|
+
timeout=self._options.send_timeout,
|
|
631
|
+
)
|
|
632
|
+
except TimeoutError:
|
|
633
|
+
with contextlib.suppress(Exception):
|
|
634
|
+
await transport.close()
|
|
578
635
|
except Exception:
|
|
579
636
|
return
|
|
580
637
|
|
|
@@ -597,10 +654,7 @@ class Channel:
|
|
|
597
654
|
async def _subscribe_to_pubsub(self) -> None:
|
|
598
655
|
if self._pubsub is None or not self._endpoint_path:
|
|
599
656
|
return
|
|
600
|
-
|
|
601
|
-
await self._pubsub.subscribe(self._pubsub_pattern(), self._handle_pubsub_message)
|
|
602
|
-
except Exception:
|
|
603
|
-
return
|
|
657
|
+
await self._pubsub.subscribe(self._pubsub_pattern(), self._handle_pubsub_message)
|
|
604
658
|
|
|
605
659
|
async def _unsubscribe_from_pubsub(self) -> None:
|
|
606
660
|
if self._pubsub is None or not self._endpoint_path:
|
|
@@ -614,23 +668,17 @@ class Channel:
|
|
|
614
668
|
if self._pubsub is None or not self._endpoint_path:
|
|
615
669
|
return
|
|
616
670
|
topic = self._channel_topic()
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
)
|
|
622
|
-
except Exception:
|
|
623
|
-
return
|
|
671
|
+
await self._pubsub.publish(
|
|
672
|
+
topic,
|
|
673
|
+
_event_to_distributed_bytes(event, event_subtype, self._clean_endpoint()),
|
|
674
|
+
)
|
|
624
675
|
|
|
625
676
|
async def _publish_distributed(self, msg_type: str, extra: dict[str, Any]) -> None:
|
|
626
677
|
if self._pubsub is None or not self._endpoint_path:
|
|
627
678
|
return
|
|
628
679
|
topic = self._channel_topic()
|
|
629
680
|
data = distributed_bytes(msg_type, self.node_id, self._clean_endpoint(), self.name, extra)
|
|
630
|
-
|
|
631
|
-
await self._pubsub.publish(topic, data)
|
|
632
|
-
except Exception:
|
|
633
|
-
return
|
|
681
|
+
await self._pubsub.publish(topic, data)
|
|
634
682
|
|
|
635
683
|
async def _handle_pubsub_message(self, _topic: str, data: bytes) -> None:
|
|
636
684
|
raw = parse_envelope(data)
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
from __future__ import annotations
|
|
2
2
|
|
|
3
3
|
import asyncio
|
|
4
|
+
import contextlib
|
|
4
5
|
from typing import TYPE_CHECKING, Any
|
|
5
6
|
|
|
6
7
|
from pondsocket_common import (
|
|
@@ -68,16 +69,21 @@ class JoinContext:
|
|
|
68
69
|
async with self._lock:
|
|
69
70
|
if self._responded:
|
|
70
71
|
raise bad_request(self.channel.name, "Already responded to the join request")
|
|
72
|
+
if assigns:
|
|
73
|
+
for k, v in assigns.items():
|
|
74
|
+
self.transport.set_assign(k, v)
|
|
75
|
+
await self.channel.add_user(self.transport)
|
|
76
|
+
try:
|
|
77
|
+
await self.channel.send_acknowledge(
|
|
78
|
+
self.event.request_id,
|
|
79
|
+
self.transport.get_id(),
|
|
80
|
+
)
|
|
81
|
+
except BaseException:
|
|
82
|
+
with contextlib.suppress(BaseException):
|
|
83
|
+
await self.channel.remove_user(self.transport.get_id())
|
|
84
|
+
raise
|
|
71
85
|
self._responded = True
|
|
72
86
|
self._accepted = True
|
|
73
|
-
if assigns:
|
|
74
|
-
for k, v in assigns.items():
|
|
75
|
-
self.transport.set_assign(k, v)
|
|
76
|
-
await self.channel.add_user(self.transport)
|
|
77
|
-
await self.channel.send_acknowledge(
|
|
78
|
-
self.event.request_id,
|
|
79
|
-
self.transport.get_id(),
|
|
80
|
-
)
|
|
81
87
|
return self
|
|
82
88
|
|
|
83
89
|
async def decline(self, code: int, message: str) -> None:
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
from __future__ import annotations
|
|
2
2
|
|
|
3
3
|
import asyncio
|
|
4
|
+
import contextlib
|
|
4
5
|
import time
|
|
5
6
|
from collections.abc import Awaitable, Callable
|
|
6
7
|
from dataclasses import dataclass
|
|
@@ -154,12 +155,19 @@ class Endpoint:
|
|
|
154
155
|
await self._check_max_connections()
|
|
155
156
|
await self._connections.create(transport.get_id(), transport)
|
|
156
157
|
self._connect_times[transport.get_id()] = time.monotonic()
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
158
|
+
try:
|
|
159
|
+
await self._send_connect_event(transport)
|
|
160
|
+
transport.on_close(self._on_transport_closed)
|
|
161
|
+
transport.on_message(self._handle_transport_message)
|
|
162
|
+
await transport.handle_messages()
|
|
163
|
+
except BaseException:
|
|
164
|
+
await self._connections.discard(transport.get_id())
|
|
165
|
+
self._connect_times.pop(transport.get_id(), None)
|
|
166
|
+
with contextlib.suppress(Exception):
|
|
167
|
+
await transport.close()
|
|
168
|
+
raise
|
|
160
169
|
self._fire_metric_connection_opened(transport)
|
|
161
170
|
await self._fire_on_connect(transport)
|
|
162
|
-
await transport.handle_messages()
|
|
163
171
|
|
|
164
172
|
async def _check_max_connections(self) -> None:
|
|
165
173
|
if self._options.max_connections <= 0:
|
|
@@ -180,22 +188,29 @@ class Endpoint:
|
|
|
180
188
|
event=SystemEvents.CONNECTION.value,
|
|
181
189
|
payload={"id": transport_id, "connectionId": transport_id},
|
|
182
190
|
)
|
|
183
|
-
|
|
184
|
-
await transport.send_event(ev)
|
|
185
|
-
except Exception:
|
|
186
|
-
return
|
|
191
|
+
await transport.send_event(ev)
|
|
187
192
|
|
|
188
193
|
async def _on_transport_closed(self, transport: Transport) -> None:
|
|
189
194
|
user_id = transport.get_id()
|
|
195
|
+
first_error: BaseException | None = None
|
|
190
196
|
for reg in list(self._channel_registrations):
|
|
191
197
|
for channel in await reg.lobby.list_channels():
|
|
192
198
|
if await channel.has_user(user_id):
|
|
193
|
-
|
|
199
|
+
try:
|
|
200
|
+
await channel.remove_user(
|
|
201
|
+
user_id,
|
|
202
|
+
reason=LeaveReason.CONNECTION_CLOSED.value,
|
|
203
|
+
)
|
|
204
|
+
except BaseException as exc:
|
|
205
|
+
if first_error is None:
|
|
206
|
+
first_error = exc
|
|
194
207
|
await self._connections.discard(user_id)
|
|
195
208
|
connect_at = self._connect_times.pop(user_id, None)
|
|
196
209
|
duration = time.monotonic() - connect_at if connect_at is not None else 0.0
|
|
197
210
|
self._fire_metric_connection_closed(user_id, duration)
|
|
198
211
|
await self._fire_on_disconnect(transport)
|
|
212
|
+
if first_error is not None:
|
|
213
|
+
raise first_error
|
|
199
214
|
|
|
200
215
|
async def _handle_transport_message(self, event: Event, transport: Transport) -> None:
|
|
201
216
|
action = event.action
|
|
@@ -61,11 +61,10 @@ class HeartbeatCoordinator:
|
|
|
61
61
|
async with self._lock:
|
|
62
62
|
if self._started or self._closed:
|
|
63
63
|
return
|
|
64
|
-
self._started = True
|
|
65
|
-
with contextlib.suppress(Exception):
|
|
66
64
|
await self._pubsub.subscribe(self._topic, self._on_message)
|
|
67
|
-
|
|
68
|
-
|
|
65
|
+
self._started = True
|
|
66
|
+
self._publish_task = asyncio.create_task(self._publish_loop())
|
|
67
|
+
self._cleanup_task = asyncio.create_task(self._cleanup_loop())
|
|
69
68
|
|
|
70
69
|
def register(self, channel: HeartbeatChannel) -> None:
|
|
71
70
|
self._channels.add(channel)
|
|
@@ -131,7 +130,9 @@ class HeartbeatCoordinator:
|
|
|
131
130
|
task.cancel()
|
|
132
131
|
with contextlib.suppress(asyncio.CancelledError):
|
|
133
132
|
await task
|
|
134
|
-
|
|
135
|
-
|
|
133
|
+
if self._started:
|
|
134
|
+
with contextlib.suppress(Exception):
|
|
135
|
+
await self._pubsub.unsubscribe(self._topic)
|
|
136
|
+
self._started = False
|
|
136
137
|
self._channels.clear()
|
|
137
138
|
self._last_seen.clear()
|
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
from __future__ import annotations
|
|
2
2
|
|
|
3
3
|
import asyncio
|
|
4
|
+
import contextlib
|
|
4
5
|
from typing import Any
|
|
5
6
|
|
|
6
7
|
from .errors import internal
|
|
7
8
|
from .transport import OnCloseCallback, OnMessageHandler
|
|
8
|
-
from .types import Event, TransportType
|
|
9
|
+
from .types import DEFAULT_RECEIVE_CHANNEL_BUFFER, DEFAULT_SEND_CHANNEL_BUFFER, Event, TransportType
|
|
9
10
|
|
|
10
11
|
|
|
11
12
|
class InMemoryTransport:
|
|
@@ -21,12 +22,23 @@ class InMemoryTransport:
|
|
|
21
22
|
"_receive_task",
|
|
22
23
|
)
|
|
23
24
|
|
|
24
|
-
def __init__(
|
|
25
|
+
def __init__(
|
|
26
|
+
self,
|
|
27
|
+
id: str,
|
|
28
|
+
*,
|
|
29
|
+
assigns: dict[str, Any] | None = None,
|
|
30
|
+
max_inbox_size: int = DEFAULT_RECEIVE_CHANNEL_BUFFER,
|
|
31
|
+
max_outbox_size: int = DEFAULT_SEND_CHANNEL_BUFFER,
|
|
32
|
+
) -> None:
|
|
33
|
+
if max_inbox_size <= 0:
|
|
34
|
+
raise ValueError("max_inbox_size must be greater than zero")
|
|
35
|
+
if max_outbox_size <= 0:
|
|
36
|
+
raise ValueError("max_outbox_size must be greater than zero")
|
|
25
37
|
self._id = id
|
|
26
38
|
self._assigns: dict[str, Any] = dict(assigns or {})
|
|
27
39
|
self._active = True
|
|
28
|
-
self._outbox: asyncio.Queue[Event] = asyncio.Queue()
|
|
29
|
-
self._inbox: asyncio.Queue[Event] = asyncio.Queue()
|
|
40
|
+
self._outbox: asyncio.Queue[Event] = asyncio.Queue(max_outbox_size)
|
|
41
|
+
self._inbox: asyncio.Queue[Event] = asyncio.Queue(max_inbox_size)
|
|
30
42
|
self._close_callbacks: list[OnCloseCallback] = []
|
|
31
43
|
self._message_handler: OnMessageHandler | None = None
|
|
32
44
|
self._receive_task: asyncio.Task[None] | None = None
|
|
@@ -56,8 +68,14 @@ class InMemoryTransport:
|
|
|
56
68
|
if not self._active:
|
|
57
69
|
return
|
|
58
70
|
self._active = False
|
|
59
|
-
if
|
|
71
|
+
if (
|
|
72
|
+
self._receive_task is not None
|
|
73
|
+
and self._receive_task is not asyncio.current_task()
|
|
74
|
+
and not self._receive_task.done()
|
|
75
|
+
):
|
|
60
76
|
self._receive_task.cancel()
|
|
77
|
+
with contextlib.suppress(asyncio.CancelledError):
|
|
78
|
+
await self._receive_task
|
|
61
79
|
for cb in list(self._close_callbacks):
|
|
62
80
|
try:
|
|
63
81
|
await cb(self)
|
|
@@ -30,6 +30,7 @@ OutgoingMiddlewareFn: TypeAlias = Callable[
|
|
|
30
30
|
class Lobby:
|
|
31
31
|
__slots__ = (
|
|
32
32
|
"_channels",
|
|
33
|
+
"_creation_tasks",
|
|
33
34
|
"_endpoint_path",
|
|
34
35
|
"_heartbeat",
|
|
35
36
|
"_leave_handler",
|
|
@@ -57,6 +58,7 @@ class Lobby:
|
|
|
57
58
|
self._pubsub = pubsub
|
|
58
59
|
self._heartbeat = heartbeat
|
|
59
60
|
self._channels: dict[str, Channel] = {}
|
|
61
|
+
self._creation_tasks: dict[str, asyncio.Task[Channel]] = {}
|
|
60
62
|
self._message_middleware: Middleware[MessageEvent, Channel] = Middleware()
|
|
61
63
|
self._outgoing_middleware: Middleware[OutgoingContext, None] = Middleware()
|
|
62
64
|
self._leave_handler: LeaveHandler | None = None
|
|
@@ -138,24 +140,38 @@ class Lobby:
|
|
|
138
140
|
existing = self._channels.get(name)
|
|
139
141
|
if existing is not None:
|
|
140
142
|
return existing
|
|
141
|
-
|
|
143
|
+
creation_task = self._creation_tasks.get(name)
|
|
144
|
+
if creation_task is None:
|
|
145
|
+
creation_task = asyncio.create_task(
|
|
146
|
+
self._create_channel(name),
|
|
147
|
+
name=f"pondsocket-channel-create:{name}",
|
|
148
|
+
)
|
|
149
|
+
creation_task.add_done_callback(self._consume_creation_result)
|
|
150
|
+
self._creation_tasks[name] = creation_task
|
|
151
|
+
return await asyncio.shield(creation_task)
|
|
152
|
+
|
|
153
|
+
async def _create_channel(self, name: str) -> Channel:
|
|
142
154
|
candidate = self._build_channel(name)
|
|
143
|
-
|
|
144
|
-
async with self._lock:
|
|
145
|
-
existing = self._channels.get(name)
|
|
146
|
-
if existing is not None:
|
|
147
|
-
await candidate.close()
|
|
148
|
-
return existing
|
|
149
|
-
self._channels[name] = candidate
|
|
150
|
-
|
|
155
|
+
current_task = asyncio.current_task()
|
|
151
156
|
try:
|
|
152
157
|
await candidate.start()
|
|
153
|
-
except Exception:
|
|
154
158
|
async with self._lock:
|
|
155
|
-
if self.
|
|
156
|
-
|
|
159
|
+
if self._creation_tasks.get(name) is not current_task:
|
|
160
|
+
raise asyncio.CancelledError
|
|
161
|
+
self._channels[name] = candidate
|
|
162
|
+
self._creation_tasks.pop(name, None)
|
|
163
|
+
return candidate
|
|
164
|
+
except BaseException:
|
|
165
|
+
await candidate.close()
|
|
166
|
+
async with self._lock:
|
|
167
|
+
if self._creation_tasks.get(name) is current_task:
|
|
168
|
+
self._creation_tasks.pop(name, None)
|
|
157
169
|
raise
|
|
158
|
-
|
|
170
|
+
|
|
171
|
+
@staticmethod
|
|
172
|
+
def _consume_creation_result(task: asyncio.Task[Channel]) -> None:
|
|
173
|
+
if not task.cancelled():
|
|
174
|
+
task.exception()
|
|
159
175
|
|
|
160
176
|
def _build_channel(self, name: str) -> Channel:
|
|
161
177
|
async def on_destroy() -> None:
|
|
@@ -184,5 +200,11 @@ class Lobby:
|
|
|
184
200
|
async with self._lock:
|
|
185
201
|
channels = list(self._channels.values())
|
|
186
202
|
self._channels.clear()
|
|
203
|
+
creation_tasks = list(self._creation_tasks.values())
|
|
204
|
+
self._creation_tasks.clear()
|
|
205
|
+
for task in creation_tasks:
|
|
206
|
+
task.cancel()
|
|
207
|
+
if creation_tasks:
|
|
208
|
+
await asyncio.gather(*creation_tasks, return_exceptions=True)
|
|
187
209
|
for ch in channels:
|
|
188
210
|
await ch.close()
|
|
@@ -19,6 +19,12 @@ class PubSubClosedError(Exception):
|
|
|
19
19
|
super().__init__("pubsub: closed")
|
|
20
20
|
|
|
21
21
|
|
|
22
|
+
class PubSubBackpressureError(Exception):
|
|
23
|
+
def __init__(self, topic: str) -> None:
|
|
24
|
+
super().__init__(f"pubsub: subscriber queue is full for topic {topic!r}")
|
|
25
|
+
self.topic = topic
|
|
26
|
+
|
|
27
|
+
|
|
22
28
|
def match_topic(pattern: str, topic: str) -> bool:
|
|
23
29
|
if pattern == topic:
|
|
24
30
|
return True
|
|
@@ -5,7 +5,12 @@ import contextlib
|
|
|
5
5
|
from dataclasses import dataclass
|
|
6
6
|
|
|
7
7
|
from .errors import not_found
|
|
8
|
-
from .pubsub import
|
|
8
|
+
from .pubsub import (
|
|
9
|
+
PubSubBackpressureError,
|
|
10
|
+
PubSubClosedError,
|
|
11
|
+
PubSubHandler,
|
|
12
|
+
match_topic,
|
|
13
|
+
)
|
|
9
14
|
|
|
10
15
|
|
|
11
16
|
@dataclass(slots=True)
|
|
@@ -21,7 +26,7 @@ class LocalPubSub:
|
|
|
21
26
|
|
|
22
27
|
def __init__(self, *, buffer_size: int = 100) -> None:
|
|
23
28
|
if buffer_size <= 0:
|
|
24
|
-
buffer_size
|
|
29
|
+
raise ValueError("buffer_size must be greater than zero")
|
|
25
30
|
self._buffer_size = buffer_size
|
|
26
31
|
self._closed = False
|
|
27
32
|
self._lock = asyncio.Lock()
|
|
@@ -71,11 +76,10 @@ class LocalPubSub:
|
|
|
71
76
|
for pattern, subs in self._subs.items():
|
|
72
77
|
if match_topic(pattern, topic):
|
|
73
78
|
targets.extend(subs)
|
|
79
|
+
if any(sub.queue.full() for sub in targets):
|
|
80
|
+
raise PubSubBackpressureError(topic)
|
|
74
81
|
for sub in targets:
|
|
75
|
-
|
|
76
|
-
sub.queue.put_nowait((topic, data))
|
|
77
|
-
except asyncio.QueueFull:
|
|
78
|
-
continue
|
|
82
|
+
sub.queue.put_nowait((topic, data))
|
|
79
83
|
|
|
80
84
|
async def close(self) -> None:
|
|
81
85
|
async with self._lock:
|
|
@@ -7,7 +7,13 @@ from pondsocket_common import ErrorTypes, ServerActions, uuid
|
|
|
7
7
|
|
|
8
8
|
from .errors import internal
|
|
9
9
|
from .transport import OnCloseCallback, OnMessageHandler
|
|
10
|
-
from .types import
|
|
10
|
+
from .types import (
|
|
11
|
+
DEFAULT_MAX_MESSAGE_SIZE,
|
|
12
|
+
DEFAULT_SEND_CHANNEL_BUFFER,
|
|
13
|
+
Event,
|
|
14
|
+
SystemEntity,
|
|
15
|
+
TransportType,
|
|
16
|
+
)
|
|
11
17
|
|
|
12
18
|
|
|
13
19
|
class SSEServerTransport:
|
|
@@ -17,6 +23,7 @@ class SSEServerTransport:
|
|
|
17
23
|
"_close_callbacks",
|
|
18
24
|
"_closed_event",
|
|
19
25
|
"_id",
|
|
26
|
+
"_max_message_size",
|
|
20
27
|
"_message_handler",
|
|
21
28
|
"_outbound",
|
|
22
29
|
)
|
|
@@ -26,13 +33,20 @@ class SSEServerTransport:
|
|
|
26
33
|
*,
|
|
27
34
|
id: str,
|
|
28
35
|
assigns: dict[str, Any] | None = None,
|
|
36
|
+
max_queue_size: int = DEFAULT_SEND_CHANNEL_BUFFER,
|
|
37
|
+
max_message_size: int = DEFAULT_MAX_MESSAGE_SIZE,
|
|
29
38
|
) -> None:
|
|
39
|
+
if max_queue_size <= 0:
|
|
40
|
+
raise ValueError("max_queue_size must be greater than zero")
|
|
41
|
+
if max_message_size <= 0:
|
|
42
|
+
raise ValueError("max_message_size must be greater than zero")
|
|
30
43
|
self._id = id
|
|
31
44
|
self._assigns: dict[str, Any] = dict(assigns or {})
|
|
32
45
|
self._active = True
|
|
33
|
-
self._outbound: asyncio.Queue[Event] = asyncio.Queue()
|
|
46
|
+
self._outbound: asyncio.Queue[Event] = asyncio.Queue(max_queue_size)
|
|
34
47
|
self._close_callbacks: list[OnCloseCallback] = []
|
|
35
48
|
self._message_handler: OnMessageHandler | None = None
|
|
49
|
+
self._max_message_size = max_message_size
|
|
36
50
|
self._closed_event = asyncio.Event()
|
|
37
51
|
|
|
38
52
|
def get_id(self) -> str:
|
|
@@ -56,7 +70,32 @@ class SSEServerTransport:
|
|
|
56
70
|
async def send_event(self, event: Event) -> None:
|
|
57
71
|
if not self._active:
|
|
58
72
|
raise internal("", "Transport is closed")
|
|
59
|
-
|
|
73
|
+
try:
|
|
74
|
+
self._outbound.put_nowait(event)
|
|
75
|
+
return
|
|
76
|
+
except asyncio.QueueFull:
|
|
77
|
+
pass
|
|
78
|
+
|
|
79
|
+
put_task = asyncio.create_task(self._outbound.put(event))
|
|
80
|
+
close_task = asyncio.create_task(self._closed_event.wait())
|
|
81
|
+
try:
|
|
82
|
+
done, pending = await asyncio.wait(
|
|
83
|
+
{put_task, close_task},
|
|
84
|
+
return_when=asyncio.FIRST_COMPLETED,
|
|
85
|
+
)
|
|
86
|
+
except asyncio.CancelledError:
|
|
87
|
+
put_task.cancel()
|
|
88
|
+
close_task.cancel()
|
|
89
|
+
await asyncio.gather(put_task, close_task, return_exceptions=True)
|
|
90
|
+
raise
|
|
91
|
+
for task in pending:
|
|
92
|
+
task.cancel()
|
|
93
|
+
if pending:
|
|
94
|
+
await asyncio.gather(*pending, return_exceptions=True)
|
|
95
|
+
if put_task in done:
|
|
96
|
+
put_task.result()
|
|
97
|
+
return
|
|
98
|
+
raise internal("", "Transport closed while waiting for outbound capacity")
|
|
60
99
|
|
|
61
100
|
def on_close(self, callback: OnCloseCallback) -> None:
|
|
62
101
|
self._close_callbacks.append(callback)
|
|
@@ -70,6 +109,9 @@ class SSEServerTransport:
|
|
|
70
109
|
async def push_message(self, data: bytes) -> None:
|
|
71
110
|
if not self._active or self._message_handler is None:
|
|
72
111
|
return
|
|
112
|
+
if len(data) > self._max_message_size:
|
|
113
|
+
await self._send_invalid_message("Message exceeds configured maximum size")
|
|
114
|
+
return
|
|
73
115
|
from pondsocket_common import ValidationError
|
|
74
116
|
|
|
75
117
|
from .wire import parse_inbound_text
|
|
@@ -126,9 +168,12 @@ class SSEServerTransport:
|
|
|
126
168
|
except asyncio.CancelledError:
|
|
127
169
|
get_task.cancel()
|
|
128
170
|
close_task.cancel()
|
|
171
|
+
await asyncio.gather(get_task, close_task, return_exceptions=True)
|
|
129
172
|
raise
|
|
130
173
|
for t in pending:
|
|
131
174
|
t.cancel()
|
|
175
|
+
if pending:
|
|
176
|
+
await asyncio.gather(*pending, return_exceptions=True)
|
|
132
177
|
if get_task in done:
|
|
133
178
|
return get_task.result()
|
|
134
179
|
if not self._outbound.empty():
|