Skip to content

Commit 7de0366

Browse files
committed
feat(notification): add single worker handling for notification deduplication
- Introduced a check in claim_notification_slot to allow notifications when only one worker is active. - Updated tests to verify that deduplication does not occur when the worker count is set to one.
1 parent 7ca8fdc commit 7de0366

2 files changed

Lines changed: 14 additions & 0 deletions

File tree

app/notification/dedup.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from app.nats import is_nats_enabled
1111
from app.nats.kv_cas import kv_cas_json, kv_get_json
1212
from app.utils.logger import get_logger
13+
from config import server_settings
1314

1415
logger = get_logger("Notification")
1516

@@ -48,6 +49,9 @@ def _identity(value: Any) -> str:
4849

4950
async def claim_notification_slot(event_name: str, args: tuple[Any, ...], kwargs: dict[str, Any]) -> bool:
5051
"""Return True if this worker should emit the notification."""
52+
if server_settings.workers <= 1:
53+
return True
54+
5155
key = notification_fingerprint(event_name, args, kwargs)
5256
now = time.time()
5357
async with _local_lock:

tests/test_notification_dedup.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,21 @@
1010
def _reset_notification_dedup(monkeypatch: pytest.MonkeyPatch):
1111
notification_dedup._local_claims.clear()
1212
notification_dedup._kv = None
13+
monkeypatch.setattr(notification_dedup.server_settings, "workers", 4)
1314
monkeypatch.setattr(notification_dedup, "is_nats_enabled", lambda: False)
1415
yield
1516
notification_dedup._local_claims.clear()
1617

1718

19+
@pytest.mark.asyncio
20+
async def test_single_worker_does_not_dedup(monkeypatch: pytest.MonkeyPatch):
21+
monkeypatch.setattr(notification_dedup.server_settings, "workers", 1)
22+
monkeypatch.setattr(notification_dedup, "_claim_shared", AsyncMock(side_effect=AssertionError("dedup must not run")))
23+
node = NodeNotification(id=3, name="Hetz Tunnel", node_version="0.5.4", xray_version="26.3.27")
24+
assert await notification_dedup.claim_notification_slot("connect_node", (node,), {}) is True
25+
assert await notification_dedup.claim_notification_slot("connect_node", (node,), {}) is True
26+
27+
1828
@pytest.mark.asyncio
1929
async def test_claim_notification_slot_drops_same_event_twice():
2030
node = NodeNotification(id=3, name="Hetz Tunnel", node_version="0.5.4", xray_version="26.3.27")

0 commit comments

Comments
 (0)