macula_routing_bucket (macula v0.25.1)
View SourceK-bucket for Kademlia routing table. Stores up to k nodes with LRU eviction policy.
Summary
Functions
Add a node to the bucket. If node exists (same node_id), move to tail (most recent). If endpoint exists with different node_id, replace the stale entry. If bucket full, replace oldest stale entry or return {error, bucket_full}.
Get bucket capacity.
Remove nodes not seen since StaleThreshold (millisecond timestamp).
Find n closest nodes to target (sorted by XOR distance).
Find a node by ID.
Extract endpoint from node info (handles multiple key formats).
Get all nodes in the bucket (ordered: oldest first).
Check if bucket contains node.
Create a new bucket with capacity k.
Remove all nodes matching an endpoint (regardless of node_id).
Remove entries with matching endpoint but different node_id (ghost cleanup).
Remove a node from the bucket.
Get number of nodes in bucket.
Update node's last_seen timestamp (moves to tail).
Types
-type bucket() :: #{capacity := pos_integer(), nodes := [node_info()]}.
-type node_info() :: #{node_id := binary(), address := {inet:ip_address(), inet:port_number()}, last_seen => integer()}.
Functions
Add a node to the bucket. If node exists (same node_id), move to tail (most recent). If endpoint exists with different node_id, replace the stale entry. If bucket full, replace oldest stale entry or return {error, bucket_full}.
-spec capacity(bucket()) -> pos_integer().
Get bucket capacity.
Remove nodes not seen since StaleThreshold (millisecond timestamp).
-spec find_closest(bucket(), binary(), pos_integer()) -> [node_info()].
Find n closest nodes to target (sorted by XOR distance).
Find a node by ID.
Extract endpoint from node info (handles multiple key formats).
Get all nodes in the bucket (ordered: oldest first).
Check if bucket contains node.
-spec new(pos_integer()) -> bucket().
Create a new bucket with capacity k.
Remove all nodes matching an endpoint (regardless of node_id).
Remove entries with matching endpoint but different node_id (ghost cleanup).
Remove a node from the bucket.
-spec size(bucket()) -> non_neg_integer().
Get number of nodes in bucket.
Update node's last_seen timestamp (moves to tail).