@ts-drp/interval-discovery
v0.11.0
Published
The DRP Interval Discovery system is a crucial component of the Distributed Resource Protocol (DRP) that enables peer discovery and connection maintenance between nodes sharing the same object IDs.
Downloads
3
Readme
DRP Interval Discovery
The DRP Interval Discovery system is a crucial component of the Distributed Resource Protocol (DRP) that enables peer discovery and connection maintenance between nodes sharing the same object IDs.
Overview
The interval discovery mechanism ensures that nodes with shared object IDs can:
- Discover each other on the network
- Maintain connections with peers
- Handle peer discovery and connection establishment gracefully
Architecture
The interval discovery system consists of three main components:
DRPIntervalDiscovery: Manages the discovery process for a specific object IDIntervalRunner: Handles the periodic execution of discovery tasksNetworkNode: Handles P2P communication between nodes
Sequence Diagram
The following sequence diagram illustrates the discovery process:
sequenceDiagram
box Node A
participant Node_A_DRPIntervalDiscovery
participant Node_A_NetworkNode
end
box Node B
participant Node_B_NetworkNode
participant Node_B_DRPIntervalDiscovery
end
Note over Node_A_DRPIntervalDiscovery: start() called
loop Every interval
Node_A_DRPIntervalDiscovery->>Node_A_NetworkNode: getGroupPeers(objectId)
alt No peers found for objectId
Note over Node_A_DRPIntervalDiscovery: Start search timer
Node_A_DRPIntervalDiscovery->>Node_A_NetworkNode: broadcastMessage(DRP_INTERVAL_DISCOVERY_TOPIC)
Node_A_NetworkNode-->>Node_B_NetworkNode: DRPDiscoveryRequest message
alt Node B has matching objectId
Note over Node_B_DRPIntervalDiscovery: handleDiscoveryRequest
Node_B_DRPIntervalDiscovery->>Node_B_NetworkNode: getGroupPeers(objectId)
Node_B_DRPIntervalDiscovery->>Node_B_NetworkNode: sendMessage(DRPDiscoveryResponse)
Node_B_NetworkNode-->>Node_A_NetworkNode: DRPDiscoveryResponse
Note over Node_A_NetworkNode: Contains peer multiaddrs
Node_A_NetworkNode-->>Node_A_DRPIntervalDiscovery: handleDiscoveryResponse
Node_A_DRPIntervalDiscovery->>Node_A_NetworkNode: connect(peer_multiaddrs)
end
else Peers found
Note over Node_A_DRPIntervalDiscovery: Reset search timer
Note over Node_A_DRPIntervalDiscovery: Skip discovery
end
end
Note over Node_A_DRPIntervalDiscovery: stop() called
Note over Node_A_DRPIntervalDiscovery: Clear interval timerConfiguration
The interval discovery system can be configured with the following options:
interface DRPIntervalDiscoveryOptions {
/** Unique identifier for the object */
readonly id: string;
/** Network node instance used for peer communication */
readonly networkNode: DRPNetworkNode;
/** Interval in milliseconds between discovery attempts. Defaults to 10,000ms */
readonly interval?: number;
/** Logger configuration options */
readonly logConfig?: LoggerOptions;
/** Duration in milliseconds to search for peers before giving up. Defaults to 5 minutes */
readonly searchDuration?: number;
}Key Features
Periodic Discovery Checks
- Default interval: 10 seconds
- Configurable through
intervaloption
Peer Discovery
- Broadcasts discovery messages when no peers are found
- Uses
DRP_INTERVAL_DISCOVERY_TOPICfor discovery messages - Includes object ID for targeted peer matching
Connection Management
- Automatically connects to discovered peers
- Maintains connections with active peers
- Handles peer multiaddress information
Search Duration
- Default duration: 5 minutes
- Configurable through
searchDurationoption - Prevents indefinite searching for unavailable peers
Usage
import { createDRPDiscovery } from "@ts-drp/interval-discovery";
// Create a new discovery instance
const discovery = createDRPDiscovery({
id: "unique-object-id",
networkNode: networkNode,
interval: 10000, // 10 seconds
searchDuration: 300000, // 5 minutes
});
// Start the discovery process
discovery.start();
// Stop the discovery process when done
discovery.stop();Message Types
The discovery system uses two main message types:
DRPDiscoveryRequest
- Broadcast to discover peers
- Contains the object ID being searched for
DRPDiscoveryResponse
- Sent in response to discovery messages
- Contains peer multiaddresses for connection establishment
- Includes subscriber information for all peers sharing the object ID
Error Handling
The discovery system includes:
- Logging of failed peer discoveries and connections
- Automatic retry mechanism within search duration
