How Discovery Works
- Subscriber Connects: When a network subscriber is created, it sends a
DiscoveryMessageon the_discoverychannel - Publisher Responds: TopicManager’s discovery thread receives the message and matches it to registered publishers
- Network Enable: Publisher’s network transport is automatically enabled
- Keep-Alive: Subscriber sends keep-alive messages every 20 seconds
- Cleanup: If keep-alive expires (60s timeout), network is automatically disabled
Discovery Message
The discovery protocol uses a simple message format that is serialized to bytes and sent over the_discovery channel:
Message Components
-
topic_hash: A 64-bit hash computed from the topic name. This enables fast topic matching without string comparisons. The hash is computed using a deterministic algorithm from the topic name, allowing TopicManager to quickly identify which publisher should respond to a discovery message. -
keep_alive_timeout_secs: The timeout duration in seconds after which a subscriber is considered disconnected if no keep-alive message is received. The default is 60 seconds. This value is set automatically by the subscriber when it creates the discovery message. -
subscriber_id: A unique 64-bit identifier for the subscriber. This ID is generated when the subscriber is created and is used to track individual subscriber connections. Multiple subscribers to the same topic will have different IDs.
Serialization
Discovery messages are serialized to bytes using theSerializableMessage trait. The #[repr(C)] attribute ensures a consistent memory layout across different platforms, making the serialization straightforward and efficient.
Message Flow
-
Subscriber sends discovery: When a network subscriber is created, it automatically serializes a
DiscoveryMessageand sends it to the_discoverychannel via Zenoh. -
TopicManager receives: TopicManager’s discovery thread continuously listens on the
_discoverychannel for incoming messages. -
Topic matching: TopicManager computes the hash for each registered publisher’s topic and compares it with the
topic_hashin the discovery message. - Network enable: When a match is found, TopicManager enables network transport for the corresponding publisher by setting an atomic flag.
- Keep-alive tracking: The subscriber’s ID and current timestamp are stored in TopicManager’s active subscribers map, which is used for keep-alive management.
Topic hashing enables fast topic matching without string comparisons. The hash is computed from the topic name using a deterministic algorithm, allowing O(1) lookup performance when matching discovery messages to publishers.
Keep-Alive Management
How Keep-Alive Works
- Initial Discovery: Subscriber sends discovery message on creation
- Periodic Updates: Subscriber sends keep-alive every 20 seconds
- Timeout: If no keep-alive received for 60 seconds, subscriber is considered disconnected
- Cleanup: TopicManager’s cleanup thread runs every 10 seconds to remove expired subscribers
- Network Disable: When no active subscribers remain, publisher’s network is automatically disabled
Keep-Alive Configuration
Keep-alive timeout is configurable per subscriber:The 60-second timeout with 20-second keep-alive intervals provides a good balance between responsiveness and network efficiency. Subscribers that disconnect are detected within 60 seconds.