pondsocket 0.0.1__tar.gz → 0.0.3__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.1 → pondsocket-0.0.3}/PKG-INFO +2 -2
- {pondsocket-0.0.1 → pondsocket-0.0.3}/pyproject.toml +2 -2
- {pondsocket-0.0.1 → pondsocket-0.0.3}/src/pondsocket/channel.py +287 -4
- {pondsocket-0.0.1 → pondsocket-0.0.3}/src/pondsocket/endpoint.py +9 -3
- {pondsocket-0.0.1 → pondsocket-0.0.3}/src/pondsocket/presence.py +8 -0
- {pondsocket-0.0.1 → pondsocket-0.0.3}/src/pondsocket/pubsub.py +7 -2
- {pondsocket-0.0.1 → pondsocket-0.0.3}/src/pondsocket/pubsub_local.py +6 -0
- {pondsocket-0.0.1 → pondsocket-0.0.3}/src/pondsocket/types.py +4 -0
- {pondsocket-0.0.1 → pondsocket-0.0.3}/tests/test_pondsocket_pubsub.py +4 -4
- {pondsocket-0.0.1 → pondsocket-0.0.3}/tests/test_pondsocket_smoke.py +1 -1
- {pondsocket-0.0.1 → pondsocket-0.0.3}/.gitignore +0 -0
- {pondsocket-0.0.1 → pondsocket-0.0.3}/README.md +0 -0
- {pondsocket-0.0.1 → pondsocket-0.0.3}/src/pondsocket/__init__.py +0 -0
- {pondsocket-0.0.1 → pondsocket-0.0.3}/src/pondsocket/contexts/__init__.py +0 -0
- {pondsocket-0.0.1 → pondsocket-0.0.3}/src/pondsocket/contexts/connection_context.py +0 -0
- {pondsocket-0.0.1 → pondsocket-0.0.3}/src/pondsocket/contexts/event_context.py +0 -0
- {pondsocket-0.0.1 → pondsocket-0.0.3}/src/pondsocket/contexts/join_context.py +0 -0
- {pondsocket-0.0.1 → pondsocket-0.0.3}/src/pondsocket/contexts/leave_context.py +0 -0
- {pondsocket-0.0.1 → pondsocket-0.0.3}/src/pondsocket/contexts/outgoing_context.py +0 -0
- {pondsocket-0.0.1 → pondsocket-0.0.3}/src/pondsocket/distributed/__init__.py +0 -0
- {pondsocket-0.0.1 → pondsocket-0.0.3}/src/pondsocket/distributed/redis_pubsub.py +0 -0
- {pondsocket-0.0.1 → pondsocket-0.0.3}/src/pondsocket/errors.py +0 -0
- {pondsocket-0.0.1 → pondsocket-0.0.3}/src/pondsocket/hooks.py +0 -0
- {pondsocket-0.0.1 → pondsocket-0.0.3}/src/pondsocket/in_memory_transport.py +0 -0
- {pondsocket-0.0.1 → pondsocket-0.0.3}/src/pondsocket/lobby.py +0 -0
- {pondsocket-0.0.1 → pondsocket-0.0.3}/src/pondsocket/middleware.py +0 -0
- {pondsocket-0.0.1 → pondsocket-0.0.3}/src/pondsocket/parser.py +0 -0
- {pondsocket-0.0.1 → pondsocket-0.0.3}/src/pondsocket/py.typed +0 -0
- {pondsocket-0.0.1 → pondsocket-0.0.3}/src/pondsocket/server.py +0 -0
- {pondsocket-0.0.1 → pondsocket-0.0.3}/src/pondsocket/sse_transport.py +0 -0
- {pondsocket-0.0.1 → pondsocket-0.0.3}/src/pondsocket/store.py +0 -0
- {pondsocket-0.0.1 → pondsocket-0.0.3}/src/pondsocket/transport.py +0 -0
- {pondsocket-0.0.1 → pondsocket-0.0.3}/src/pondsocket/wire.py +0 -0
- {pondsocket-0.0.1 → pondsocket-0.0.3}/tests/test_pondsocket_channel.py +0 -0
- {pondsocket-0.0.1 → pondsocket-0.0.3}/tests/test_pondsocket_distributed_local.py +0 -0
- {pondsocket-0.0.1 → pondsocket-0.0.3}/tests/test_pondsocket_errors.py +0 -0
- {pondsocket-0.0.1 → pondsocket-0.0.3}/tests/test_pondsocket_hooks_integration.py +0 -0
- {pondsocket-0.0.1 → pondsocket-0.0.3}/tests/test_pondsocket_hooks_unit.py +0 -0
- {pondsocket-0.0.1 → pondsocket-0.0.3}/tests/test_pondsocket_in_memory_transport.py +0 -0
- {pondsocket-0.0.1 → pondsocket-0.0.3}/tests/test_pondsocket_integration.py +0 -0
- {pondsocket-0.0.1 → pondsocket-0.0.3}/tests/test_pondsocket_middleware.py +0 -0
- {pondsocket-0.0.1 → pondsocket-0.0.3}/tests/test_pondsocket_parser.py +0 -0
- {pondsocket-0.0.1 → pondsocket-0.0.3}/tests/test_pondsocket_presence.py +0 -0
- {pondsocket-0.0.1 → pondsocket-0.0.3}/tests/test_pondsocket_redis_pubsub.py +0 -0
- {pondsocket-0.0.1 → pondsocket-0.0.3}/tests/test_pondsocket_sse_transport.py +0 -0
- {pondsocket-0.0.1 → pondsocket-0.0.3}/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.3
|
|
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.3
|
|
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.3"
|
|
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.3,<0.1.0",
|
|
11
11
|
]
|
|
12
12
|
|
|
13
13
|
[project.optional-dependencies]
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
from __future__ import annotations
|
|
2
2
|
|
|
3
3
|
import asyncio
|
|
4
|
+
import contextlib
|
|
4
5
|
from collections.abc import Awaitable, Callable
|
|
5
6
|
from dataclasses import dataclass, field
|
|
6
7
|
from typing import Any, TypeAlias
|
|
@@ -26,6 +27,7 @@ from .types import (
|
|
|
26
27
|
AllExceptSender,
|
|
27
28
|
AllUsers,
|
|
28
29
|
Event,
|
|
30
|
+
InternalActions,
|
|
29
31
|
LeaveReason,
|
|
30
32
|
MessageEvent,
|
|
31
33
|
Options,
|
|
@@ -34,7 +36,6 @@ from .types import (
|
|
|
34
36
|
ToUsers,
|
|
35
37
|
User,
|
|
36
38
|
)
|
|
37
|
-
from .wire import event_to_pubsub_bytes, pubsub_bytes_to_event
|
|
38
39
|
|
|
39
40
|
LeaveHandler: TypeAlias = Callable[[LeaveContext], Awaitable[None]]
|
|
40
41
|
LeaveMiddlewareFn: TypeAlias = Callable[
|
|
@@ -128,6 +129,9 @@ class Channel:
|
|
|
128
129
|
self._active = False
|
|
129
130
|
if self._dispatch_task is not None and not self._dispatch_task.done():
|
|
130
131
|
self._dispatch_task.cancel()
|
|
132
|
+
with contextlib.suppress(asyncio.CancelledError):
|
|
133
|
+
await self._dispatch_task
|
|
134
|
+
self._dispatch_task = None
|
|
131
135
|
await self._unsubscribe_from_pubsub()
|
|
132
136
|
self._fire_metric_channel_destroyed()
|
|
133
137
|
await self._fire_on_destroy()
|
|
@@ -164,6 +168,7 @@ class Channel:
|
|
|
164
168
|
self, user_id: str, reason: str = LeaveReason.EXPLICIT_LEAVE.value
|
|
165
169
|
) -> None:
|
|
166
170
|
if not await self._connections.discard(user_id):
|
|
171
|
+
await self._publish_user_command("user:remove", user_id, reason)
|
|
167
172
|
return
|
|
168
173
|
assigns = await self._assigns.get(user_id, {}) or {}
|
|
169
174
|
presence: PondPresence | None = None
|
|
@@ -210,6 +215,15 @@ class Channel:
|
|
|
210
215
|
current = dict(current)
|
|
211
216
|
current[key] = value
|
|
212
217
|
await self._assigns.update(user_id, current)
|
|
218
|
+
ev = Event(
|
|
219
|
+
action=InternalActions.ASSIGNS.value,
|
|
220
|
+
channel_name=self.name,
|
|
221
|
+
request_id=uuid(),
|
|
222
|
+
event="assigns:update",
|
|
223
|
+
payload={"userId": user_id, "key": key, "value": value, "assigns": dict(current)},
|
|
224
|
+
node_id=self.node_id,
|
|
225
|
+
)
|
|
226
|
+
await self._publish_event_to_pubsub(ev, "assigns:update")
|
|
213
227
|
|
|
214
228
|
async def get_assigns(self) -> dict[str, PondAssigns]:
|
|
215
229
|
snap = await self._assigns.snapshot()
|
|
@@ -228,6 +242,9 @@ class Channel:
|
|
|
228
242
|
await self._presence.untrack(user_id)
|
|
229
243
|
|
|
230
244
|
async def evict_user(self, user_id: str, reason: str) -> None:
|
|
245
|
+
if not await self._connections.has(user_id):
|
|
246
|
+
await self._publish_user_command("user:evict", user_id, reason)
|
|
247
|
+
return
|
|
231
248
|
ev = Event(
|
|
232
249
|
action=ServerActions.SYSTEM.value,
|
|
233
250
|
channel_name=self.name,
|
|
@@ -363,6 +380,7 @@ class Channel:
|
|
|
363
380
|
event=event_type.value,
|
|
364
381
|
payload={"presence": presence_snapshot, "changed": changed},
|
|
365
382
|
node_id=self.node_id,
|
|
383
|
+
user_id=changed_user_id,
|
|
366
384
|
)
|
|
367
385
|
recipients = await self._presence.keys()
|
|
368
386
|
if recipients:
|
|
@@ -395,6 +413,15 @@ class Channel:
|
|
|
395
413
|
payload=payload,
|
|
396
414
|
node_id=self.node_id,
|
|
397
415
|
)
|
|
416
|
+
match recipients:
|
|
417
|
+
case AllUsers():
|
|
418
|
+
ev.recipient_descriptor = "ALL_USERS"
|
|
419
|
+
case AllExceptSender(sender_id=sender):
|
|
420
|
+
ev.recipient_descriptor = "ALL_EXCEPT_SENDER"
|
|
421
|
+
ev.from_user_id = sender
|
|
422
|
+
case ToUsers(user_ids=ids):
|
|
423
|
+
ev.recipient_descriptor = list(ids)
|
|
424
|
+
ev.recipients = list(ids)
|
|
398
425
|
resolved = await self._resolve_recipients(recipients)
|
|
399
426
|
if resolved:
|
|
400
427
|
await self._enqueue_dispatch(ev, resolved)
|
|
@@ -497,12 +524,15 @@ class Channel:
|
|
|
497
524
|
return
|
|
498
525
|
topic = format_topic(self._clean_endpoint(), self.name, event_subtype)
|
|
499
526
|
try:
|
|
500
|
-
await self._pubsub.publish(
|
|
527
|
+
await self._pubsub.publish(
|
|
528
|
+
topic,
|
|
529
|
+
_event_to_distributed_bytes(event, event_subtype, self._clean_endpoint()),
|
|
530
|
+
)
|
|
501
531
|
except Exception:
|
|
502
532
|
return
|
|
503
533
|
|
|
504
534
|
async def _handle_pubsub_message(self, _topic: str, data: bytes) -> None:
|
|
505
|
-
event =
|
|
535
|
+
event = _distributed_bytes_to_event(data)
|
|
506
536
|
if event is None:
|
|
507
537
|
return
|
|
508
538
|
if event.node_id and event.node_id == self.node_id:
|
|
@@ -512,15 +542,98 @@ class Channel:
|
|
|
512
542
|
if not self._active:
|
|
513
543
|
return
|
|
514
544
|
if event.action == ServerActions.BROADCAST.value:
|
|
515
|
-
recipients = await self.
|
|
545
|
+
recipients = await self._resolve_distributed_recipients(event)
|
|
516
546
|
if recipients:
|
|
517
547
|
await self._enqueue_dispatch(event, recipients)
|
|
518
548
|
return
|
|
519
549
|
if event.action == ServerActions.PRESENCE.value:
|
|
550
|
+
await self._merge_remote_presence(event)
|
|
520
551
|
recipients = await self._presence.keys()
|
|
521
552
|
if recipients:
|
|
522
553
|
await self._enqueue_dispatch(event, recipients)
|
|
523
554
|
return
|
|
555
|
+
if event.action == InternalActions.ASSIGNS.value:
|
|
556
|
+
await self._handle_remote_assigns(event)
|
|
557
|
+
return
|
|
558
|
+
if event.action == InternalActions.USER_COMMAND.value:
|
|
559
|
+
await self._handle_remote_user_command(event)
|
|
560
|
+
return
|
|
561
|
+
|
|
562
|
+
async def _resolve_distributed_recipients(self, event: Event) -> list[str]:
|
|
563
|
+
local = await self._connections.keys()
|
|
564
|
+
descriptor = event.recipient_descriptor
|
|
565
|
+
if isinstance(descriptor, list):
|
|
566
|
+
wanted = {str(item) for item in descriptor}
|
|
567
|
+
return [uid for uid in local if uid in wanted]
|
|
568
|
+
if descriptor == "ALL_EXCEPT_SENDER":
|
|
569
|
+
return [uid for uid in local if uid != event.from_user_id]
|
|
570
|
+
return local
|
|
571
|
+
|
|
572
|
+
async def _merge_remote_presence(self, event: Event) -> None:
|
|
573
|
+
if not isinstance(event.payload, dict):
|
|
574
|
+
return
|
|
575
|
+
changed = event.payload.get("changed")
|
|
576
|
+
user_id = ""
|
|
577
|
+
if isinstance(changed, dict):
|
|
578
|
+
raw_id = event.user_id or changed.get("userId") or changed.get("id")
|
|
579
|
+
user_id = str(raw_id) if raw_id else ""
|
|
580
|
+
if not user_id:
|
|
581
|
+
return
|
|
582
|
+
if event.event == PresenceEventTypes.LEAVE.value:
|
|
583
|
+
await self._presence.discard(user_id)
|
|
584
|
+
return
|
|
585
|
+
if isinstance(changed, dict):
|
|
586
|
+
presence = {k: v for k, v in changed.items() if k not in {"userId", "id"}}
|
|
587
|
+
if not presence:
|
|
588
|
+
presence = dict(changed)
|
|
589
|
+
await self._presence.set_remote(user_id, presence)
|
|
590
|
+
|
|
591
|
+
async def _handle_remote_assigns(self, event: Event) -> None:
|
|
592
|
+
if not isinstance(event.payload, dict):
|
|
593
|
+
return
|
|
594
|
+
user_id = str(event.payload.get("userId") or "")
|
|
595
|
+
key = event.payload.get("key")
|
|
596
|
+
if not user_id or not isinstance(key, str):
|
|
597
|
+
remote_assigns = event.payload.get("assigns")
|
|
598
|
+
if user_id and isinstance(remote_assigns, dict) and await self._assigns.has(user_id):
|
|
599
|
+
await self._assigns.update(user_id, dict(remote_assigns))
|
|
600
|
+
return
|
|
601
|
+
if not await self._assigns.has(user_id):
|
|
602
|
+
return
|
|
603
|
+
current = await self._assigns.get(user_id, {}) or {}
|
|
604
|
+
updated = dict(current)
|
|
605
|
+
updated[key] = event.payload.get("value")
|
|
606
|
+
await self._assigns.update(user_id, updated)
|
|
607
|
+
|
|
608
|
+
async def _handle_remote_user_command(self, event: Event) -> None:
|
|
609
|
+
if not isinstance(event.payload, dict):
|
|
610
|
+
return
|
|
611
|
+
user_id = str(event.payload.get("userId") or "")
|
|
612
|
+
reason = str(event.payload.get("reason") or "remote command")
|
|
613
|
+
if not user_id or not await self._connections.has(user_id):
|
|
614
|
+
return
|
|
615
|
+
if event.event == "user:evict":
|
|
616
|
+
ev = Event(
|
|
617
|
+
action=ServerActions.SYSTEM.value,
|
|
618
|
+
channel_name=self.name,
|
|
619
|
+
request_id=uuid(),
|
|
620
|
+
event="EVICTED",
|
|
621
|
+
payload={"reason": reason},
|
|
622
|
+
node_id=self.node_id,
|
|
623
|
+
)
|
|
624
|
+
await self._send_direct([user_id], ev)
|
|
625
|
+
await self.remove_user(user_id, reason=reason)
|
|
626
|
+
|
|
627
|
+
async def _publish_user_command(self, command: str, user_id: str, reason: str) -> None:
|
|
628
|
+
ev = Event(
|
|
629
|
+
action=InternalActions.USER_COMMAND.value,
|
|
630
|
+
channel_name=self.name,
|
|
631
|
+
request_id=uuid(),
|
|
632
|
+
event=command,
|
|
633
|
+
payload={"userId": user_id, "reason": reason},
|
|
634
|
+
node_id=self.node_id,
|
|
635
|
+
)
|
|
636
|
+
await self._publish_event_to_pubsub(ev, command)
|
|
524
637
|
|
|
525
638
|
def _fire_metric_channel_created(self) -> None:
|
|
526
639
|
hooks = self._options.hooks
|
|
@@ -578,3 +691,173 @@ class Channel:
|
|
|
578
691
|
await hooks.after_message(event, err)
|
|
579
692
|
except Exception:
|
|
580
693
|
return
|
|
694
|
+
|
|
695
|
+
|
|
696
|
+
def _now_ms() -> int:
|
|
697
|
+
import time
|
|
698
|
+
|
|
699
|
+
return int(time.time() * 1000)
|
|
700
|
+
|
|
701
|
+
|
|
702
|
+
def _event_to_distributed_bytes(event: Event, subtype: str, endpoint: str) -> bytes:
|
|
703
|
+
import json
|
|
704
|
+
|
|
705
|
+
if event.action == ServerActions.BROADCAST.value:
|
|
706
|
+
message_type = "USER_MESSAGE"
|
|
707
|
+
elif event.action == ServerActions.PRESENCE.value and (
|
|
708
|
+
subtype.endswith("remove") or event.event == "LEAVE"
|
|
709
|
+
):
|
|
710
|
+
message_type = "PRESENCE_REMOVED"
|
|
711
|
+
elif event.action == ServerActions.PRESENCE.value:
|
|
712
|
+
message_type = "PRESENCE_UPDATE"
|
|
713
|
+
elif event.action == InternalActions.ASSIGNS.value:
|
|
714
|
+
message_type = "ASSIGNS_UPDATE"
|
|
715
|
+
elif event.action == InternalActions.USER_COMMAND.value and event.event == "user:remove":
|
|
716
|
+
message_type = "USER_REMOVE"
|
|
717
|
+
elif event.action == InternalActions.USER_COMMAND.value:
|
|
718
|
+
message_type = "EVICT_USER"
|
|
719
|
+
else:
|
|
720
|
+
message_type = "USER_MESSAGE"
|
|
721
|
+
|
|
722
|
+
envelope: dict[str, object] = {
|
|
723
|
+
"protocol": "pondsocket.distributed",
|
|
724
|
+
"version": 1,
|
|
725
|
+
"type": message_type,
|
|
726
|
+
"messageId": uuid(),
|
|
727
|
+
"sourceNodeId": event.node_id,
|
|
728
|
+
"endpointName": endpoint,
|
|
729
|
+
"channelName": event.channel_name,
|
|
730
|
+
"timestamp": _now_ms(),
|
|
731
|
+
}
|
|
732
|
+
if message_type == "USER_MESSAGE":
|
|
733
|
+
envelope.update(
|
|
734
|
+
{
|
|
735
|
+
"fromUserId": event.from_user_id,
|
|
736
|
+
"event": event.event,
|
|
737
|
+
"payload": event.payload if event.payload is not None else {},
|
|
738
|
+
"requestId": event.request_id,
|
|
739
|
+
"recipientDescriptor": event.recipient_descriptor,
|
|
740
|
+
}
|
|
741
|
+
)
|
|
742
|
+
elif message_type == "PRESENCE_UPDATE":
|
|
743
|
+
payload = event.payload if isinstance(event.payload, dict) else {}
|
|
744
|
+
envelope["event"] = event.event
|
|
745
|
+
changed = payload.get("changed", {})
|
|
746
|
+
if event.user_id:
|
|
747
|
+
envelope["userId"] = event.user_id
|
|
748
|
+
elif isinstance(changed, dict):
|
|
749
|
+
envelope["userId"] = str(changed.get("userId") or changed.get("id") or "")
|
|
750
|
+
else:
|
|
751
|
+
envelope["userId"] = ""
|
|
752
|
+
envelope["presence"] = payload.get("changed", {})
|
|
753
|
+
envelope["payload"] = payload
|
|
754
|
+
elif message_type == "PRESENCE_REMOVED":
|
|
755
|
+
payload = event.payload if isinstance(event.payload, dict) else {}
|
|
756
|
+
changed = payload.get("changed", {})
|
|
757
|
+
if event.user_id:
|
|
758
|
+
envelope["userId"] = event.user_id
|
|
759
|
+
elif isinstance(changed, dict):
|
|
760
|
+
envelope["userId"] = str(changed.get("userId") or changed.get("id") or "")
|
|
761
|
+
else:
|
|
762
|
+
envelope["userId"] = ""
|
|
763
|
+
envelope["payload"] = payload
|
|
764
|
+
elif message_type == "ASSIGNS_UPDATE":
|
|
765
|
+
payload = event.payload if isinstance(event.payload, dict) else {}
|
|
766
|
+
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
|
+
envelope["assigns"] = payload.get("assigns", {})
|
|
772
|
+
envelope["payload"] = payload
|
|
773
|
+
elif message_type in {"EVICT_USER", "USER_REMOVE"}:
|
|
774
|
+
payload = event.payload if isinstance(event.payload, dict) else {}
|
|
775
|
+
envelope["userId"] = payload.get("userId", "")
|
|
776
|
+
envelope["reason"] = payload.get("reason", "")
|
|
777
|
+
return json.dumps(envelope, ensure_ascii=False, separators=(",", ":")).encode("utf-8")
|
|
778
|
+
|
|
779
|
+
|
|
780
|
+
def _distributed_bytes_to_event(data: bytes) -> Event | None:
|
|
781
|
+
import json
|
|
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:
|
|
792
|
+
return None
|
|
793
|
+
message_type = raw.get("type")
|
|
794
|
+
channel_name = raw.get("channelName")
|
|
795
|
+
node_id = raw.get("sourceNodeId")
|
|
796
|
+
if not isinstance(message_type, str) or not isinstance(channel_name, str) or not isinstance(node_id, str):
|
|
797
|
+
return None
|
|
798
|
+
request_id = str(raw.get("requestId") or uuid())
|
|
799
|
+
if message_type == "USER_MESSAGE":
|
|
800
|
+
descriptor = raw.get("recipientDescriptor") or "ALL_USERS"
|
|
801
|
+
if isinstance(descriptor, list):
|
|
802
|
+
descriptor = [str(item) for item in descriptor]
|
|
803
|
+
return Event(
|
|
804
|
+
action=ServerActions.BROADCAST.value,
|
|
805
|
+
channel_name=channel_name,
|
|
806
|
+
request_id=request_id,
|
|
807
|
+
event=str(raw.get("event") or ""),
|
|
808
|
+
payload=raw.get("payload") if raw.get("payload") is not None else {},
|
|
809
|
+
node_id=node_id,
|
|
810
|
+
recipients=descriptor if isinstance(descriptor, list) else [],
|
|
811
|
+
recipient_descriptor=descriptor,
|
|
812
|
+
from_user_id=str(raw.get("fromUserId") or "CHANNEL"),
|
|
813
|
+
)
|
|
814
|
+
if message_type == "PRESENCE_UPDATE":
|
|
815
|
+
payload = raw.get("payload") if isinstance(raw.get("payload"), dict) else None
|
|
816
|
+
return Event(
|
|
817
|
+
action=ServerActions.PRESENCE.value,
|
|
818
|
+
channel_name=channel_name,
|
|
819
|
+
request_id=request_id,
|
|
820
|
+
event=str(raw.get("event") or PresenceEventTypes.UPDATE.value),
|
|
821
|
+
payload=payload or {"presence": [], "changed": raw.get("presence") or {}},
|
|
822
|
+
node_id=node_id,
|
|
823
|
+
user_id=str(raw.get("userId") or ""),
|
|
824
|
+
)
|
|
825
|
+
if message_type == "PRESENCE_REMOVED":
|
|
826
|
+
payload = raw.get("payload") if isinstance(raw.get("payload"), dict) else None
|
|
827
|
+
return Event(
|
|
828
|
+
action=ServerActions.PRESENCE.value,
|
|
829
|
+
channel_name=channel_name,
|
|
830
|
+
request_id=request_id,
|
|
831
|
+
event=PresenceEventTypes.LEAVE.value,
|
|
832
|
+
payload=payload or {"presence": [], "changed": {"userId": raw.get("userId") or ""}},
|
|
833
|
+
node_id=node_id,
|
|
834
|
+
user_id=str(raw.get("userId") or ""),
|
|
835
|
+
)
|
|
836
|
+
if message_type == "ASSIGNS_UPDATE":
|
|
837
|
+
payload = raw.get("payload") if isinstance(raw.get("payload"), dict) else None
|
|
838
|
+
assigns_payload = {
|
|
839
|
+
"userId": raw.get("userId") or "",
|
|
840
|
+
"assigns": raw.get("assigns") or {},
|
|
841
|
+
}
|
|
842
|
+
if "key" in raw:
|
|
843
|
+
assigns_payload["key"] = raw.get("key")
|
|
844
|
+
if "value" in raw:
|
|
845
|
+
assigns_payload["value"] = raw.get("value")
|
|
846
|
+
return Event(
|
|
847
|
+
action=InternalActions.ASSIGNS.value,
|
|
848
|
+
channel_name=channel_name,
|
|
849
|
+
request_id=request_id,
|
|
850
|
+
event="assigns:update",
|
|
851
|
+
payload=payload or assigns_payload,
|
|
852
|
+
node_id=node_id,
|
|
853
|
+
)
|
|
854
|
+
if message_type in {"EVICT_USER", "USER_REMOVE"}:
|
|
855
|
+
return Event(
|
|
856
|
+
action=InternalActions.USER_COMMAND.value,
|
|
857
|
+
channel_name=channel_name,
|
|
858
|
+
request_id=request_id,
|
|
859
|
+
event="user:remove" if message_type == "USER_REMOVE" else "user:evict",
|
|
860
|
+
payload={"userId": raw.get("userId") or "", "reason": raw.get("reason") or ""},
|
|
861
|
+
node_id=node_id,
|
|
862
|
+
)
|
|
863
|
+
return None
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
from __future__ import annotations
|
|
2
2
|
|
|
3
|
+
import asyncio
|
|
3
4
|
import time
|
|
4
5
|
from collections.abc import Awaitable, Callable
|
|
5
6
|
from dataclasses import dataclass
|
|
@@ -54,6 +55,7 @@ class Endpoint:
|
|
|
54
55
|
"_options",
|
|
55
56
|
"_path",
|
|
56
57
|
"_pubsub",
|
|
58
|
+
"_registration_lock",
|
|
57
59
|
)
|
|
58
60
|
|
|
59
61
|
def __init__(
|
|
@@ -73,6 +75,7 @@ class Endpoint:
|
|
|
73
75
|
self._connections: Store[Transport] = Store()
|
|
74
76
|
self._channel_registrations: list[_ChannelRegistration] = []
|
|
75
77
|
self._connect_times: dict[str, float] = {}
|
|
78
|
+
self._registration_lock = asyncio.Lock()
|
|
76
79
|
|
|
77
80
|
@property
|
|
78
81
|
def path(self) -> str:
|
|
@@ -126,9 +129,10 @@ class Endpoint:
|
|
|
126
129
|
return ctx
|
|
127
130
|
|
|
128
131
|
async def register_transport(self, transport: Transport) -> None:
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
+
async with self._registration_lock:
|
|
133
|
+
await self._check_max_connections()
|
|
134
|
+
await self._connections.create(transport.get_id(), transport)
|
|
135
|
+
self._connect_times[transport.get_id()] = time.monotonic()
|
|
132
136
|
await self._send_connect_event(transport)
|
|
133
137
|
transport.on_close(self._on_transport_closed)
|
|
134
138
|
transport.on_message(self._handle_transport_message)
|
|
@@ -212,6 +216,8 @@ class Endpoint:
|
|
|
212
216
|
await ctx.decline(401, "Join handler did not respond")
|
|
213
217
|
if ctx.is_accepted:
|
|
214
218
|
await self._fire_after_join(transport, channel_name)
|
|
219
|
+
elif await channel.user_count() == 0:
|
|
220
|
+
await channel.close()
|
|
215
221
|
return
|
|
216
222
|
await self._send_not_found(transport, event)
|
|
217
223
|
|
|
@@ -100,6 +100,14 @@ class PresenceClient:
|
|
|
100
100
|
async with self._lock:
|
|
101
101
|
return list(self._store.keys())
|
|
102
102
|
|
|
103
|
+
async def set_remote(self, user_id: str, data: PondPresence) -> None:
|
|
104
|
+
async with self._lock:
|
|
105
|
+
self._store[user_id] = data
|
|
106
|
+
|
|
107
|
+
async def discard(self, user_id: str) -> None:
|
|
108
|
+
async with self._lock:
|
|
109
|
+
self._store.pop(user_id, None)
|
|
110
|
+
|
|
103
111
|
async def values(self) -> list[PondPresence]:
|
|
104
112
|
async with self._lock:
|
|
105
113
|
return [dict(v) for v in self._store.values()]
|
|
@@ -29,7 +29,12 @@ def match_topic(pattern: str, topic: str) -> bool:
|
|
|
29
29
|
|
|
30
30
|
|
|
31
31
|
def format_topic(endpoint: str, channel: str, event: str) -> str:
|
|
32
|
-
|
|
32
|
+
del event
|
|
33
|
+
return f"pondsocket:v1:default:{endpoint}:{channel}"
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def format_heartbeat_topic() -> str:
|
|
37
|
+
return "pondsocket:v1:default:__heartbeat__"
|
|
33
38
|
|
|
34
39
|
|
|
35
40
|
def format_presence_topic(endpoint: str, channel: str) -> str:
|
|
@@ -41,4 +46,4 @@ def format_message_topic(endpoint: str, channel: str) -> str:
|
|
|
41
46
|
|
|
42
47
|
|
|
43
48
|
def format_system_topic(event: str) -> str:
|
|
44
|
-
return f"pondsocket:system:{event}"
|
|
49
|
+
return f"pondsocket:v1:default:system:{event}"
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
from __future__ import annotations
|
|
2
2
|
|
|
3
3
|
import asyncio
|
|
4
|
+
import contextlib
|
|
4
5
|
from dataclasses import dataclass
|
|
5
6
|
|
|
6
7
|
from .errors import not_found
|
|
@@ -59,6 +60,8 @@ class LocalPubSub:
|
|
|
59
60
|
subs = self._subs.pop(pattern)
|
|
60
61
|
for sub in subs:
|
|
61
62
|
sub.task.cancel()
|
|
63
|
+
with contextlib.suppress(asyncio.CancelledError):
|
|
64
|
+
await sub.task
|
|
62
65
|
|
|
63
66
|
async def publish(self, topic: str, data: bytes) -> None:
|
|
64
67
|
if self._closed:
|
|
@@ -83,3 +86,6 @@ class LocalPubSub:
|
|
|
83
86
|
self._subs.clear()
|
|
84
87
|
for sub in all_subs:
|
|
85
88
|
sub.task.cancel()
|
|
89
|
+
for sub in all_subs:
|
|
90
|
+
with contextlib.suppress(asyncio.CancelledError):
|
|
91
|
+
await sub.task
|
|
@@ -79,6 +79,10 @@ class Event:
|
|
|
79
79
|
event: str
|
|
80
80
|
payload: Any = None
|
|
81
81
|
node_id: str = ""
|
|
82
|
+
recipients: list[str] = field(default_factory=list)
|
|
83
|
+
recipient_descriptor: object = "ALL_USERS"
|
|
84
|
+
from_user_id: str = "CHANNEL"
|
|
85
|
+
user_id: str = ""
|
|
82
86
|
|
|
83
87
|
def __post_init__(self) -> None:
|
|
84
88
|
if not self.action:
|
|
@@ -38,10 +38,10 @@ def test_match_topic_minimal_wildcard_pattern_matches_prefix() -> None:
|
|
|
38
38
|
|
|
39
39
|
|
|
40
40
|
def test_format_helpers() -> None:
|
|
41
|
-
assert format_topic("api", "room", "message") == "pondsocket:api:room
|
|
42
|
-
assert format_presence_topic("api", "room") == "pondsocket:api:room
|
|
43
|
-
assert format_message_topic("api", "room") == "pondsocket:api:room
|
|
44
|
-
assert format_system_topic("ping") == "pondsocket:system:ping"
|
|
41
|
+
assert format_topic("api", "room", "message") == "pondsocket:v1:default:api:room"
|
|
42
|
+
assert format_presence_topic("api", "room") == "pondsocket:v1:default:api:room"
|
|
43
|
+
assert format_message_topic("api", "room") == "pondsocket:v1:default:api:room"
|
|
44
|
+
assert format_system_topic("ping") == "pondsocket:v1:default:system:ping"
|
|
45
45
|
|
|
46
46
|
|
|
47
47
|
def test_local_pubsub_satisfies_protocol() -> None:
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|