pondsocket 0.0.4__tar.gz → 0.0.6__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.6}/PKG-INFO +2 -2
- {pondsocket-0.0.4 → pondsocket-0.0.6}/pyproject.toml +2 -2
- {pondsocket-0.0.4 → pondsocket-0.0.6}/src/pondsocket/__init__.py +4 -0
- {pondsocket-0.0.4 → pondsocket-0.0.6}/src/pondsocket/channel.py +287 -40
- pondsocket-0.0.6/src/pondsocket/distributed_wire.py +62 -0
- {pondsocket-0.0.4 → pondsocket-0.0.6}/src/pondsocket/endpoint.py +15 -15
- pondsocket-0.0.6/src/pondsocket/heartbeat.py +137 -0
- {pondsocket-0.0.4 → pondsocket-0.0.6}/src/pondsocket/lobby.py +5 -0
- pondsocket-0.0.6/src/pondsocket/pubsub.py +81 -0
- {pondsocket-0.0.4 → pondsocket-0.0.6}/src/pondsocket/server.py +15 -1
- {pondsocket-0.0.4 → pondsocket-0.0.6}/src/pondsocket/sse_transport.py +22 -2
- {pondsocket-0.0.4 → pondsocket-0.0.6}/src/pondsocket/types.py +21 -0
- pondsocket-0.0.6/tests/test_pondsocket_distributed_protocol.py +499 -0
- {pondsocket-0.0.4 → pondsocket-0.0.6}/tests/test_pondsocket_integration.py +31 -13
- pondsocket-0.0.6/tests/test_pondsocket_join_autodecline_adversarial.py +130 -0
- pondsocket-0.0.6/tests/test_pondsocket_leave_event_adversarial.py +184 -0
- pondsocket-0.0.6/tests/test_pondsocket_smoke.py +9 -0
- pondsocket-0.0.6/tests/test_pondsocket_sse_invalid_message_adversarial.py +112 -0
- {pondsocket-0.0.4 → pondsocket-0.0.6}/tests/test_pondsocket_sse_transport.py +60 -2
- pondsocket-0.0.4/src/pondsocket/pubsub.py +0 -49
- pondsocket-0.0.4/tests/test_pondsocket_smoke.py +0 -7
- {pondsocket-0.0.4 → pondsocket-0.0.6}/.gitignore +0 -0
- {pondsocket-0.0.4 → pondsocket-0.0.6}/README.md +0 -0
- {pondsocket-0.0.4 → pondsocket-0.0.6}/src/pondsocket/contexts/__init__.py +0 -0
- {pondsocket-0.0.4 → pondsocket-0.0.6}/src/pondsocket/contexts/connection_context.py +0 -0
- {pondsocket-0.0.4 → pondsocket-0.0.6}/src/pondsocket/contexts/event_context.py +0 -0
- {pondsocket-0.0.4 → pondsocket-0.0.6}/src/pondsocket/contexts/join_context.py +0 -0
- {pondsocket-0.0.4 → pondsocket-0.0.6}/src/pondsocket/contexts/leave_context.py +0 -0
- {pondsocket-0.0.4 → pondsocket-0.0.6}/src/pondsocket/contexts/outgoing_context.py +0 -0
- {pondsocket-0.0.4 → pondsocket-0.0.6}/src/pondsocket/distributed/__init__.py +0 -0
- {pondsocket-0.0.4 → pondsocket-0.0.6}/src/pondsocket/distributed/redis_pubsub.py +0 -0
- {pondsocket-0.0.4 → pondsocket-0.0.6}/src/pondsocket/errors.py +0 -0
- {pondsocket-0.0.4 → pondsocket-0.0.6}/src/pondsocket/hooks.py +0 -0
- {pondsocket-0.0.4 → pondsocket-0.0.6}/src/pondsocket/in_memory_transport.py +0 -0
- {pondsocket-0.0.4 → pondsocket-0.0.6}/src/pondsocket/middleware.py +0 -0
- {pondsocket-0.0.4 → pondsocket-0.0.6}/src/pondsocket/parser.py +0 -0
- {pondsocket-0.0.4 → pondsocket-0.0.6}/src/pondsocket/presence.py +0 -0
- {pondsocket-0.0.4 → pondsocket-0.0.6}/src/pondsocket/pubsub_local.py +0 -0
- {pondsocket-0.0.4 → pondsocket-0.0.6}/src/pondsocket/py.typed +0 -0
- {pondsocket-0.0.4 → pondsocket-0.0.6}/src/pondsocket/store.py +0 -0
- {pondsocket-0.0.4 → pondsocket-0.0.6}/src/pondsocket/transport.py +0 -0
- {pondsocket-0.0.4 → pondsocket-0.0.6}/src/pondsocket/typed.py +0 -0
- {pondsocket-0.0.4 → pondsocket-0.0.6}/src/pondsocket/wire.py +0 -0
- {pondsocket-0.0.4 → pondsocket-0.0.6}/tests/test_pondsocket_channel.py +0 -0
- {pondsocket-0.0.4 → pondsocket-0.0.6}/tests/test_pondsocket_distributed_local.py +0 -0
- {pondsocket-0.0.4 → pondsocket-0.0.6}/tests/test_pondsocket_errors.py +0 -0
- {pondsocket-0.0.4 → pondsocket-0.0.6}/tests/test_pondsocket_hooks_integration.py +0 -0
- {pondsocket-0.0.4 → pondsocket-0.0.6}/tests/test_pondsocket_hooks_unit.py +0 -0
- {pondsocket-0.0.4 → pondsocket-0.0.6}/tests/test_pondsocket_in_memory_transport.py +0 -0
- {pondsocket-0.0.4 → pondsocket-0.0.6}/tests/test_pondsocket_middleware.py +0 -0
- {pondsocket-0.0.4 → pondsocket-0.0.6}/tests/test_pondsocket_parser.py +0 -0
- {pondsocket-0.0.4 → pondsocket-0.0.6}/tests/test_pondsocket_presence.py +0 -0
- {pondsocket-0.0.4 → pondsocket-0.0.6}/tests/test_pondsocket_pubsub.py +0 -0
- {pondsocket-0.0.4 → pondsocket-0.0.6}/tests/test_pondsocket_redis_pubsub.py +0 -0
- {pondsocket-0.0.4 → pondsocket-0.0.6}/tests/test_pondsocket_store.py +0 -0
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: pondsocket
|
|
3
|
-
Version: 0.0.
|
|
3
|
+
Version: 0.0.6
|
|
4
4
|
Summary: PondSocket - channel-based real-time framework with bring-your-own-server design.
|
|
5
5
|
Author: eleven-am
|
|
6
6
|
License: GPL-3.0-or-later
|
|
7
7
|
Requires-Python: >=3.11
|
|
8
|
-
Requires-Dist: pondsocket-common<0.1.0,>=0.0.
|
|
8
|
+
Requires-Dist: pondsocket-common<0.1.0,>=0.0.6
|
|
9
9
|
Provides-Extra: redis
|
|
10
10
|
Requires-Dist: redis>=5; extra == 'redis'
|
|
11
11
|
Description-Content-Type: text/markdown
|
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
[project]
|
|
2
2
|
name = "pondsocket"
|
|
3
|
-
version = "0.0.
|
|
3
|
+
version = "0.0.6"
|
|
4
4
|
description = "PondSocket - channel-based real-time framework with bring-your-own-server design."
|
|
5
5
|
readme = "README.md"
|
|
6
6
|
requires-python = ">=3.11"
|
|
7
7
|
license = { text = "GPL-3.0-or-later" }
|
|
8
8
|
authors = [{ name = "eleven-am" }]
|
|
9
9
|
dependencies = [
|
|
10
|
-
"pondsocket-common>=0.0.
|
|
10
|
+
"pondsocket-common>=0.0.6,<0.1.0",
|
|
11
11
|
]
|
|
12
12
|
|
|
13
13
|
[project.optional-dependencies]
|
|
@@ -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
|
)
|
|
@@ -107,11 +126,11 @@ class Channel:
|
|
|
107
126
|
self._dispatch_queue: asyncio.Queue[_DispatchItem] = asyncio.Queue(
|
|
108
127
|
self._options.internal_queue_buffer
|
|
109
128
|
)
|
|
110
|
-
self._dispatch_semaphore = asyncio.Semaphore(
|
|
111
|
-
self._options.dispatch_concurrency
|
|
112
|
-
)
|
|
129
|
+
self._dispatch_semaphore = asyncio.Semaphore(self._options.dispatch_concurrency)
|
|
113
130
|
self._dispatch_task: asyncio.Task[None] | None = None
|
|
114
131
|
self._lifecycle_lock = asyncio.Lock()
|
|
132
|
+
self._node_users: dict[str, set[str]] = {}
|
|
133
|
+
self._user_get_waiters: dict[str, asyncio.Future[User | None]] = {}
|
|
115
134
|
|
|
116
135
|
async def start(self) -> None:
|
|
117
136
|
async with self._lifecycle_lock:
|
|
@@ -120,6 +139,8 @@ class Channel:
|
|
|
120
139
|
self._active = True
|
|
121
140
|
self._dispatch_task = asyncio.create_task(self._dispatch_loop())
|
|
122
141
|
await self._subscribe_to_pubsub()
|
|
142
|
+
await self._start_liveness()
|
|
143
|
+
await self._request_channel_state()
|
|
123
144
|
self._fire_metric_channel_created()
|
|
124
145
|
|
|
125
146
|
async def close(self) -> None:
|
|
@@ -133,9 +154,29 @@ class Channel:
|
|
|
133
154
|
await self._dispatch_task
|
|
134
155
|
self._dispatch_task = None
|
|
135
156
|
await self._unsubscribe_from_pubsub()
|
|
157
|
+
self._stop_liveness()
|
|
158
|
+
self._node_users.clear()
|
|
159
|
+
self._resolve_pending_user_gets()
|
|
136
160
|
self._fire_metric_channel_destroyed()
|
|
137
161
|
await self._fire_on_destroy()
|
|
138
162
|
|
|
163
|
+
async def _start_liveness(self) -> None:
|
|
164
|
+
if self._heartbeat is None or self._pubsub is None or not self._endpoint_path:
|
|
165
|
+
return
|
|
166
|
+
self._heartbeat.register(self)
|
|
167
|
+
await self._heartbeat.start()
|
|
168
|
+
|
|
169
|
+
def _stop_liveness(self) -> None:
|
|
170
|
+
if self._heartbeat is not None:
|
|
171
|
+
self._heartbeat.unregister(self)
|
|
172
|
+
|
|
173
|
+
def _resolve_pending_user_gets(self) -> None:
|
|
174
|
+
waiters = list(self._user_get_waiters.values())
|
|
175
|
+
self._user_get_waiters.clear()
|
|
176
|
+
for fut in waiters:
|
|
177
|
+
if not fut.done():
|
|
178
|
+
fut.set_result(None)
|
|
179
|
+
|
|
139
180
|
async def _fire_on_destroy(self) -> None:
|
|
140
181
|
handler = self._on_destroy
|
|
141
182
|
if handler is None:
|
|
@@ -160,16 +201,25 @@ class Channel:
|
|
|
160
201
|
if not self._active:
|
|
161
202
|
raise internal(self.name, "Channel is not active")
|
|
162
203
|
user_id = transport.get_id()
|
|
204
|
+
assigns = dict(transport.clone_assigns())
|
|
163
205
|
await self._connections.create(user_id, transport)
|
|
164
|
-
await self._assigns.upsert(user_id,
|
|
206
|
+
await self._assigns.upsert(user_id, assigns)
|
|
165
207
|
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
|
+
)
|
|
166
212
|
|
|
167
213
|
async def remove_user(
|
|
168
|
-
self,
|
|
169
|
-
|
|
214
|
+
self,
|
|
215
|
+
user_id: str,
|
|
216
|
+
reason: str = LeaveReason.EXPLICIT_LEAVE.value,
|
|
217
|
+
*,
|
|
218
|
+
skip_distributed: bool = False,
|
|
219
|
+
) -> bool:
|
|
170
220
|
if not await self._connections.discard(user_id):
|
|
171
221
|
await self._publish_user_command("user:remove", user_id, reason)
|
|
172
|
-
return
|
|
222
|
+
return False
|
|
173
223
|
assigns = await self._assigns.get(user_id, {}) or {}
|
|
174
224
|
presence: PondPresence | None = None
|
|
175
225
|
if await self._presence.has(user_id):
|
|
@@ -180,10 +230,17 @@ class Channel:
|
|
|
180
230
|
await self._presence.untrack(user_id)
|
|
181
231
|
self._fire_metric_channel_left(user_id)
|
|
182
232
|
|
|
233
|
+
if not skip_distributed:
|
|
234
|
+
await self._publish_distributed(
|
|
235
|
+
DistributedMessageType.USER_LEFT.value,
|
|
236
|
+
{"userId": user_id},
|
|
237
|
+
)
|
|
238
|
+
|
|
183
239
|
await self._fire_leave_handler(user, reason)
|
|
184
240
|
|
|
185
241
|
if await self._connections.len() == 0:
|
|
186
242
|
await self._fire_on_destroy()
|
|
243
|
+
return True
|
|
187
244
|
|
|
188
245
|
async def _fire_leave_handler(self, user: User, reason: str) -> None:
|
|
189
246
|
if self._leave_handler is None:
|
|
@@ -197,6 +254,9 @@ class Channel:
|
|
|
197
254
|
|
|
198
255
|
async def get_user(self, user_id: str) -> User:
|
|
199
256
|
if not await self._connections.has(user_id):
|
|
257
|
+
remote = await self._request_remote_user(user_id)
|
|
258
|
+
if remote is not None:
|
|
259
|
+
return remote
|
|
200
260
|
raise not_found(self.name, f"User {user_id} not in channel")
|
|
201
261
|
assigns = await self._assigns.get(user_id, {}) or {}
|
|
202
262
|
presence: PondPresence | None = None
|
|
@@ -220,7 +280,7 @@ class Channel:
|
|
|
220
280
|
channel_name=self.name,
|
|
221
281
|
request_id=uuid(),
|
|
222
282
|
event="assigns:update",
|
|
223
|
-
payload={"userId": user_id, "
|
|
283
|
+
payload={"userId": user_id, "assigns": dict(current)},
|
|
224
284
|
node_id=self.node_id,
|
|
225
285
|
)
|
|
226
286
|
await self._publish_event_to_pubsub(ev, "assigns:update")
|
|
@@ -254,7 +314,7 @@ class Channel:
|
|
|
254
314
|
node_id=self.node_id,
|
|
255
315
|
)
|
|
256
316
|
await self._send_direct([user_id], ev)
|
|
257
|
-
await self.remove_user(user_id, reason=LeaveReason.EVICTED.value)
|
|
317
|
+
await self.remove_user(user_id, reason=LeaveReason.EVICTED.value, skip_distributed=True)
|
|
258
318
|
|
|
259
319
|
async def broadcast(self, event_name: str, payload: PondMessage) -> None:
|
|
260
320
|
await self._send_event(
|
|
@@ -500,8 +560,17 @@ class Channel:
|
|
|
500
560
|
ep = self._endpoint_path
|
|
501
561
|
return ep[1:] if ep.startswith("/") else ep
|
|
502
562
|
|
|
563
|
+
def _channel_topic(self) -> str:
|
|
564
|
+
return format_topic(
|
|
565
|
+
self._clean_endpoint(),
|
|
566
|
+
self.name,
|
|
567
|
+
"",
|
|
568
|
+
namespace=self._options.namespace,
|
|
569
|
+
prefix=self._options.key_prefix,
|
|
570
|
+
)
|
|
571
|
+
|
|
503
572
|
def _pubsub_pattern(self) -> str:
|
|
504
|
-
return
|
|
573
|
+
return self._channel_topic()
|
|
505
574
|
|
|
506
575
|
async def _subscribe_to_pubsub(self) -> None:
|
|
507
576
|
if self._pubsub is None or not self._endpoint_path:
|
|
@@ -522,7 +591,7 @@ class Channel:
|
|
|
522
591
|
async def _publish_event_to_pubsub(self, event: Event, event_subtype: str) -> None:
|
|
523
592
|
if self._pubsub is None or not self._endpoint_path:
|
|
524
593
|
return
|
|
525
|
-
topic =
|
|
594
|
+
topic = self._channel_topic()
|
|
526
595
|
try:
|
|
527
596
|
await self._pubsub.publish(
|
|
528
597
|
topic,
|
|
@@ -531,16 +600,47 @@ class Channel:
|
|
|
531
600
|
except Exception:
|
|
532
601
|
return
|
|
533
602
|
|
|
603
|
+
async def _publish_distributed(self, msg_type: str, extra: dict[str, Any]) -> None:
|
|
604
|
+
if self._pubsub is None or not self._endpoint_path:
|
|
605
|
+
return
|
|
606
|
+
topic = self._channel_topic()
|
|
607
|
+
data = distributed_bytes(msg_type, self.node_id, self._clean_endpoint(), self.name, extra)
|
|
608
|
+
try:
|
|
609
|
+
await self._pubsub.publish(topic, data)
|
|
610
|
+
except Exception:
|
|
611
|
+
return
|
|
612
|
+
|
|
534
613
|
async def _handle_pubsub_message(self, _topic: str, data: bytes) -> None:
|
|
535
|
-
|
|
536
|
-
if
|
|
614
|
+
raw = parse_envelope(data)
|
|
615
|
+
if raw is None:
|
|
537
616
|
return
|
|
538
|
-
|
|
617
|
+
node_id = raw.get("sourceNodeId")
|
|
618
|
+
if not isinstance(node_id, str) or node_id == self.node_id:
|
|
539
619
|
return
|
|
540
|
-
if
|
|
620
|
+
if raw.get("channelName") != self.name:
|
|
541
621
|
return
|
|
542
622
|
if not self._active:
|
|
543
623
|
return
|
|
624
|
+
msg_type = raw.get("type")
|
|
625
|
+
if msg_type in _LEGACY_DISTRIBUTED_TYPES:
|
|
626
|
+
event = _event_from_envelope(raw)
|
|
627
|
+
if event is not None:
|
|
628
|
+
await self._dispatch_event(event)
|
|
629
|
+
return
|
|
630
|
+
if msg_type == DistributedMessageType.STATE_REQUEST.value:
|
|
631
|
+
await self._handle_state_request(raw)
|
|
632
|
+
elif msg_type == DistributedMessageType.STATE_RESPONSE.value:
|
|
633
|
+
await self._handle_state_response(raw)
|
|
634
|
+
elif msg_type == DistributedMessageType.USER_JOINED.value:
|
|
635
|
+
await self._handle_remote_user_joined(raw)
|
|
636
|
+
elif msg_type == DistributedMessageType.USER_LEFT.value:
|
|
637
|
+
await self._handle_remote_user_left(raw)
|
|
638
|
+
elif msg_type == DistributedMessageType.USER_GET_REQUEST.value:
|
|
639
|
+
await self._handle_user_get_request(raw)
|
|
640
|
+
elif msg_type == DistributedMessageType.USER_GET_RESPONSE.value:
|
|
641
|
+
await self._handle_user_get_response(raw)
|
|
642
|
+
|
|
643
|
+
async def _dispatch_event(self, event: Event) -> None:
|
|
544
644
|
if event.action == ServerActions.BROADCAST.value:
|
|
545
645
|
recipients = await self._resolve_distributed_recipients(event)
|
|
546
646
|
if recipients:
|
|
@@ -581,12 +681,20 @@ class Channel:
|
|
|
581
681
|
return
|
|
582
682
|
if event.event == PresenceEventTypes.LEAVE.value:
|
|
583
683
|
await self._presence.discard(user_id)
|
|
684
|
+
if isinstance(event.payload, dict):
|
|
685
|
+
event.payload["presence"] = await self._presence.values()
|
|
584
686
|
return
|
|
585
687
|
if isinstance(changed, dict):
|
|
586
688
|
presence = {k: v for k, v in changed.items() if k not in {"userId", "id"}}
|
|
587
689
|
if not presence:
|
|
588
690
|
presence = dict(changed)
|
|
691
|
+
existed = await self._presence.has(user_id)
|
|
589
692
|
await self._presence.set_remote(user_id, presence)
|
|
693
|
+
event.event = (
|
|
694
|
+
PresenceEventTypes.UPDATE.value if existed else PresenceEventTypes.JOIN.value
|
|
695
|
+
)
|
|
696
|
+
if isinstance(event.payload, dict):
|
|
697
|
+
event.payload["presence"] = await self._presence.values()
|
|
590
698
|
|
|
591
699
|
async def _handle_remote_assigns(self, event: Event) -> None:
|
|
592
700
|
if not isinstance(event.payload, dict):
|
|
@@ -635,6 +743,153 @@ class Channel:
|
|
|
635
743
|
)
|
|
636
744
|
await self._publish_event_to_pubsub(ev, command)
|
|
637
745
|
|
|
746
|
+
async def _request_channel_state(self) -> None:
|
|
747
|
+
if self._pubsub is None or not self._endpoint_path:
|
|
748
|
+
return
|
|
749
|
+
await self._publish_distributed(
|
|
750
|
+
DistributedMessageType.STATE_REQUEST.value,
|
|
751
|
+
{"fromNode": self.node_id},
|
|
752
|
+
)
|
|
753
|
+
|
|
754
|
+
async def _handle_state_request(self, _raw: dict[str, Any]) -> None:
|
|
755
|
+
local_ids = await self._connections.keys()
|
|
756
|
+
if not local_ids:
|
|
757
|
+
return
|
|
758
|
+
users: list[dict[str, Any]] = []
|
|
759
|
+
for uid in local_ids:
|
|
760
|
+
assigns = await self._assigns.get(uid, {}) or {}
|
|
761
|
+
presence: PondPresence = {}
|
|
762
|
+
if await self._presence.has(uid):
|
|
763
|
+
presence = await self._presence.get(uid)
|
|
764
|
+
users.append({"id": uid, "assigns": dict(assigns), "presence": presence})
|
|
765
|
+
await self._publish_distributed(
|
|
766
|
+
DistributedMessageType.STATE_RESPONSE.value,
|
|
767
|
+
{"users": users},
|
|
768
|
+
)
|
|
769
|
+
|
|
770
|
+
async def _handle_state_response(self, raw: dict[str, Any]) -> None:
|
|
771
|
+
users = raw.get("users")
|
|
772
|
+
if not isinstance(users, list):
|
|
773
|
+
return
|
|
774
|
+
node_id = str(raw.get("sourceNodeId") or "")
|
|
775
|
+
for user in users:
|
|
776
|
+
if not isinstance(user, dict):
|
|
777
|
+
continue
|
|
778
|
+
user_id = str(user.get("id") or "")
|
|
779
|
+
if not user_id or await self._connections.has(user_id):
|
|
780
|
+
continue
|
|
781
|
+
assigns = user.get("assigns")
|
|
782
|
+
await self._assigns.upsert(user_id, dict(assigns) if isinstance(assigns, dict) else {})
|
|
783
|
+
self._track_node_user(node_id, user_id)
|
|
784
|
+
presence = user.get("presence")
|
|
785
|
+
if isinstance(presence, dict) and presence:
|
|
786
|
+
await self._presence.set_remote(user_id, dict(presence))
|
|
787
|
+
|
|
788
|
+
async def _handle_remote_user_joined(self, raw: dict[str, Any]) -> None:
|
|
789
|
+
user_id = str(raw.get("userId") or "")
|
|
790
|
+
if not user_id or await self._connections.has(user_id):
|
|
791
|
+
return
|
|
792
|
+
node_id = str(raw.get("sourceNodeId") or "")
|
|
793
|
+
assigns = raw.get("assigns")
|
|
794
|
+
await self._assigns.upsert(user_id, dict(assigns) if isinstance(assigns, dict) else {})
|
|
795
|
+
self._track_node_user(node_id, user_id)
|
|
796
|
+
presence = raw.get("presence")
|
|
797
|
+
if isinstance(presence, dict) and presence:
|
|
798
|
+
await self._presence.set_remote(user_id, dict(presence))
|
|
799
|
+
|
|
800
|
+
async def _handle_remote_user_left(self, raw: dict[str, Any]) -> None:
|
|
801
|
+
user_id = str(raw.get("userId") or "")
|
|
802
|
+
if not user_id or await self._connections.has(user_id):
|
|
803
|
+
return
|
|
804
|
+
node_id = str(raw.get("sourceNodeId") or "")
|
|
805
|
+
await self._assigns.discard(user_id)
|
|
806
|
+
await self._presence.discard(user_id)
|
|
807
|
+
self._untrack_node_user(node_id, user_id)
|
|
808
|
+
|
|
809
|
+
async def _handle_user_get_request(self, raw: dict[str, Any]) -> None:
|
|
810
|
+
user_id = str(raw.get("userId") or "")
|
|
811
|
+
request_id = str(raw.get("requestId") or "")
|
|
812
|
+
if not user_id or not request_id:
|
|
813
|
+
return
|
|
814
|
+
if not await self._connections.has(user_id):
|
|
815
|
+
return
|
|
816
|
+
assigns = await self._assigns.get(user_id, {}) or {}
|
|
817
|
+
presence: PondPresence = {}
|
|
818
|
+
if await self._presence.has(user_id):
|
|
819
|
+
presence = await self._presence.get(user_id)
|
|
820
|
+
await self._publish_distributed(
|
|
821
|
+
DistributedMessageType.USER_GET_RESPONSE.value,
|
|
822
|
+
{
|
|
823
|
+
"userId": user_id,
|
|
824
|
+
"requestId": request_id,
|
|
825
|
+
"assigns": dict(assigns),
|
|
826
|
+
"presence": presence,
|
|
827
|
+
},
|
|
828
|
+
)
|
|
829
|
+
|
|
830
|
+
async def _handle_user_get_response(self, raw: dict[str, Any]) -> None:
|
|
831
|
+
request_id = str(raw.get("requestId") or "")
|
|
832
|
+
fut = self._user_get_waiters.get(request_id)
|
|
833
|
+
if fut is None or fut.done():
|
|
834
|
+
return
|
|
835
|
+
assigns = raw.get("assigns")
|
|
836
|
+
presence = raw.get("presence")
|
|
837
|
+
user = User(
|
|
838
|
+
id=str(raw.get("userId") or ""),
|
|
839
|
+
assigns=dict(assigns) if isinstance(assigns, dict) else {},
|
|
840
|
+
presence=dict(presence) if isinstance(presence, dict) else None,
|
|
841
|
+
)
|
|
842
|
+
fut.set_result(user)
|
|
843
|
+
|
|
844
|
+
async def _request_remote_user(self, user_id: str) -> User | None:
|
|
845
|
+
if self._pubsub is None or not self._endpoint_path:
|
|
846
|
+
return None
|
|
847
|
+
request_id = uuid()
|
|
848
|
+
loop = asyncio.get_event_loop()
|
|
849
|
+
fut: asyncio.Future[User | None] = loop.create_future()
|
|
850
|
+
self._user_get_waiters[request_id] = fut
|
|
851
|
+
try:
|
|
852
|
+
await self._publish_distributed(
|
|
853
|
+
DistributedMessageType.USER_GET_REQUEST.value,
|
|
854
|
+
{
|
|
855
|
+
"userId": user_id,
|
|
856
|
+
"requestId": request_id,
|
|
857
|
+
"fromNode": self.node_id,
|
|
858
|
+
},
|
|
859
|
+
)
|
|
860
|
+
return await asyncio.wait_for(fut, timeout=self._options.user_get_timeout)
|
|
861
|
+
except (TimeoutError, asyncio.CancelledError):
|
|
862
|
+
return None
|
|
863
|
+
finally:
|
|
864
|
+
self._user_get_waiters.pop(request_id, None)
|
|
865
|
+
|
|
866
|
+
def _track_node_user(self, node_id: str, user_id: str) -> None:
|
|
867
|
+
if not node_id:
|
|
868
|
+
return
|
|
869
|
+
self._node_users.setdefault(node_id, set()).add(user_id)
|
|
870
|
+
if self._heartbeat is not None:
|
|
871
|
+
self._heartbeat.note_node(node_id)
|
|
872
|
+
|
|
873
|
+
def _untrack_node_user(self, node_id: str, user_id: str) -> None:
|
|
874
|
+
if not node_id:
|
|
875
|
+
return
|
|
876
|
+
users = self._node_users.get(node_id)
|
|
877
|
+
if users is None:
|
|
878
|
+
return
|
|
879
|
+
users.discard(user_id)
|
|
880
|
+
if not users:
|
|
881
|
+
self._node_users.pop(node_id, None)
|
|
882
|
+
|
|
883
|
+
async def evict_node_users(self, node_id: str) -> None:
|
|
884
|
+
users = self._node_users.pop(node_id, None)
|
|
885
|
+
if not users:
|
|
886
|
+
return
|
|
887
|
+
for user_id in users:
|
|
888
|
+
if await self._connections.has(user_id):
|
|
889
|
+
continue
|
|
890
|
+
await self._assigns.discard(user_id)
|
|
891
|
+
await self._presence.discard(user_id)
|
|
892
|
+
|
|
638
893
|
def _fire_metric_channel_created(self) -> None:
|
|
639
894
|
hooks = self._options.hooks
|
|
640
895
|
if hooks is None or hooks.metrics is None:
|
|
@@ -681,9 +936,7 @@ class Channel:
|
|
|
681
936
|
return exc
|
|
682
937
|
return None
|
|
683
938
|
|
|
684
|
-
async def _fire_after_message(
|
|
685
|
-
self, event: Event, err: BaseException | None
|
|
686
|
-
) -> None:
|
|
939
|
+
async def _fire_after_message(self, event: Event, err: BaseException | None) -> None:
|
|
687
940
|
hooks = self._options.hooks
|
|
688
941
|
if hooks is None or hooks.after_message is None:
|
|
689
942
|
return
|
|
@@ -741,7 +994,6 @@ def _event_to_distributed_bytes(event: Event, subtype: str, endpoint: str) -> by
|
|
|
741
994
|
)
|
|
742
995
|
elif message_type == "PRESENCE_UPDATE":
|
|
743
996
|
payload = event.payload if isinstance(event.payload, dict) else {}
|
|
744
|
-
envelope["event"] = event.event
|
|
745
997
|
changed = payload.get("changed", {})
|
|
746
998
|
if event.user_id:
|
|
747
999
|
envelope["userId"] = event.user_id
|
|
@@ -749,8 +1001,7 @@ def _event_to_distributed_bytes(event: Event, subtype: str, endpoint: str) -> by
|
|
|
749
1001
|
envelope["userId"] = str(changed.get("userId") or changed.get("id") or "")
|
|
750
1002
|
else:
|
|
751
1003
|
envelope["userId"] = ""
|
|
752
|
-
envelope["presence"] =
|
|
753
|
-
envelope["payload"] = payload
|
|
1004
|
+
envelope["presence"] = changed if isinstance(changed, dict) else {}
|
|
754
1005
|
elif message_type == "PRESENCE_REMOVED":
|
|
755
1006
|
payload = event.payload if isinstance(event.payload, dict) else {}
|
|
756
1007
|
changed = payload.get("changed", {})
|
|
@@ -760,40 +1011,36 @@ def _event_to_distributed_bytes(event: Event, subtype: str, endpoint: str) -> by
|
|
|
760
1011
|
envelope["userId"] = str(changed.get("userId") or changed.get("id") or "")
|
|
761
1012
|
else:
|
|
762
1013
|
envelope["userId"] = ""
|
|
763
|
-
envelope["payload"] = payload
|
|
764
1014
|
elif message_type == "ASSIGNS_UPDATE":
|
|
765
1015
|
payload = event.payload if isinstance(event.payload, dict) else {}
|
|
766
1016
|
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
1017
|
envelope["assigns"] = payload.get("assigns", {})
|
|
772
|
-
|
|
773
|
-
elif message_type in {"EVICT_USER", "USER_REMOVE"}:
|
|
1018
|
+
elif message_type == "EVICT_USER":
|
|
774
1019
|
payload = event.payload if isinstance(event.payload, dict) else {}
|
|
775
1020
|
envelope["userId"] = payload.get("userId", "")
|
|
776
1021
|
envelope["reason"] = payload.get("reason", "")
|
|
1022
|
+
elif message_type == "USER_REMOVE":
|
|
1023
|
+
payload = event.payload if isinstance(event.payload, dict) else {}
|
|
1024
|
+
envelope["userId"] = payload.get("userId", "")
|
|
777
1025
|
return json.dumps(envelope, ensure_ascii=False, separators=(",", ":")).encode("utf-8")
|
|
778
1026
|
|
|
779
1027
|
|
|
780
1028
|
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:
|
|
1029
|
+
raw = parse_envelope(data)
|
|
1030
|
+
if raw is None:
|
|
792
1031
|
return None
|
|
1032
|
+
return _event_from_envelope(raw)
|
|
1033
|
+
|
|
1034
|
+
|
|
1035
|
+
def _event_from_envelope(raw: dict[str, Any]) -> Event | None:
|
|
793
1036
|
message_type = raw.get("type")
|
|
794
1037
|
channel_name = raw.get("channelName")
|
|
795
1038
|
node_id = raw.get("sourceNodeId")
|
|
796
|
-
if
|
|
1039
|
+
if (
|
|
1040
|
+
not isinstance(message_type, str)
|
|
1041
|
+
or not isinstance(channel_name, str)
|
|
1042
|
+
or not isinstance(node_id, str)
|
|
1043
|
+
):
|
|
797
1044
|
return None
|
|
798
1045
|
request_id = str(raw.get("requestId") or uuid())
|
|
799
1046
|
if message_type == "USER_MESSAGE":
|
|
@@ -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
|