macula_content_dht (macula v0.20.5)
View SourceDHT integration for Macula content-addressed storage.
Handles content announcement and discovery through the DHT: - Announce manifest availability to mesh peers - Discover providers of content by MCID - TTL-based re-announcement for liveness
DHT Key Structure
DHT keys are SHA-256 hashes of the MCID. This ensures: - Uniform distribution in the DHT keyspace - Same content always maps to same key - 32-byte keys match DHT node ID size
Provider Discovery
When content is published, the provider announces to the DHT:
Key: hash(MCID)
Value: #{node_id, endpoint, metadata, advertised_at, ttl}Multiple providers can announce the same content.
Example Usage
%% Announce content availability
{Key, Value} = macula_content_dht:create_announcement(MCID, NodeId, Endpoint, Info),
macula_routing_server:store(RoutingPid, Key, Value).
%% Discover providers
case macula_routing_server:find_value(RoutingPid, Key, 20) of
{ok, Providers} -> handle_providers(Providers);
{nodes, _} -> {error, not_found}
end.
Summary
Functions
Announce manifest availability to DHT. Stores provider info at DHT key derived from MCID.
Announce manifest with options. Options: - ttl: Custom TTL (default: 300 seconds)
Create DHT announcement for content availability. Returns {DHTKey, ProviderValue} tuple ready for storage.
Create provider info map with all required fields.
Create removal marker for DHT. When a provider stops hosting content, they announce removal.
Get default TTL for content announcements.
Generate DHT key from MCID. Uses SHA-256 to hash the MCID, ensuring uniform distribution in the DHT keyspace.
Format raw provider values into standardized list. Handles both single provider and list of providers.
Get TTL from options or use default.
Locate providers of content by MCID. Queries DHT and returns list of provider endpoints.
Locate providers with options. Options: - k: Number of closest nodes to query (default: 20) - timeout: Query timeout in milliseconds
Calculate interval for re-announcement. Re-announces slightly before TTL expires to maintain presence. Returns interval in seconds.
Remove manifest announcement from DHT.
Types
-type mcid() :: <<_:272>>.
34 bytes: version + codec + hash
-type provider_info() :: #{node_id := binary(), endpoint := binary(), metadata := map(), advertised_at := integer(), ttl => pos_integer()}.
Functions
Announce manifest availability to DHT. Stores provider info at DHT key derived from MCID.
Announce manifest with options. Options: - ttl: Custom TTL (default: 300 seconds)
Create DHT announcement for content availability. Returns {DHTKey, ProviderValue} tuple ready for storage.
-spec create_provider_info(binary(), binary(), map()) -> provider_info().
Create provider info map with all required fields.
Create removal marker for DHT. When a provider stops hosting content, they announce removal.
-spec default_ttl() -> pos_integer().
Get default TTL for content announcements.
Generate DHT key from MCID. Uses SHA-256 to hash the MCID, ensuring uniform distribution in the DHT keyspace.
-spec format_providers([provider_info()] | provider_info()) -> [provider_info()].
Format raw provider values into standardized list. Handles both single provider and list of providers.
-spec get_ttl(map()) -> pos_integer().
Get TTL from options or use default.
-spec locate_providers(mcid()) -> {ok, [provider_info()]} | {error, term()}.
Locate providers of content by MCID. Queries DHT and returns list of provider endpoints.
-spec locate_providers(mcid(), map()) -> {ok, [provider_info()]} | {error, term()}.
Locate providers with options. Options: - k: Number of closest nodes to query (default: 20) - timeout: Query timeout in milliseconds
-spec reannounce_interval(pos_integer()) -> pos_integer().
Calculate interval for re-announcement. Re-announces slightly before TTL expires to maintain presence. Returns interval in seconds.
Remove manifest announcement from DHT.