dht

package module
v0.8.1 Latest Latest
Warning

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

Go to latest
Published: Dec 18, 2024 License: BSD-2-Clause, GPL-2.0 Imports: 18 Imported by: 0

README

About

This is a golang implementation of the Kademlia DHT protocol. It forms a Peer-to-Peer overlay network to support building fully decentralized features for your next-gen data intensive applications.

This library is currently used in my recent projects, I found it to be pretty stable, but its APIs have not been finalized yet.

Features

  • clean kademlia implementation, nothing to do with bittorrent's Mainline DHT
  • pure Golang, no cgo
  • a simple in-memory key-value store with TTL support

Being able to defend sybil attacks is not planned, users need to ensure only participants running trusted code are allowed to join the DHT.

How to Use

See struct DHT's godoc for details, tests in dht_test.go demonstrate how to use the library.

The Experiment

This library is a little bit special in the sense that other than the selected LLM (OpenAI GPT-4o) no other reference (paper, search engine, online discussion forum or blog articles etc.) is ever consulted during the development. This experiment helped to understand how much a SOTA LLM can assist during development. My overall experience strongly indicates that SOTA LLMs like OpenAI GPT-4o is already capable of correctly presenting and explaining the full details of a public and complex network protocol like Kadelmia for the purposes of implementing the concerned protocol.

License

This library follows a dual licensing model -

  • it is licensed under the 2-clause BSD license if you have written evidence showing that you are a licensee of github.com/lni/pothos
  • otherwise, it is licensed under the GPL-2 license

Documentation

Index

Constants

View Source
const (
	RequestJoin reqType = iota
	RequestPut
	RequestGet
	RequestGetFromCached
)
View Source
const (
	// a small K is used to reflect the fact that this is not a global service
	DefaultK int = 12
	// a small number of bits is used to reflect the fact that the network size
	// is much smaller
	DefaultBits int = 128
)
View Source
const (
	RPCPing rpcType = iota
	RPCJoin
	RPCFindNode
	RPCPut
	RPCGet
)

Variables

View Source
var (
	ErrBufferTooSmall = errors.New("size of the buffer is too small")
)

Functions

This section is empty.

Types

type Config

type Config = DHTConfig

type DHT

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

DHT implements the kademlia protocol with a key-value store interface exposed for users. Asynchronous Put() and Get() methods follow the best effort approach.

Note that this DHT is intended to be used in trusted environment, e.g. managed data centers, sybil attacks are assumed to be impossible in such settings.

func NewDHT

func NewDHT(cfg Config, logger *zap.Logger) (*DHT, error)

NewDHT creates a DHT peer ready to be started.

func (*DHT) Close

func (d *DHT) Close() error

Close closes the local DHT peer.

func (*DHT) Get

func (d *DHT) Get(key Key)

Get asynchronously queries the DHT network for values associated with the specified key. Matched values will be locally cached in current peer's local store. Caller should use a follow up GetCached() call to access the returned values.

func (*DHT) GetCached

func (d *DHT) GetCached(key Key) [][]byte

GetCached returns the values associated with the specified key by only looking at current peer's local store. It is normally invoked after a successful return of Get().

func (*DHT) Put

func (d *DHT) Put(key Key, value []byte, ttl uint16)

Put asynchronously puts the specified key value pair onto the DHT network with its TTL value set to ttl seconds.

func (*DHT) ScheduleGet

func (d *DHT) ScheduleGet(delay time.Duration, key Key)

ScheduleGet schedules a Get() to be performed after the specified delay.

func (*DHT) Start

func (d *DHT) Start() error

Start starts the local DHT peer.

type DHTConfig

type DHTConfig struct {
	// Secret of the DHT deployment. Note that this is not a security setting,
	// it is only used to prevent dht nodes from different deployments talking to
	// each other as a result of misconfiguration.
	Secret  uint16
	Proto   string
	Routers []net.UDPAddr
	Address net.UDPAddr
}

DHTConfig is the config for the DHT.

type Key

type Key = key

Jump to

Keyboard shortcuts

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