pondsocket 0.0.6__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.6 → pondsocket-0.0.8}/PKG-INFO +2 -2
- {pondsocket-0.0.6 → pondsocket-0.0.8}/pyproject.toml +1 -1
- {pondsocket-0.0.6 → pondsocket-0.0.8}/src/pondsocket/__init__.py +2 -0
- {pondsocket-0.0.6 → pondsocket-0.0.8}/src/pondsocket/channel.py +118 -48
- {pondsocket-0.0.6 → pondsocket-0.0.8}/src/pondsocket/contexts/join_context.py +14 -8
- {pondsocket-0.0.6 → pondsocket-0.0.8}/src/pondsocket/endpoint.py +24 -9
- {pondsocket-0.0.6 → pondsocket-0.0.8}/src/pondsocket/heartbeat.py +7 -6
- {pondsocket-0.0.6 → pondsocket-0.0.8}/src/pondsocket/in_memory_transport.py +23 -5
- {pondsocket-0.0.6 → pondsocket-0.0.8}/src/pondsocket/lobby.py +35 -13
- {pondsocket-0.0.6 → pondsocket-0.0.8}/src/pondsocket/presence.py +4 -0
- {pondsocket-0.0.6 → pondsocket-0.0.8}/src/pondsocket/pubsub.py +6 -0
- {pondsocket-0.0.6 → pondsocket-0.0.8}/src/pondsocket/pubsub_local.py +10 -6
- {pondsocket-0.0.6 → pondsocket-0.0.8}/src/pondsocket/sse_transport.py +48 -3
- {pondsocket-0.0.6 → pondsocket-0.0.8}/src/pondsocket/types.py +32 -0
- {pondsocket-0.0.6 → pondsocket-0.0.8}/tests/test_pondsocket_channel.py +200 -2
- pondsocket-0.0.8/tests/test_pondsocket_channel_lifecycle_adversarial.py +355 -0
- {pondsocket-0.0.6 → pondsocket-0.0.8}/tests/test_pondsocket_distributed_protocol.py +53 -0
- {pondsocket-0.0.6 → 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.6 → pondsocket-0.0.8}/tests/test_pondsocket_pubsub.py +10 -4
- {pondsocket-0.0.6 → pondsocket-0.0.8}/tests/test_pondsocket_sse_transport.py +16 -0
- {pondsocket-0.0.6 → pondsocket-0.0.8}/.gitignore +0 -0
- {pondsocket-0.0.6 → pondsocket-0.0.8}/README.md +0 -0
- {pondsocket-0.0.6 → pondsocket-0.0.8}/src/pondsocket/contexts/__init__.py +0 -0
- {pondsocket-0.0.6 → pondsocket-0.0.8}/src/pondsocket/contexts/connection_context.py +0 -0
- {pondsocket-0.0.6 → pondsocket-0.0.8}/src/pondsocket/contexts/event_context.py +0 -0
- {pondsocket-0.0.6 → pondsocket-0.0.8}/src/pondsocket/contexts/leave_context.py +0 -0
- {pondsocket-0.0.6 → pondsocket-0.0.8}/src/pondsocket/contexts/outgoing_context.py +0 -0
- {pondsocket-0.0.6 → pondsocket-0.0.8}/src/pondsocket/distributed/__init__.py +0 -0
- {pondsocket-0.0.6 → pondsocket-0.0.8}/src/pondsocket/distributed/redis_pubsub.py +0 -0
- {pondsocket-0.0.6 → pondsocket-0.0.8}/src/pondsocket/distributed_wire.py +0 -0
- {pondsocket-0.0.6 → pondsocket-0.0.8}/src/pondsocket/errors.py +0 -0
- {pondsocket-0.0.6 → pondsocket-0.0.8}/src/pondsocket/hooks.py +0 -0
- {pondsocket-0.0.6 → pondsocket-0.0.8}/src/pondsocket/middleware.py +0 -0
- {pondsocket-0.0.6 → pondsocket-0.0.8}/src/pondsocket/parser.py +0 -0
- {pondsocket-0.0.6 → pondsocket-0.0.8}/src/pondsocket/py.typed +0 -0
- {pondsocket-0.0.6 → pondsocket-0.0.8}/src/pondsocket/server.py +0 -0
- {pondsocket-0.0.6 → pondsocket-0.0.8}/src/pondsocket/store.py +0 -0
- {pondsocket-0.0.6 → pondsocket-0.0.8}/src/pondsocket/transport.py +0 -0
- {pondsocket-0.0.6 → pondsocket-0.0.8}/src/pondsocket/typed.py +0 -0
- {pondsocket-0.0.6 → pondsocket-0.0.8}/src/pondsocket/wire.py +0 -0
- {pondsocket-0.0.6 → pondsocket-0.0.8}/tests/test_pondsocket_distributed_local.py +0 -0
- {pondsocket-0.0.6 → pondsocket-0.0.8}/tests/test_pondsocket_errors.py +0 -0
- {pondsocket-0.0.6 → pondsocket-0.0.8}/tests/test_pondsocket_hooks_integration.py +0 -0
- {pondsocket-0.0.6 → pondsocket-0.0.8}/tests/test_pondsocket_hooks_unit.py +0 -0
- {pondsocket-0.0.6 → pondsocket-0.0.8}/tests/test_pondsocket_in_memory_transport.py +0 -0
- {pondsocket-0.0.6 → pondsocket-0.0.8}/tests/test_pondsocket_join_autodecline_adversarial.py +0 -0
- {pondsocket-0.0.6 → pondsocket-0.0.8}/tests/test_pondsocket_leave_event_adversarial.py +0 -0
- {pondsocket-0.0.6 → pondsocket-0.0.8}/tests/test_pondsocket_middleware.py +0 -0
- {pondsocket-0.0.6 → pondsocket-0.0.8}/tests/test_pondsocket_parser.py +0 -0
- {pondsocket-0.0.6 → pondsocket-0.0.8}/tests/test_pondsocket_presence.py +0 -0
- {pondsocket-0.0.6 → pondsocket-0.0.8}/tests/test_pondsocket_redis_pubsub.py +0 -0
- {pondsocket-0.0.6 → pondsocket-0.0.8}/tests/test_pondsocket_smoke.py +0 -0
- {pondsocket-0.0.6 → pondsocket-0.0.8}/tests/test_pondsocket_sse_invalid_message_adversarial.py +0 -0
- {pondsocket-0.0.6 → 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",
|
|
@@ -82,6 +82,7 @@ class Channel:
|
|
|
82
82
|
__slots__ = (
|
|
83
83
|
"_active",
|
|
84
84
|
"_assigns",
|
|
85
|
+
"_close_task",
|
|
85
86
|
"_connections",
|
|
86
87
|
"_dispatch_queue",
|
|
87
88
|
"_dispatch_semaphore",
|
|
@@ -121,6 +122,7 @@ class Channel:
|
|
|
121
122
|
self._on_destroy = opts.on_destroy
|
|
122
123
|
self._active: bool = False
|
|
123
124
|
self._connections: Store[Transport] = Store()
|
|
125
|
+
self._close_task: asyncio.Task[None] | None = None
|
|
124
126
|
self._assigns: Store[PondAssigns] = Store()
|
|
125
127
|
self._presence = PresenceClient(self)
|
|
126
128
|
self._dispatch_queue: asyncio.Queue[_DispatchItem] = asyncio.Queue(
|
|
@@ -133,26 +135,56 @@ class Channel:
|
|
|
133
135
|
self._user_get_waiters: dict[str, asyncio.Future[User | None]] = {}
|
|
134
136
|
|
|
135
137
|
async def start(self) -> None:
|
|
138
|
+
close_task = self._close_task
|
|
139
|
+
if close_task is not None:
|
|
140
|
+
await asyncio.shield(close_task)
|
|
136
141
|
async with self._lifecycle_lock:
|
|
137
142
|
if self._active:
|
|
138
143
|
return
|
|
144
|
+
self._close_task = None
|
|
139
145
|
self._active = True
|
|
140
146
|
self._dispatch_task = asyncio.create_task(self._dispatch_loop())
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
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
|
|
145
155
|
|
|
146
156
|
async def close(self) -> None:
|
|
147
157
|
async with self._lifecycle_lock:
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
self.
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
158
|
+
close_task = self._close_task
|
|
159
|
+
if close_task is None:
|
|
160
|
+
if not self._active:
|
|
161
|
+
return
|
|
162
|
+
self._active = False
|
|
163
|
+
close_task = asyncio.create_task(
|
|
164
|
+
self._finish_close(),
|
|
165
|
+
name=f"pondsocket-channel-close:{self.name}",
|
|
166
|
+
)
|
|
167
|
+
self._close_task = close_task
|
|
168
|
+
|
|
169
|
+
if close_task is asyncio.current_task():
|
|
170
|
+
return
|
|
171
|
+
if self._dispatch_task is asyncio.current_task():
|
|
172
|
+
return
|
|
173
|
+
await asyncio.shield(close_task)
|
|
174
|
+
|
|
175
|
+
async def _finish_close(self) -> None:
|
|
176
|
+
dispatch_task = self._dispatch_task
|
|
177
|
+
self._dispatch_task = None
|
|
178
|
+
if dispatch_task is not None and not dispatch_task.done():
|
|
179
|
+
dispatch_task.cancel()
|
|
180
|
+
with contextlib.suppress(asyncio.CancelledError):
|
|
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()
|
|
156
188
|
await self._unsubscribe_from_pubsub()
|
|
157
189
|
self._stop_liveness()
|
|
158
190
|
self._node_users.clear()
|
|
@@ -203,12 +235,17 @@ class Channel:
|
|
|
203
235
|
user_id = transport.get_id()
|
|
204
236
|
assigns = dict(transport.clone_assigns())
|
|
205
237
|
await self._connections.create(user_id, transport)
|
|
206
|
-
|
|
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
|
|
207
248
|
self._fire_metric_channel_joined(user_id)
|
|
208
|
-
await self._publish_distributed(
|
|
209
|
-
DistributedMessageType.USER_JOINED.value,
|
|
210
|
-
{"userId": user_id, "presence": {}, "assigns": assigns},
|
|
211
|
-
)
|
|
212
249
|
|
|
213
250
|
async def remove_user(
|
|
214
251
|
self,
|
|
@@ -226,20 +263,36 @@ class Channel:
|
|
|
226
263
|
presence = await self._presence.get(user_id)
|
|
227
264
|
user = User(id=user_id, assigns=dict(assigns), presence=presence)
|
|
228
265
|
|
|
266
|
+
first_error: BaseException | None = None
|
|
267
|
+
|
|
229
268
|
await self._assigns.discard(user_id)
|
|
230
|
-
|
|
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)
|
|
231
274
|
self._fire_metric_channel_left(user_id)
|
|
232
275
|
|
|
233
276
|
if not skip_distributed:
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
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
|
|
238
285
|
|
|
239
286
|
await self._fire_leave_handler(user, reason)
|
|
240
287
|
|
|
241
288
|
if await self._connections.len() == 0:
|
|
242
|
-
|
|
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
|
|
243
296
|
return True
|
|
244
297
|
|
|
245
298
|
async def _fire_leave_handler(self, user: User, reason: str) -> None:
|
|
@@ -369,12 +422,15 @@ class Channel:
|
|
|
369
422
|
await self._send_direct(user_ids, ev)
|
|
370
423
|
|
|
371
424
|
async def send_acknowledge(self, request_id: str, user_id: str) -> None:
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
425
|
+
ev = Event(
|
|
426
|
+
action=ServerActions.SYSTEM.value,
|
|
427
|
+
channel_name=self.name,
|
|
375
428
|
request_id=request_id,
|
|
376
|
-
|
|
429
|
+
event=SystemEvents.ACKNOWLEDGE.value,
|
|
430
|
+
payload={},
|
|
431
|
+
node_id=self.node_id,
|
|
377
432
|
)
|
|
433
|
+
await self._send_required(user_id, ev)
|
|
378
434
|
|
|
379
435
|
async def send_unauthorized(
|
|
380
436
|
self,
|
|
@@ -400,10 +456,30 @@ class Channel:
|
|
|
400
456
|
if transport is None or not transport.is_active():
|
|
401
457
|
continue
|
|
402
458
|
try:
|
|
403
|
-
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()
|
|
404
466
|
except Exception:
|
|
405
467
|
continue
|
|
406
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
|
+
|
|
407
483
|
async def handle_incoming_event(self, event: Event, sender_id: str) -> None:
|
|
408
484
|
if not await self._connections.has(sender_id):
|
|
409
485
|
return
|
|
@@ -493,10 +569,7 @@ class Channel:
|
|
|
493
569
|
return
|
|
494
570
|
item = _DispatchItem(event=event, recipient_ids=recipient_ids)
|
|
495
571
|
timeout = self._options.internal_queue_timeout
|
|
496
|
-
|
|
497
|
-
await asyncio.wait_for(self._dispatch_queue.put(item), timeout=timeout)
|
|
498
|
-
except TimeoutError:
|
|
499
|
-
return
|
|
572
|
+
await asyncio.wait_for(self._dispatch_queue.put(item), timeout=timeout)
|
|
500
573
|
|
|
501
574
|
async def _dispatch_loop(self) -> None:
|
|
502
575
|
while True:
|
|
@@ -552,7 +625,13 @@ class Channel:
|
|
|
552
625
|
if ctx.is_blocked():
|
|
553
626
|
return
|
|
554
627
|
try:
|
|
555
|
-
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()
|
|
556
635
|
except Exception:
|
|
557
636
|
return
|
|
558
637
|
|
|
@@ -575,10 +654,7 @@ class Channel:
|
|
|
575
654
|
async def _subscribe_to_pubsub(self) -> None:
|
|
576
655
|
if self._pubsub is None or not self._endpoint_path:
|
|
577
656
|
return
|
|
578
|
-
|
|
579
|
-
await self._pubsub.subscribe(self._pubsub_pattern(), self._handle_pubsub_message)
|
|
580
|
-
except Exception:
|
|
581
|
-
return
|
|
657
|
+
await self._pubsub.subscribe(self._pubsub_pattern(), self._handle_pubsub_message)
|
|
582
658
|
|
|
583
659
|
async def _unsubscribe_from_pubsub(self) -> None:
|
|
584
660
|
if self._pubsub is None or not self._endpoint_path:
|
|
@@ -592,23 +668,17 @@ class Channel:
|
|
|
592
668
|
if self._pubsub is None or not self._endpoint_path:
|
|
593
669
|
return
|
|
594
670
|
topic = self._channel_topic()
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
)
|
|
600
|
-
except Exception:
|
|
601
|
-
return
|
|
671
|
+
await self._pubsub.publish(
|
|
672
|
+
topic,
|
|
673
|
+
_event_to_distributed_bytes(event, event_subtype, self._clean_endpoint()),
|
|
674
|
+
)
|
|
602
675
|
|
|
603
676
|
async def _publish_distributed(self, msg_type: str, extra: dict[str, Any]) -> None:
|
|
604
677
|
if self._pubsub is None or not self._endpoint_path:
|
|
605
678
|
return
|
|
606
679
|
topic = self._channel_topic()
|
|
607
680
|
data = distributed_bytes(msg_type, self.node_id, self._clean_endpoint(), self.name, extra)
|
|
608
|
-
|
|
609
|
-
await self._pubsub.publish(topic, data)
|
|
610
|
-
except Exception:
|
|
611
|
-
return
|
|
681
|
+
await self._pubsub.publish(topic, data)
|
|
612
682
|
|
|
613
683
|
async def _handle_pubsub_message(self, _topic: str, data: bytes) -> None:
|
|
614
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:
|