Skip to content

Commit

Permalink
Call TriggerResubscribeIfScheduled on mDNS re-discovery
Browse files Browse the repository at this point in the history
If we have active subscriptions, and we re-discover the device through
mDNS, we can ask the SDK to re-subscribe to the device immediately.
This should make device to be available quicker when they come back
online.
  • Loading branch information
agners committed Jun 10, 2024
1 parent 2728546 commit 39f9d16
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
9 changes: 9 additions & 0 deletions matter_server/server/device_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -1301,6 +1301,15 @@ def _on_mdns_operational_node_state(
elif (now - last_seen) > NODE_MDNS_BACKOFF:
# node came back online after being offline for a while or restarted
logger.info("Node %s re-discovered on MDNS", node_id)
elif state_change == ServiceStateChange.Added:
# Trigger node re-subscriptions when mDNS entry got added
logger.info("Node %s activity on MDNS, trigger resubscribe", node_id)
asyncio.create_task(
self._chip_device_controller.trigger_resubscribe_if_scheduled(
node_id, "mDNS state change detected"
)
)
return
else:
# ignore all other cases
return
Expand Down
11 changes: 11 additions & 0 deletions matter_server/server/sdk.py
Original file line number Diff line number Diff line change
Expand Up @@ -396,3 +396,14 @@ async def shutdown_subscription(self, node_id: int) -> None:
def node_has_subscription(self, node_id: int) -> bool:
"""Check if a node has an active subscription."""
return node_id in self._subscriptions

async def trigger_resubscribe_if_scheduled(self, node_id: int, reason: str) -> None:
"""Trigger resubscribe now if a resubscribe is scheduled.
If the ReadClient currently has a resubscription attempt scheduled, This
function allows to trigger that attempt immediately. This is useful
when the server side is up and communicating, and it's a good time to
try to resubscribe.
"""
if sub := self._subscriptions.get(node_id, None):
await sub.TriggerResubscribeIfScheduled(reason)

0 comments on commit 39f9d16

Please sign in to comment.