pondsocket 0.0.4__tar.gz → 0.0.5__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.4 → pondsocket-0.0.5}/PKG-INFO +1 -1
- {pondsocket-0.0.4 → pondsocket-0.0.5}/pyproject.toml +1 -1
- {pondsocket-0.0.4 → pondsocket-0.0.5}/src/pondsocket/__init__.py +4 -0
- {pondsocket-0.0.4 → pondsocket-0.0.5}/src/pondsocket/channel.py +289 -31
- pondsocket-0.0.5/src/pondsocket/distributed_wire.py +62 -0
- {pondsocket-0.0.4 → pondsocket-0.0.5}/src/pondsocket/endpoint.py +5 -0
- pondsocket-0.0.5/src/pondsocket/heartbeat.py +137 -0
- {pondsocket-0.0.4 → pondsocket-0.0.5}/src/pondsocket/lobby.py +5 -0
- pondsocket-0.0.5/src/pondsocket/pubsub.py +81 -0
- {pondsocket-0.0.4 → pondsocket-0.0.5}/src/pondsocket/server.py +15 -1
- {pondsocket-0.0.4 → pondsocket-0.0.5}/src/pondsocket/types.py +21 -0
- pondsocket-0.0.5/tests/test_pondsocket_distributed_protocol.py +499 -0
- pondsocket-0.0.4/src/pondsocket/pubsub.py +0 -49
- {pondsocket-0.0.4 → pondsocket-0.0.5}/.gitignore +0 -0
- {pondsocket-0.0.4 → pondsocket-0.0.5}/README.md +0 -0
- {pondsocket-0.0.4 → pondsocket-0.0.5}/src/pondsocket/contexts/__init__.py +0 -0
- {pondsocket-0.0.4 → pondsocket-0.0.5}/src/pondsocket/contexts/connection_context.py +0 -0
- {pondsocket-0.0.4 → pondsocket-0.0.5}/src/pondsocket/contexts/event_context.py +0 -0
- {pondsocket-0.0.4 → pondsocket-0.0.5}/src/pondsocket/contexts/join_context.py +0 -0
- {pondsocket-0.0.4 → pondsocket-0.0.5}/src/pondsocket/contexts/leave_context.py +0 -0
- {pondsocket-0.0.4 → pondsocket-0.0.5}/src/pondsocket/contexts/outgoing_context.py +0 -0
- {pondsocket-0.0.4 → pondsocket-0.0.5}/src/pondsocket/distributed/__init__.py +0 -0
- {pondsocket-0.0.4 → pondsocket-0.0.5}/src/pondsocket/distributed/redis_pubsub.py +0 -0
- {pondsocket-0.0.4 → pondsocket-0.0.5}/src/pondsocket/errors.py +0 -0
- {pondsocket-0.0.4 → pondsocket-0.0.5}/src/pondsocket/hooks.py +0 -0
- {pondsocket-0.0.4 → pondsocket-0.0.5}/src/pondsocket/in_memory_transport.py +0 -0
- {pondsocket-0.0.4 → pondsocket-0.0.5}/src/pondsocket/middleware.py +0 -0
- {pondsocket-0.0.4 → pondsocket-0.0.5}/src/pondsocket/parser.py +0 -0
- {pondsocket-0.0.4 → pondsocket-0.0.5}/src/pondsocket/presence.py +0 -0
- {pondsocket-0.0.4 → pondsocket-0.0.5}/src/pondsocket/pubsub_local.py +0 -0
- {pondsocket-0.0.4 → pondsocket-0.0.5}/src/pondsocket/py.typed +0 -0
- {pondsocket-0.0.4 → pondsocket-0.0.5}/src/pondsocket/sse_transport.py +0 -0
- {pondsocket-0.0.4 → pondsocket-0.0.5}/src/pondsocket/store.py +0 -0
- {pondsocket-0.0.4 → pondsocket-0.0.5}/src/pondsocket/transport.py +0 -0
- {pondsocket-0.0.4 → pondsocket-0.0.5}/src/pondsocket/typed.py +0 -0
- {pondsocket-0.0.4 → pondsocket-0.0.5}/src/pondsocket/wire.py +0 -0
- {pondsocket-0.0.4 → pondsocket-0.0.5}/tests/test_pondsocket_channel.py +0 -0
- {pondsocket-0.0.4 → pondsocket-0.0.5}/tests/test_pondsocket_distributed_local.py +0 -0
- {pondsocket-0.0.4 → pondsocket-0.0.5}/tests/test_pondsocket_errors.py +0 -0
- {pondsocket-0.0.4 → pondsocket-0.0.5}/tests/test_pondsocket_hooks_integration.py +0 -0
- {pondsocket-0.0.4 → pondsocket-0.0.5}/tests/test_pondsocket_hooks_unit.py +0 -0
- {pondsocket-0.0.4 → pondsocket-0.0.5}/tests/test_pondsocket_in_memory_transport.py +0 -0
- {pondsocket-0.0.4 → pondsocket-0.0.5}/tests/test_pondsocket_integration.py +0 -0
- {pondsocket-0.0.4 → pondsocket-0.0.5}/tests/test_pondsocket_middleware.py +0 -0
- {pondsocket-0.0.4 → pondsocket-0.0.5}/tests/test_pondsocket_parser.py +0 -0
- {pondsocket-0.0.4 → pondsocket-0.0.5}/tests/test_pondsocket_presence.py +0 -0
- {pondsocket-0.0.4 → pondsocket-0.0.5}/tests/test_pondsocket_pubsub.py +0 -0
- {pondsocket-0.0.4 → pondsocket-0.0.5}/tests/test_pondsocket_redis_pubsub.py +0 -0
- {pondsocket-0.0.4 → pondsocket-0.0.5}/tests/test_pondsocket_smoke.py +0 -0
- {pondsocket-0.0.4 → pondsocket-0.0.5}/tests/test_pondsocket_sse_transport.py +0 -0
- {pondsocket-0.0.4 → pondsocket-0.0.5}/tests/test_pondsocket_store.py +0 -0
|
@@ -67,6 +67,7 @@ from .presence import PresenceClient
|
|
|
67
67
|
from .pubsub import (
|
|
68
68
|
PubSub,
|
|
69
69
|
PubSubClosedError,
|
|
70
|
+
format_heartbeat_topic,
|
|
70
71
|
format_message_topic,
|
|
71
72
|
format_presence_topic,
|
|
72
73
|
format_system_topic,
|
|
@@ -90,6 +91,7 @@ from .typed import (
|
|
|
90
91
|
from .types import (
|
|
91
92
|
AllExceptSender,
|
|
92
93
|
AllUsers,
|
|
94
|
+
DistributedMessageType,
|
|
93
95
|
Event,
|
|
94
96
|
InternalActions,
|
|
95
97
|
LeaveReason,
|
|
@@ -131,6 +133,7 @@ __all__ = [
|
|
|
131
133
|
"ConnectionDecision",
|
|
132
134
|
"ConnectionHandler",
|
|
133
135
|
"ConnectionMiddlewareFn",
|
|
136
|
+
"DistributedMessageType",
|
|
134
137
|
"Endpoint",
|
|
135
138
|
"EndpointMatch",
|
|
136
139
|
"Event",
|
|
@@ -201,6 +204,7 @@ __all__ = [
|
|
|
201
204
|
"event_to_pubsub_bytes",
|
|
202
205
|
"event_to_wire",
|
|
203
206
|
"forbidden",
|
|
207
|
+
"format_heartbeat_topic",
|
|
204
208
|
"format_message_topic",
|
|
205
209
|
"format_presence_topic",
|
|
206
210
|
"format_system_topic",
|
|
@@ -17,7 +17,9 @@ from pondsocket_common import (
|
|
|
17
17
|
|
|
18
18
|
from .contexts.leave_context import LeaveContext
|
|
19
19
|
from .contexts.outgoing_context import OutgoingContext
|
|
20
|
+
from .distributed_wire import distributed_bytes, parse_envelope
|
|
20
21
|
from .errors import internal, not_found
|
|
22
|
+
from .heartbeat import HeartbeatCoordinator
|
|
21
23
|
from .middleware import Middleware, execute_with_middleware
|
|
22
24
|
from .presence import PresenceClient
|
|
23
25
|
from .pubsub import PubSub, format_topic
|
|
@@ -26,6 +28,7 @@ from .transport import Transport
|
|
|
26
28
|
from .types import (
|
|
27
29
|
AllExceptSender,
|
|
28
30
|
AllUsers,
|
|
31
|
+
DistributedMessageType,
|
|
29
32
|
Event,
|
|
30
33
|
InternalActions,
|
|
31
34
|
LeaveReason,
|
|
@@ -37,6 +40,17 @@ from .types import (
|
|
|
37
40
|
User,
|
|
38
41
|
)
|
|
39
42
|
|
|
43
|
+
_LEGACY_DISTRIBUTED_TYPES = frozenset(
|
|
44
|
+
{
|
|
45
|
+
DistributedMessageType.USER_MESSAGE.value,
|
|
46
|
+
DistributedMessageType.PRESENCE_UPDATE.value,
|
|
47
|
+
DistributedMessageType.PRESENCE_REMOVED.value,
|
|
48
|
+
DistributedMessageType.ASSIGNS_UPDATE.value,
|
|
49
|
+
DistributedMessageType.EVICT_USER.value,
|
|
50
|
+
DistributedMessageType.USER_REMOVE.value,
|
|
51
|
+
}
|
|
52
|
+
)
|
|
53
|
+
|
|
40
54
|
LeaveHandler: TypeAlias = Callable[[LeaveContext], Awaitable[None]]
|
|
41
55
|
LeaveMiddlewareFn: TypeAlias = Callable[
|
|
42
56
|
[LeaveContext, Callable[[], Awaitable[None]]], Awaitable[None]
|
|
@@ -61,6 +75,7 @@ class ChannelOptions:
|
|
|
61
75
|
on_destroy: OnDestroy | None = None
|
|
62
76
|
endpoint_path: str = ""
|
|
63
77
|
pubsub: PubSub | None = None
|
|
78
|
+
heartbeat: HeartbeatCoordinator | None = None
|
|
64
79
|
|
|
65
80
|
|
|
66
81
|
class Channel:
|
|
@@ -72,15 +87,18 @@ class Channel:
|
|
|
72
87
|
"_dispatch_semaphore",
|
|
73
88
|
"_dispatch_task",
|
|
74
89
|
"_endpoint_path",
|
|
90
|
+
"_heartbeat",
|
|
75
91
|
"_leave_handler",
|
|
76
92
|
"_leave_middlewares",
|
|
77
93
|
"_lifecycle_lock",
|
|
78
94
|
"_message_middleware",
|
|
95
|
+
"_node_users",
|
|
79
96
|
"_on_destroy",
|
|
80
97
|
"_options",
|
|
81
98
|
"_outgoing_middleware",
|
|
82
99
|
"_presence",
|
|
83
100
|
"_pubsub",
|
|
101
|
+
"_user_get_waiters",
|
|
84
102
|
"name",
|
|
85
103
|
"node_id",
|
|
86
104
|
)
|
|
@@ -91,6 +109,7 @@ class Channel:
|
|
|
91
109
|
self._options = opts.options
|
|
92
110
|
self._endpoint_path = opts.endpoint_path
|
|
93
111
|
self._pubsub = opts.pubsub
|
|
112
|
+
self._heartbeat = opts.heartbeat
|
|
94
113
|
self._message_middleware: Middleware[MessageEvent, Channel] = (
|
|
95
114
|
opts.message_middleware or Middleware()
|
|
96
115
|
)
|
|
@@ -112,6 +131,8 @@ class Channel:
|
|
|
112
131
|
)
|
|
113
132
|
self._dispatch_task: asyncio.Task[None] | None = None
|
|
114
133
|
self._lifecycle_lock = asyncio.Lock()
|
|
134
|
+
self._node_users: dict[str, set[str]] = {}
|
|
135
|
+
self._user_get_waiters: dict[str, asyncio.Future[User | None]] = {}
|
|
115
136
|
|
|
116
137
|
async def start(self) -> None:
|
|
117
138
|
async with self._lifecycle_lock:
|
|
@@ -120,6 +141,8 @@ class Channel:
|
|
|
120
141
|
self._active = True
|
|
121
142
|
self._dispatch_task = asyncio.create_task(self._dispatch_loop())
|
|
122
143
|
await self._subscribe_to_pubsub()
|
|
144
|
+
await self._start_liveness()
|
|
145
|
+
await self._request_channel_state()
|
|
123
146
|
self._fire_metric_channel_created()
|
|
124
147
|
|
|
125
148
|
async def close(self) -> None:
|
|
@@ -133,9 +156,29 @@ class Channel:
|
|
|
133
156
|
await self._dispatch_task
|
|
134
157
|
self._dispatch_task = None
|
|
135
158
|
await self._unsubscribe_from_pubsub()
|
|
159
|
+
self._stop_liveness()
|
|
160
|
+
self._node_users.clear()
|
|
161
|
+
self._resolve_pending_user_gets()
|
|
136
162
|
self._fire_metric_channel_destroyed()
|
|
137
163
|
await self._fire_on_destroy()
|
|
138
164
|
|
|
165
|
+
async def _start_liveness(self) -> None:
|
|
166
|
+
if self._heartbeat is None or self._pubsub is None or not self._endpoint_path:
|
|
167
|
+
return
|
|
168
|
+
self._heartbeat.register(self)
|
|
169
|
+
await self._heartbeat.start()
|
|
170
|
+
|
|
171
|
+
def _stop_liveness(self) -> None:
|
|
172
|
+
if self._heartbeat is not None:
|
|
173
|
+
self._heartbeat.unregister(self)
|
|
174
|
+
|
|
175
|
+
def _resolve_pending_user_gets(self) -> None:
|
|
176
|
+
waiters = list(self._user_get_waiters.values())
|
|
177
|
+
self._user_get_waiters.clear()
|
|
178
|
+
for fut in waiters:
|
|
179
|
+
if not fut.done():
|
|
180
|
+
fut.set_result(None)
|
|
181
|
+
|
|
139
182
|
async def _fire_on_destroy(self) -> None:
|
|
140
183
|
handler = self._on_destroy
|
|
141
184
|
if handler is None:
|
|
@@ -160,12 +203,21 @@ class Channel:
|
|
|
160
203
|
if not self._active:
|
|
161
204
|
raise internal(self.name, "Channel is not active")
|
|
162
205
|
user_id = transport.get_id()
|
|
206
|
+
assigns = dict(transport.clone_assigns())
|
|
163
207
|
await self._connections.create(user_id, transport)
|
|
164
|
-
await self._assigns.upsert(user_id,
|
|
208
|
+
await self._assigns.upsert(user_id, assigns)
|
|
165
209
|
self._fire_metric_channel_joined(user_id)
|
|
210
|
+
await self._publish_distributed(
|
|
211
|
+
DistributedMessageType.USER_JOINED.value,
|
|
212
|
+
{"userId": user_id, "presence": {}, "assigns": assigns},
|
|
213
|
+
)
|
|
166
214
|
|
|
167
215
|
async def remove_user(
|
|
168
|
-
self,
|
|
216
|
+
self,
|
|
217
|
+
user_id: str,
|
|
218
|
+
reason: str = LeaveReason.EXPLICIT_LEAVE.value,
|
|
219
|
+
*,
|
|
220
|
+
skip_distributed: bool = False,
|
|
169
221
|
) -> None:
|
|
170
222
|
if not await self._connections.discard(user_id):
|
|
171
223
|
await self._publish_user_command("user:remove", user_id, reason)
|
|
@@ -180,6 +232,12 @@ class Channel:
|
|
|
180
232
|
await self._presence.untrack(user_id)
|
|
181
233
|
self._fire_metric_channel_left(user_id)
|
|
182
234
|
|
|
235
|
+
if not skip_distributed:
|
|
236
|
+
await self._publish_distributed(
|
|
237
|
+
DistributedMessageType.USER_LEFT.value,
|
|
238
|
+
{"userId": user_id},
|
|
239
|
+
)
|
|
240
|
+
|
|
183
241
|
await self._fire_leave_handler(user, reason)
|
|
184
242
|
|
|
185
243
|
if await self._connections.len() == 0:
|
|
@@ -197,6 +255,9 @@ class Channel:
|
|
|
197
255
|
|
|
198
256
|
async def get_user(self, user_id: str) -> User:
|
|
199
257
|
if not await self._connections.has(user_id):
|
|
258
|
+
remote = await self._request_remote_user(user_id)
|
|
259
|
+
if remote is not None:
|
|
260
|
+
return remote
|
|
200
261
|
raise not_found(self.name, f"User {user_id} not in channel")
|
|
201
262
|
assigns = await self._assigns.get(user_id, {}) or {}
|
|
202
263
|
presence: PondPresence | None = None
|
|
@@ -220,7 +281,7 @@ class Channel:
|
|
|
220
281
|
channel_name=self.name,
|
|
221
282
|
request_id=uuid(),
|
|
222
283
|
event="assigns:update",
|
|
223
|
-
payload={"userId": user_id, "
|
|
284
|
+
payload={"userId": user_id, "assigns": dict(current)},
|
|
224
285
|
node_id=self.node_id,
|
|
225
286
|
)
|
|
226
287
|
await self._publish_event_to_pubsub(ev, "assigns:update")
|
|
@@ -254,7 +315,9 @@ class Channel:
|
|
|
254
315
|
node_id=self.node_id,
|
|
255
316
|
)
|
|
256
317
|
await self._send_direct([user_id], ev)
|
|
257
|
-
await self.remove_user(
|
|
318
|
+
await self.remove_user(
|
|
319
|
+
user_id, reason=LeaveReason.EVICTED.value, skip_distributed=True
|
|
320
|
+
)
|
|
258
321
|
|
|
259
322
|
async def broadcast(self, event_name: str, payload: PondMessage) -> None:
|
|
260
323
|
await self._send_event(
|
|
@@ -500,8 +563,17 @@ class Channel:
|
|
|
500
563
|
ep = self._endpoint_path
|
|
501
564
|
return ep[1:] if ep.startswith("/") else ep
|
|
502
565
|
|
|
566
|
+
def _channel_topic(self) -> str:
|
|
567
|
+
return format_topic(
|
|
568
|
+
self._clean_endpoint(),
|
|
569
|
+
self.name,
|
|
570
|
+
"",
|
|
571
|
+
namespace=self._options.namespace,
|
|
572
|
+
prefix=self._options.key_prefix,
|
|
573
|
+
)
|
|
574
|
+
|
|
503
575
|
def _pubsub_pattern(self) -> str:
|
|
504
|
-
return
|
|
576
|
+
return self._channel_topic()
|
|
505
577
|
|
|
506
578
|
async def _subscribe_to_pubsub(self) -> None:
|
|
507
579
|
if self._pubsub is None or not self._endpoint_path:
|
|
@@ -522,7 +594,7 @@ class Channel:
|
|
|
522
594
|
async def _publish_event_to_pubsub(self, event: Event, event_subtype: str) -> None:
|
|
523
595
|
if self._pubsub is None or not self._endpoint_path:
|
|
524
596
|
return
|
|
525
|
-
topic =
|
|
597
|
+
topic = self._channel_topic()
|
|
526
598
|
try:
|
|
527
599
|
await self._pubsub.publish(
|
|
528
600
|
topic,
|
|
@@ -531,16 +603,49 @@ class Channel:
|
|
|
531
603
|
except Exception:
|
|
532
604
|
return
|
|
533
605
|
|
|
606
|
+
async def _publish_distributed(self, msg_type: str, extra: dict[str, Any]) -> None:
|
|
607
|
+
if self._pubsub is None or not self._endpoint_path:
|
|
608
|
+
return
|
|
609
|
+
topic = self._channel_topic()
|
|
610
|
+
data = distributed_bytes(
|
|
611
|
+
msg_type, self.node_id, self._clean_endpoint(), self.name, extra
|
|
612
|
+
)
|
|
613
|
+
try:
|
|
614
|
+
await self._pubsub.publish(topic, data)
|
|
615
|
+
except Exception:
|
|
616
|
+
return
|
|
617
|
+
|
|
534
618
|
async def _handle_pubsub_message(self, _topic: str, data: bytes) -> None:
|
|
535
|
-
|
|
536
|
-
if
|
|
619
|
+
raw = parse_envelope(data)
|
|
620
|
+
if raw is None:
|
|
537
621
|
return
|
|
538
|
-
|
|
622
|
+
node_id = raw.get("sourceNodeId")
|
|
623
|
+
if not isinstance(node_id, str) or node_id == self.node_id:
|
|
539
624
|
return
|
|
540
|
-
if
|
|
625
|
+
if raw.get("channelName") != self.name:
|
|
541
626
|
return
|
|
542
627
|
if not self._active:
|
|
543
628
|
return
|
|
629
|
+
msg_type = raw.get("type")
|
|
630
|
+
if msg_type in _LEGACY_DISTRIBUTED_TYPES:
|
|
631
|
+
event = _event_from_envelope(raw)
|
|
632
|
+
if event is not None:
|
|
633
|
+
await self._dispatch_event(event)
|
|
634
|
+
return
|
|
635
|
+
if msg_type == DistributedMessageType.STATE_REQUEST.value:
|
|
636
|
+
await self._handle_state_request(raw)
|
|
637
|
+
elif msg_type == DistributedMessageType.STATE_RESPONSE.value:
|
|
638
|
+
await self._handle_state_response(raw)
|
|
639
|
+
elif msg_type == DistributedMessageType.USER_JOINED.value:
|
|
640
|
+
await self._handle_remote_user_joined(raw)
|
|
641
|
+
elif msg_type == DistributedMessageType.USER_LEFT.value:
|
|
642
|
+
await self._handle_remote_user_left(raw)
|
|
643
|
+
elif msg_type == DistributedMessageType.USER_GET_REQUEST.value:
|
|
644
|
+
await self._handle_user_get_request(raw)
|
|
645
|
+
elif msg_type == DistributedMessageType.USER_GET_RESPONSE.value:
|
|
646
|
+
await self._handle_user_get_response(raw)
|
|
647
|
+
|
|
648
|
+
async def _dispatch_event(self, event: Event) -> None:
|
|
544
649
|
if event.action == ServerActions.BROADCAST.value:
|
|
545
650
|
recipients = await self._resolve_distributed_recipients(event)
|
|
546
651
|
if recipients:
|
|
@@ -581,12 +686,22 @@ class Channel:
|
|
|
581
686
|
return
|
|
582
687
|
if event.event == PresenceEventTypes.LEAVE.value:
|
|
583
688
|
await self._presence.discard(user_id)
|
|
689
|
+
if isinstance(event.payload, dict):
|
|
690
|
+
event.payload["presence"] = await self._presence.values()
|
|
584
691
|
return
|
|
585
692
|
if isinstance(changed, dict):
|
|
586
693
|
presence = {k: v for k, v in changed.items() if k not in {"userId", "id"}}
|
|
587
694
|
if not presence:
|
|
588
695
|
presence = dict(changed)
|
|
696
|
+
existed = await self._presence.has(user_id)
|
|
589
697
|
await self._presence.set_remote(user_id, presence)
|
|
698
|
+
event.event = (
|
|
699
|
+
PresenceEventTypes.UPDATE.value
|
|
700
|
+
if existed
|
|
701
|
+
else PresenceEventTypes.JOIN.value
|
|
702
|
+
)
|
|
703
|
+
if isinstance(event.payload, dict):
|
|
704
|
+
event.payload["presence"] = await self._presence.values()
|
|
590
705
|
|
|
591
706
|
async def _handle_remote_assigns(self, event: Event) -> None:
|
|
592
707
|
if not isinstance(event.payload, dict):
|
|
@@ -635,6 +750,159 @@ class Channel:
|
|
|
635
750
|
)
|
|
636
751
|
await self._publish_event_to_pubsub(ev, command)
|
|
637
752
|
|
|
753
|
+
async def _request_channel_state(self) -> None:
|
|
754
|
+
if self._pubsub is None or not self._endpoint_path:
|
|
755
|
+
return
|
|
756
|
+
await self._publish_distributed(
|
|
757
|
+
DistributedMessageType.STATE_REQUEST.value,
|
|
758
|
+
{"fromNode": self.node_id},
|
|
759
|
+
)
|
|
760
|
+
|
|
761
|
+
async def _handle_state_request(self, _raw: dict[str, Any]) -> None:
|
|
762
|
+
local_ids = await self._connections.keys()
|
|
763
|
+
if not local_ids:
|
|
764
|
+
return
|
|
765
|
+
users: list[dict[str, Any]] = []
|
|
766
|
+
for uid in local_ids:
|
|
767
|
+
assigns = await self._assigns.get(uid, {}) or {}
|
|
768
|
+
presence: PondPresence = {}
|
|
769
|
+
if await self._presence.has(uid):
|
|
770
|
+
presence = await self._presence.get(uid)
|
|
771
|
+
users.append({"id": uid, "assigns": dict(assigns), "presence": presence})
|
|
772
|
+
await self._publish_distributed(
|
|
773
|
+
DistributedMessageType.STATE_RESPONSE.value,
|
|
774
|
+
{"users": users},
|
|
775
|
+
)
|
|
776
|
+
|
|
777
|
+
async def _handle_state_response(self, raw: dict[str, Any]) -> None:
|
|
778
|
+
users = raw.get("users")
|
|
779
|
+
if not isinstance(users, list):
|
|
780
|
+
return
|
|
781
|
+
node_id = str(raw.get("sourceNodeId") or "")
|
|
782
|
+
for user in users:
|
|
783
|
+
if not isinstance(user, dict):
|
|
784
|
+
continue
|
|
785
|
+
user_id = str(user.get("id") or "")
|
|
786
|
+
if not user_id or await self._connections.has(user_id):
|
|
787
|
+
continue
|
|
788
|
+
assigns = user.get("assigns")
|
|
789
|
+
await self._assigns.upsert(
|
|
790
|
+
user_id, dict(assigns) if isinstance(assigns, dict) else {}
|
|
791
|
+
)
|
|
792
|
+
self._track_node_user(node_id, user_id)
|
|
793
|
+
presence = user.get("presence")
|
|
794
|
+
if isinstance(presence, dict) and presence:
|
|
795
|
+
await self._presence.set_remote(user_id, dict(presence))
|
|
796
|
+
|
|
797
|
+
async def _handle_remote_user_joined(self, raw: dict[str, Any]) -> None:
|
|
798
|
+
user_id = str(raw.get("userId") or "")
|
|
799
|
+
if not user_id or await self._connections.has(user_id):
|
|
800
|
+
return
|
|
801
|
+
node_id = str(raw.get("sourceNodeId") or "")
|
|
802
|
+
assigns = raw.get("assigns")
|
|
803
|
+
await self._assigns.upsert(
|
|
804
|
+
user_id, dict(assigns) if isinstance(assigns, dict) else {}
|
|
805
|
+
)
|
|
806
|
+
self._track_node_user(node_id, user_id)
|
|
807
|
+
presence = raw.get("presence")
|
|
808
|
+
if isinstance(presence, dict) and presence:
|
|
809
|
+
await self._presence.set_remote(user_id, dict(presence))
|
|
810
|
+
|
|
811
|
+
async def _handle_remote_user_left(self, raw: dict[str, Any]) -> None:
|
|
812
|
+
user_id = str(raw.get("userId") or "")
|
|
813
|
+
if not user_id or await self._connections.has(user_id):
|
|
814
|
+
return
|
|
815
|
+
node_id = str(raw.get("sourceNodeId") or "")
|
|
816
|
+
await self._assigns.discard(user_id)
|
|
817
|
+
await self._presence.discard(user_id)
|
|
818
|
+
self._untrack_node_user(node_id, user_id)
|
|
819
|
+
|
|
820
|
+
async def _handle_user_get_request(self, raw: dict[str, Any]) -> None:
|
|
821
|
+
user_id = str(raw.get("userId") or "")
|
|
822
|
+
request_id = str(raw.get("requestId") or "")
|
|
823
|
+
if not user_id or not request_id:
|
|
824
|
+
return
|
|
825
|
+
if not await self._connections.has(user_id):
|
|
826
|
+
return
|
|
827
|
+
assigns = await self._assigns.get(user_id, {}) or {}
|
|
828
|
+
presence: PondPresence = {}
|
|
829
|
+
if await self._presence.has(user_id):
|
|
830
|
+
presence = await self._presence.get(user_id)
|
|
831
|
+
await self._publish_distributed(
|
|
832
|
+
DistributedMessageType.USER_GET_RESPONSE.value,
|
|
833
|
+
{
|
|
834
|
+
"userId": user_id,
|
|
835
|
+
"requestId": request_id,
|
|
836
|
+
"assigns": dict(assigns),
|
|
837
|
+
"presence": presence,
|
|
838
|
+
},
|
|
839
|
+
)
|
|
840
|
+
|
|
841
|
+
async def _handle_user_get_response(self, raw: dict[str, Any]) -> None:
|
|
842
|
+
request_id = str(raw.get("requestId") or "")
|
|
843
|
+
fut = self._user_get_waiters.get(request_id)
|
|
844
|
+
if fut is None or fut.done():
|
|
845
|
+
return
|
|
846
|
+
assigns = raw.get("assigns")
|
|
847
|
+
presence = raw.get("presence")
|
|
848
|
+
user = User(
|
|
849
|
+
id=str(raw.get("userId") or ""),
|
|
850
|
+
assigns=dict(assigns) if isinstance(assigns, dict) else {},
|
|
851
|
+
presence=dict(presence) if isinstance(presence, dict) else None,
|
|
852
|
+
)
|
|
853
|
+
fut.set_result(user)
|
|
854
|
+
|
|
855
|
+
async def _request_remote_user(self, user_id: str) -> User | None:
|
|
856
|
+
if self._pubsub is None or not self._endpoint_path:
|
|
857
|
+
return None
|
|
858
|
+
request_id = uuid()
|
|
859
|
+
loop = asyncio.get_event_loop()
|
|
860
|
+
fut: asyncio.Future[User | None] = loop.create_future()
|
|
861
|
+
self._user_get_waiters[request_id] = fut
|
|
862
|
+
try:
|
|
863
|
+
await self._publish_distributed(
|
|
864
|
+
DistributedMessageType.USER_GET_REQUEST.value,
|
|
865
|
+
{
|
|
866
|
+
"userId": user_id,
|
|
867
|
+
"requestId": request_id,
|
|
868
|
+
"fromNode": self.node_id,
|
|
869
|
+
},
|
|
870
|
+
)
|
|
871
|
+
return await asyncio.wait_for(
|
|
872
|
+
fut, timeout=self._options.user_get_timeout
|
|
873
|
+
)
|
|
874
|
+
except (TimeoutError, asyncio.CancelledError):
|
|
875
|
+
return None
|
|
876
|
+
finally:
|
|
877
|
+
self._user_get_waiters.pop(request_id, None)
|
|
878
|
+
|
|
879
|
+
def _track_node_user(self, node_id: str, user_id: str) -> None:
|
|
880
|
+
if not node_id:
|
|
881
|
+
return
|
|
882
|
+
self._node_users.setdefault(node_id, set()).add(user_id)
|
|
883
|
+
if self._heartbeat is not None:
|
|
884
|
+
self._heartbeat.note_node(node_id)
|
|
885
|
+
|
|
886
|
+
def _untrack_node_user(self, node_id: str, user_id: str) -> None:
|
|
887
|
+
if not node_id:
|
|
888
|
+
return
|
|
889
|
+
users = self._node_users.get(node_id)
|
|
890
|
+
if users is None:
|
|
891
|
+
return
|
|
892
|
+
users.discard(user_id)
|
|
893
|
+
if not users:
|
|
894
|
+
self._node_users.pop(node_id, None)
|
|
895
|
+
|
|
896
|
+
async def evict_node_users(self, node_id: str) -> None:
|
|
897
|
+
users = self._node_users.pop(node_id, None)
|
|
898
|
+
if not users:
|
|
899
|
+
return
|
|
900
|
+
for user_id in users:
|
|
901
|
+
if await self._connections.has(user_id):
|
|
902
|
+
continue
|
|
903
|
+
await self._assigns.discard(user_id)
|
|
904
|
+
await self._presence.discard(user_id)
|
|
905
|
+
|
|
638
906
|
def _fire_metric_channel_created(self) -> None:
|
|
639
907
|
hooks = self._options.hooks
|
|
640
908
|
if hooks is None or hooks.metrics is None:
|
|
@@ -741,7 +1009,6 @@ def _event_to_distributed_bytes(event: Event, subtype: str, endpoint: str) -> by
|
|
|
741
1009
|
)
|
|
742
1010
|
elif message_type == "PRESENCE_UPDATE":
|
|
743
1011
|
payload = event.payload if isinstance(event.payload, dict) else {}
|
|
744
|
-
envelope["event"] = event.event
|
|
745
1012
|
changed = payload.get("changed", {})
|
|
746
1013
|
if event.user_id:
|
|
747
1014
|
envelope["userId"] = event.user_id
|
|
@@ -749,8 +1016,7 @@ def _event_to_distributed_bytes(event: Event, subtype: str, endpoint: str) -> by
|
|
|
749
1016
|
envelope["userId"] = str(changed.get("userId") or changed.get("id") or "")
|
|
750
1017
|
else:
|
|
751
1018
|
envelope["userId"] = ""
|
|
752
|
-
envelope["presence"] =
|
|
753
|
-
envelope["payload"] = payload
|
|
1019
|
+
envelope["presence"] = changed if isinstance(changed, dict) else {}
|
|
754
1020
|
elif message_type == "PRESENCE_REMOVED":
|
|
755
1021
|
payload = event.payload if isinstance(event.payload, dict) else {}
|
|
756
1022
|
changed = payload.get("changed", {})
|
|
@@ -760,36 +1026,28 @@ def _event_to_distributed_bytes(event: Event, subtype: str, endpoint: str) -> by
|
|
|
760
1026
|
envelope["userId"] = str(changed.get("userId") or changed.get("id") or "")
|
|
761
1027
|
else:
|
|
762
1028
|
envelope["userId"] = ""
|
|
763
|
-
envelope["payload"] = payload
|
|
764
1029
|
elif message_type == "ASSIGNS_UPDATE":
|
|
765
1030
|
payload = event.payload if isinstance(event.payload, dict) else {}
|
|
766
1031
|
envelope["userId"] = payload.get("userId", "")
|
|
767
|
-
if "key" in payload:
|
|
768
|
-
envelope["key"] = payload.get("key")
|
|
769
|
-
if "value" in payload:
|
|
770
|
-
envelope["value"] = payload.get("value")
|
|
771
1032
|
envelope["assigns"] = payload.get("assigns", {})
|
|
772
|
-
|
|
773
|
-
elif message_type in {"EVICT_USER", "USER_REMOVE"}:
|
|
1033
|
+
elif message_type == "EVICT_USER":
|
|
774
1034
|
payload = event.payload if isinstance(event.payload, dict) else {}
|
|
775
1035
|
envelope["userId"] = payload.get("userId", "")
|
|
776
1036
|
envelope["reason"] = payload.get("reason", "")
|
|
1037
|
+
elif message_type == "USER_REMOVE":
|
|
1038
|
+
payload = event.payload if isinstance(event.payload, dict) else {}
|
|
1039
|
+
envelope["userId"] = payload.get("userId", "")
|
|
777
1040
|
return json.dumps(envelope, ensure_ascii=False, separators=(",", ":")).encode("utf-8")
|
|
778
1041
|
|
|
779
1042
|
|
|
780
1043
|
def _distributed_bytes_to_event(data: bytes) -> Event | None:
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
try:
|
|
784
|
-
raw = json.loads(data.decode("utf-8"))
|
|
785
|
-
except (UnicodeDecodeError, json.JSONDecodeError):
|
|
786
|
-
return None
|
|
787
|
-
if not isinstance(raw, dict):
|
|
788
|
-
return None
|
|
789
|
-
if raw.get("protocol") != "pondsocket.distributed":
|
|
790
|
-
return None
|
|
791
|
-
if raw.get("version") != 1:
|
|
1044
|
+
raw = parse_envelope(data)
|
|
1045
|
+
if raw is None:
|
|
792
1046
|
return None
|
|
1047
|
+
return _event_from_envelope(raw)
|
|
1048
|
+
|
|
1049
|
+
|
|
1050
|
+
def _event_from_envelope(raw: dict[str, Any]) -> Event | None:
|
|
793
1051
|
message_type = raw.get("type")
|
|
794
1052
|
channel_name = raw.get("channelName")
|
|
795
1053
|
node_id = raw.get("sourceNodeId")
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import json
|
|
4
|
+
import time
|
|
5
|
+
from typing import Any
|
|
6
|
+
|
|
7
|
+
from pondsocket_common import uuid
|
|
8
|
+
|
|
9
|
+
PROTOCOL = "pondsocket.distributed"
|
|
10
|
+
VERSION = 1
|
|
11
|
+
HEARTBEAT_PART = "__heartbeat__"
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def now_ms() -> int:
|
|
15
|
+
return int(time.time() * 1000)
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def distributed_bytes(
|
|
19
|
+
msg_type: str,
|
|
20
|
+
node_id: str,
|
|
21
|
+
endpoint: str,
|
|
22
|
+
channel: str,
|
|
23
|
+
extra: dict[str, Any],
|
|
24
|
+
) -> bytes:
|
|
25
|
+
envelope: dict[str, Any] = {
|
|
26
|
+
"protocol": PROTOCOL,
|
|
27
|
+
"version": VERSION,
|
|
28
|
+
"type": msg_type,
|
|
29
|
+
"messageId": uuid(),
|
|
30
|
+
"sourceNodeId": node_id,
|
|
31
|
+
"endpointName": endpoint,
|
|
32
|
+
"channelName": channel,
|
|
33
|
+
"timestamp": now_ms(),
|
|
34
|
+
}
|
|
35
|
+
envelope.update(extra)
|
|
36
|
+
return json.dumps(envelope, ensure_ascii=False, separators=(",", ":")).encode("utf-8")
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def heartbeat_bytes(node_id: str) -> bytes:
|
|
40
|
+
return distributed_bytes(
|
|
41
|
+
"NODE_HEARTBEAT",
|
|
42
|
+
node_id,
|
|
43
|
+
HEARTBEAT_PART,
|
|
44
|
+
HEARTBEAT_PART,
|
|
45
|
+
{"nodeId": node_id},
|
|
46
|
+
)
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def parse_envelope(data: bytes) -> dict[str, Any] | None:
|
|
50
|
+
try:
|
|
51
|
+
raw = json.loads(data.decode("utf-8"))
|
|
52
|
+
except (UnicodeDecodeError, json.JSONDecodeError):
|
|
53
|
+
return None
|
|
54
|
+
if not isinstance(raw, dict):
|
|
55
|
+
return None
|
|
56
|
+
if raw.get("protocol") != PROTOCOL or raw.get("version") != VERSION:
|
|
57
|
+
return None
|
|
58
|
+
if not isinstance(raw.get("type"), str):
|
|
59
|
+
return None
|
|
60
|
+
if not isinstance(raw.get("sourceNodeId"), str):
|
|
61
|
+
return None
|
|
62
|
+
return raw
|
|
@@ -12,6 +12,7 @@ from .channel import Channel
|
|
|
12
12
|
from .contexts.connection_context import ConnectionContext, ConnectionDecision
|
|
13
13
|
from .contexts.join_context import JoinContext
|
|
14
14
|
from .errors import PondError, forbidden, not_found
|
|
15
|
+
from .heartbeat import HeartbeatCoordinator
|
|
15
16
|
from .lobby import Lobby
|
|
16
17
|
from .middleware import execute_with_middleware
|
|
17
18
|
from .parser import parse
|
|
@@ -56,6 +57,7 @@ class Endpoint:
|
|
|
56
57
|
"_connection_handler",
|
|
57
58
|
"_connection_middlewares",
|
|
58
59
|
"_connections",
|
|
60
|
+
"_heartbeat",
|
|
59
61
|
"_options",
|
|
60
62
|
"_path",
|
|
61
63
|
"_pubsub",
|
|
@@ -69,6 +71,7 @@ class Endpoint:
|
|
|
69
71
|
connection_handler: ConnectionHandler,
|
|
70
72
|
options: Options | None = None,
|
|
71
73
|
pubsub: PubSub | None = None,
|
|
74
|
+
heartbeat: HeartbeatCoordinator | None = None,
|
|
72
75
|
connection_middlewares: list[ConnectionMiddlewareFn] | None = None,
|
|
73
76
|
) -> None:
|
|
74
77
|
self._path = path
|
|
@@ -76,6 +79,7 @@ class Endpoint:
|
|
|
76
79
|
self._connection_middlewares = list(connection_middlewares or [])
|
|
77
80
|
self._options = options or Options()
|
|
78
81
|
self._pubsub = pubsub
|
|
82
|
+
self._heartbeat = heartbeat
|
|
79
83
|
self._connections: Store[Transport] = Store()
|
|
80
84
|
self._channel_registrations: list[_ChannelRegistration] = []
|
|
81
85
|
self._connect_times: dict[str, float] = {}
|
|
@@ -99,6 +103,7 @@ class Endpoint:
|
|
|
99
103
|
endpoint_path=self._path,
|
|
100
104
|
options=self._options,
|
|
101
105
|
pubsub=self._pubsub,
|
|
106
|
+
heartbeat=self._heartbeat,
|
|
102
107
|
)
|
|
103
108
|
self._channel_registrations.append(
|
|
104
109
|
_ChannelRegistration(
|