Documentation ¶
Index ¶
- Constants
- type DHT
- func (dht *DHT) AddNode(node *Node)
- func (dht *DHT) FindNode(key []byte) (value []byte, found bool, err error)
- func (dht *DHT) Get(key []byte) (value []byte, senderID []byte, found bool, err error)
- func (dht *DHT) GetClosestContacts(count int, target []byte, filterFunc NodeFilterFunc, ignoredNodes ...[]byte) []*Node
- func (dht *DHT) GetSelfID() []byte
- func (dht *DHT) IsNodeCloser(node1, node2 []byte) bool
- func (dht *DHT) MarkNodeAsSeen(ID []byte)
- func (dht *DHT) NewInformationRequest(Action int, Key []byte, Nodes []*Node) (ir *InformationRequest)
- func (dht *DHT) Nodes() []*Node
- func (dht *DHT) NumNodes() int
- func (dht *DHT) RefreshBuckets(target int)
- func (dht *DHT) RemoveNode(ID []byte)
- func (dht *DHT) Store(key []byte, dataSize uint64) (err error)
- type InformationRequest
- type Node
- type NodeFilterFunc
- type NodeMessage
Constants ¶
const ( ActionFindNode = iota // Find a node ActionFindValue // Find a value )
Actions for performing the information request
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type DHT ¶
type DHT struct { // ShouldEvict determines whether node 1 shall be evicted in favor of node 2 ShouldEvict func(node1, node2 *Node) bool // SendRequestStore sends an announcement-store message to the remote node. It informs the remote node that the local one stores the given key-value. SendRequestStore func(node *Node, key []byte, dataSize uint64) // SendRequestFindNode sends an information request to find a particular node. nodes are the nodes to send the request to. SendRequestFindNode func(request *InformationRequest) // SendRequestFindValue sends an information request to find data. nodes are the nodes to send the request to. SendRequestFindValue func(request *InformationRequest) // The maximum time to wait for a response to any message in Store, Get, FindNode TMsgTimeout time.Duration // contains filtered or unexported fields }
DHT represents the state of the local node in the distributed hash table
func (*DHT) AddNode ¶
AddNode adds a node into the appropriate k bucket. These buckets are stored in big-endian order so we look at the bits from right to left in order to find the appropriate bucket.
func (*DHT) GetClosestContacts ¶
func (dht *DHT) GetClosestContacts(count int, target []byte, filterFunc NodeFilterFunc, ignoredNodes ...[]byte) []*Node
GetClosestContacts returns the closes contacts in the hash table
func (*DHT) IsNodeCloser ¶
IsNodeCloser compares 2 nodes to self. If true, the first node is closer (= smaller distance) to self than the second.
func (*DHT) MarkNodeAsSeen ¶
MarkNodeAsSeen marks a node as seen, which pushes it to the top in the bucket list.
func (*DHT) NewInformationRequest ¶
func (dht *DHT) NewInformationRequest(Action int, Key []byte, Nodes []*Node) (ir *InformationRequest)
NewInformationRequest creates a new information request and adds it to the list. It marks the count of nodes as active, meaning the caller should later decrease it via ActiveNodesSub.
func (*DHT) RefreshBuckets ¶
RefreshBuckets refreshes all buckets not meeting the target node number. 0 to refresh all.
type InformationRequest ¶
type InformationRequest struct { Action int // ActionX Key []byte // Key that is being queried ResultChan chan *NodeMessage // Result channel ActiveNodes uint64 // Number of nodes actively handling the request. Nodes []*Node // Nodes that are receiving the request. IsTerminated bool // If true, it was signaled for termination TerminateSignal chan struct{} // gets closed on termination signal, can be used in select via "case _ = <- network.terminateSignal:" sync.Mutex // for sychronized closing }
InformationRequest is an asynchronous request sent to nodes. It tracks any asynchronous replies and handles timeouts.
func (*InformationRequest) CollectResults ¶
func (ir *InformationRequest) CollectResults(timeout time.Duration) (results []*NodeMessage)
CollectResults collects all information request responses within the given timeout.
func (*InformationRequest) Done ¶
func (ir *InformationRequest) Done()
Done is called when a remote node is done.
func (*InformationRequest) QueueResult ¶
func (ir *InformationRequest) QueueResult(message *NodeMessage)
QueueResult accepts incoming results
func (*InformationRequest) Terminate ¶
func (ir *InformationRequest) Terminate()
Terminate sends the termination signal to all workers. It is safe to call Terminate multiple times.
type Node ¶
type Node struct { // ID is the unique identifier ID []byte // LastSeen when was this node last considered seen by the DHT LastSeen time.Time // Info is an arbitrary pointer specified by the caller Info interface{} }
Node is the over-the-wire representation of a node
type NodeFilterFunc ¶
NodeFilterFunc is called to filter nodes based on the callers choice
type NodeMessage ¶
type NodeMessage struct { SenderID []byte // Sender of the message Data []byte // FIND_VALUE: Actual data Closest []*Node // FIND_VALUE, FIND_NODE: Closest nodes to the requested key Storing []*Node // FIND_VALUE: Nodes known to store the value Error error // To be removed }
NodeMessage is a message sent by a node