Documentation ¶
Index ¶
- func ContentAddressable(data []byte) []byte
- func FromNetworkNode(n NetworkNode) *protocol.Node
- func FromNetworkNodes(ns ...NetworkNode) (out []*protocol.Node)
- func GatewayFingerprint(ip net.IP, port int) []byte
- func MustNewID() []byte
- func NewID() ([]byte, error)
- func UDPAddressFromNode(n NetworkNode) (net.Addr, error)
- func WithUDPNodeDialer(d dialer) grpc.DialOption
- type DHT
- func (dht *DHT) Bind(s *grpc.Server) error
- func (dht *DHT) Bootstrap(nodes ...NetworkNode) (err error)
- func (dht *DHT) Dial(ctx context.Context, n NetworkNode) (*grpc.ClientConn, error)
- func (dht *DHT) Disconnect() error
- func (dht *DHT) GetSelf() NetworkNode
- func (dht *DHT) GetSelfID() []byte
- func (dht *DHT) Locate(key []byte) (_none []NetworkNode, err error)
- func (dht *DHT) Nodes() []NetworkNode
- func (dht *DHT) NumNodes() int
- type MemoryStore
- func (ms *MemoryStore) Delete(key []byte)
- func (ms *MemoryStore) ExpireKeys()
- func (ms *MemoryStore) GetAllKeysForReplication() [][]byte
- func (ms *MemoryStore) Retrieve(key []byte) (data []byte, found bool)
- func (ms *MemoryStore) Store(key []byte, data []byte, replication time.Time, expiration time.Time, ...) error
- type Message
- type MessageOption
- type NetworkNode
- type Option
- type Socket
- func (t Socket) Accept() (net.Conn, error)
- func (t Socket) Addr() net.Addr
- func (t Socket) Close() error
- func (t Socket) Dial(ctx context.Context, addr net.Addr) (conn net.Conn, err error)
- func (t Socket) LocalAddr() net.Addr
- func (t Socket) LocalNode() NetworkNode
- func (t Socket) Merge(options ...SocketOption) Socket
- func (t Socket) NewNode() NetworkNode
- type SocketOption
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ContentAddressable ¶
ContentAddressable returns the key of the provided data. uses sha1.
func FromNetworkNode ¶
func FromNetworkNode(n NetworkNode) *protocol.Node
FromNetworkNode converts internal node structure to protocol node structure.
func FromNetworkNodes ¶
func FromNetworkNodes(ns ...NetworkNode) (out []*protocol.Node)
FromNetworkNodes convience function for converting multiple NetworkNode instances.
func GatewayFingerprint ¶
GatewayFingerprint generate a fingerprint a IP/port combination.
func UDPAddressFromNode ¶
func UDPAddressFromNode(n NetworkNode) (net.Addr, error)
UDPAddressFromNode convert a NetworkNode into its corresponding udp address.
func WithUDPNodeDialer ¶
func WithUDPNodeDialer(d dialer) grpc.DialOption
WithUDPNodeDialer creates a DialOption from a dialer
Types ¶
type DHT ¶
type DHT struct { // Seconds after which an otherwise unaccessed bucket must be refreshed, // also used to reap dead nodes from the DHT. if a node hasnt been seen in 4x // the refresh period then its dead. TRefresh time.Duration // The maximum time to wait for a response from a node before discarding // it from the bucket TPingMax time.Duration // The maximum time to wait to locate a key before timing out. TLocateTimeout time.Duration // contains filtered or unexported fields }
DHT represents the state of the local node in the distributed hash table
func (*DHT) Bootstrap ¶
func (dht *DHT) Bootstrap(nodes ...NetworkNode) (err error)
Bootstrap attempts to bootstrap the network using the BootstrapNodes provided to the Options struct. This will trigger an iterativeFindNode to the provided BootstrapNodes.
func (*DHT) Dial ¶
func (dht *DHT) Dial(ctx context.Context, n NetworkNode) (*grpc.ClientConn, error)
Dial a peer in the DHT.
func (*DHT) Disconnect ¶
Disconnect will trigger a disconnect from the network. All underlying sockets will be closed.
func (*DHT) GetSelf ¶
func (dht *DHT) GetSelf() NetworkNode
GetSelf returns the node information of the local node.
func (*DHT) Locate ¶
func (dht *DHT) Locate(key []byte) (_none []NetworkNode, err error)
Locate does an iterative search through the network based on key. used to locate the nodes to interact with for a given key.
func (*DHT) Nodes ¶
func (dht *DHT) Nodes() []NetworkNode
Nodes returns the nodes themselves sotred in the routing table.
type MemoryStore ¶
type MemoryStore struct {
// contains filtered or unexported fields
}
MemoryStore is a simple in-memory key/value store used for unit testing, and the CLI example
func NewMemoryStore ¶
func NewMemoryStore(dht *DHT) *MemoryStore
NewMemoryStore create a properly initialized memory store.
func (*MemoryStore) Delete ¶
func (ms *MemoryStore) Delete(key []byte)
Delete deletes a key/value pair from the MemoryStore
func (*MemoryStore) ExpireKeys ¶
func (ms *MemoryStore) ExpireKeys()
ExpireKeys should expire all key/values due for expiration.
func (*MemoryStore) GetAllKeysForReplication ¶
func (ms *MemoryStore) GetAllKeysForReplication() [][]byte
GetAllKeysForReplication should return the keys of all data to be replicated across the network. Typically all data should be replicated every tReplicate seconds.
type Message ¶
type Message struct { Sender *NetworkNode Receiver *NetworkNode ID int64 Type int IsResponse bool Data interface{} }
Message sent and received between nodes.
type MessageOption ¶
type MessageOption func(*Message)
MessageOption ...
func MessageOptionSender ¶
func MessageOptionSender(n *NetworkNode) MessageOption
MessageOptionSender ...
type NetworkNode ¶
type NetworkNode struct { // ID is a 20 byte unique identifier ID []byte // IP is the public address of the node IP net.IP // Port is the public port of the node Port int // LastSeen when was this node last considered seen by the DHT LastSeen time.Time }
NetworkNode is the over-the-wire representation of a node
type Option ¶
type Option func(*DHT)
Option for a distributed hash table.
func OptionNodeID ¶
OptionNodeID set node id, overriding the fingerprint. only should be used in tests.
func OptionNodeIDChecksum ¶
func OptionNodeIDChecksum(c nodeChecksum) Option
OptionNodeIDChecksum set the function that forces node IDs to conform to a checksum. prevents nodes from having multiple IDs for a single address/port.
func OptionTimeout ¶
OptionTimeout - timeout to wait beforing timing out when locating a key.
type Socket ¶
Socket network connection with public IP information.
func NewSocket ¶
func NewSocket(addr string, options ...SocketOption) (s Socket, err error)
NewSocket public ip of the socket.
func (Socket) Close ¶
Close closes the listener. Any blocked Accept operations will be unblocked and return errors.
func (Socket) Merge ¶
func (t Socket) Merge(options ...SocketOption) Socket
Merge options into the socket.
func (Socket) NewNode ¶
func (t Socket) NewNode() NetworkNode
NewNode create a node from the current socket and the given id.
type SocketOption ¶
type SocketOption func(*Socket)
SocketOption option for the utp socket.
func SocketOptionGateway ¶
func SocketOptionGateway(gateway net.IP) SocketOption
SocketOptionGateway public IP for the socket.
func SocketOptionPort ¶
func SocketOptionPort(port int) SocketOption
SocketOptionPort public for for the socket.