distribute/discovery/adapter

Discovery Adapter contract and utilities.

This module defines the behaviour contract that all discovery adapters must implement, along with helper functions for creating default options.

Adapter Contract

A discovery adapter handles peer discovery and membership tracking. It provides:

Implementing an Adapter

To implement a custom adapter, create a module that provides a function returning a DiscoveryAdapter record with all required functions:

pub fn new() -> DiscoveryAdapter {
  DiscoveryAdapter(
    start: my_start,
    stop: my_stop,
    subscribe: my_subscribe,
    unsubscribe: my_unsubscribe,
    snapshot: my_snapshot,
    lookup: my_lookup,
    health: my_health,
  )
}

See distribute/discovery/beam_adapter for a reference implementation.

Types

Discovery adapter behaviour contract.

All discovery implementations must provide these 7 core functions.

  1. start - Initialize and start the adapter
  2. stop - Graceful shutdown with timeout
  3. subscribe - Register callback for membership events
  4. unsubscribe - Remove a subscription
  5. snapshot - Get current membership state
  6. lookup - Get metadata for a specific peer
  7. health - Get current adapter health status
pub type DiscoveryAdapter {
  DiscoveryAdapter(
    start: fn(types.AdapterOptions) -> Result(
      types.AdapterHandle,
      types.DiscoveryError,
    ),
    stop: fn(types.AdapterHandle, Int) -> Result(
      Nil,
      types.DiscoveryError,
    ),
    subscribe: fn(
      types.AdapterHandle,
      fn(types.DiscoveryEvent) -> Nil,
    ) -> Result(types.SubscriptionId, types.DiscoveryError),
    unsubscribe: fn(types.AdapterHandle, types.SubscriptionId) -> Result(
      Nil,
      types.DiscoveryError,
    ),
    snapshot: fn(types.AdapterHandle) -> Result(
      List(types.PeerInfo),
      types.DiscoveryError,
    ),
    lookup: fn(types.AdapterHandle, String) -> Result(
      types.PeerInfo,
      types.DiscoveryError,
    ),
    health: fn(types.AdapterHandle) -> types.HealthStatus,
    metrics: fn(types.AdapterHandle) -> types.DiscoveryMetrics,
  )
}

Constructors

Values

pub fn default_options(name: String) -> types.AdapterOptions

Create default adapter options.

Provides sensible defaults:

  • sync_interval_ms: 5000 (5 seconds)
  • sync_timeout_ms: 10000 (10 seconds)
  • telemetry_prefix: “discovery”
  • retry_config: 3 attempts with exponential backoff
Search Document