dht

package module
v0.0.0-...-59a18d3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 26, 2016 License: MIT Imports: 7 Imported by: 0

README

dht

dht is a distributed hash table implementation that uses Consul and rendezvous hashing to distribute keys among distributed nodes. Because it uses rendezvous hashing to determine key placement, removing a node from the hash table is minimally disruptive in terms of key re-assignment.

Local hash table state is refreshed in a background goroutine using blocking Consul API queries with the default consistency mode. Errors encountered in the background goroutine are logged using the log package in the following format:

[dht <name> <id>] error: <error message>

dht requires a locally-running Consul agent (version 0.5.2 or newer) with its HTTP API listening on 127.0.0.1:8500. dht nodes run a simple HTTP server on an available ephemeral port to allow Consul to periodically check that the node is still alive.

Example

node1, err := dht.Join("worker", "worker-1")
node2, err := dht.Join("worker", "worker-2")

node1.Member("some_key") // true
node2.Member("some_key") // false

err = node1.Leave()
err = node2.Leave()

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Node

type Node struct {
	// contains filtered or unexported fields
}

Node is a single node in a distributed hash table, coordinated using services registered in Consul. Key membership is determined using rendezvous hashing to ensure even distribution of keys and minimal key membership changes when a Node fails or otherwise leaves the hash table.

Errors encountered when making blocking GET requests to the Consul agent API are logged using the log package.

func Join

func Join(name, id string) (node *Node, err error)

Join creates a new Node and adds it to the distributed hash table specified by the given name. The given id should be unique among all Nodes in the hash table.

func (*Node) Leave

func (n *Node) Leave() (err error)

Leave removes the Node from the distributed hash table by de-registering it from Consul. Once Leave is called, the Node should be discarded. An error is returned if the Node is unable to successfully deregister itself from Consul. In that case, Consul's health check for the Node will fail and require manual cleanup.

func (*Node) Member

func (n *Node) Member(key string) bool

Member returns true if the given key belongs to this Node in the distributed hash table.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL