Documentation
¶
Overview ¶
Package gossipkv provides an embedded, decentralized key-value store with an eventual-consistency gossip protocol.
Overview ¶
gossipkv is designed for simple, low-latency data sharing between service instances without external dependencies. Nodes exchange updates using a lightweight gossip cycle (digest + delta) over UDP.
Data model ¶
Records are versioned and merged using a last-write-wins (LWW) strategy based on UpdatedAt, Version, and NodeID. This provides eventual consistency with minimal coordination overhead.
Generics ¶
The DB type is generic over key and value types. Keys must be string or a type with underlying string. Values can be any type.
Networking ¶
Gossip is enabled when a bind address is provided. Without a bind address, the database works in local in-memory mode.
Serialization ¶
Values are serialized for transport using a Codec. The default is GobCodec, while BytesCodec and StringCodec provide faster binary encoding for []byte and string values.
Example
db, err := gossipkv.New[string, string](
gossipkv.WithBindAddr("127.0.0.1:9001"),
gossipkv.WithSeeds([]string{"127.0.0.1:9002"}),
gossipkv.WithCodec(gossipkv.StringCodec{}),
)
if err != nil {
// handle error
}
_ = db.Set(context.Background(), "key", "value")
_, _ = db.Get(context.Background(), "key")
Index ¶
- Variables
- type BytesCodec
- type Codec
- type Config
- type DB
- type GobCodec
- type Option
- func WithBindAddr(addr string) Option
- func WithCodec[V any](codec Codec[V]) Option
- func WithDiscovery(enabled bool) Option
- func WithErrorHandler(handler func(error)) Option
- func WithGossipInterval(interval time.Duration) Option
- func WithNodeID(nodeID string) Option
- func WithSeeds(seeds []string) Option
- type StringCodec
Constants ¶
This section is empty.
Variables ¶
var ( // ErrNotFound indicates that the requested key is missing. ErrNotFound = errors.New("gossipkv: key not found") // ErrClosed indicates that the DB has been closed. ErrClosed = errors.New("gossipkv: db is closed") // ErrTimeout indicates that the context deadline expired. ErrTimeout = errors.New("gossipkv: operation timed out") // ErrCanceled indicates that the context was canceled. ErrCanceled = errors.New("gossipkv: operation canceled") )
Functions ¶
This section is empty.
Types ¶
type BytesCodec ¶
type BytesCodec struct{}
BytesCodec passes through raw bytes without copying. The caller must not modify the returned slice.
type Config ¶
type Config struct {
NodeID string
BindAddr string
Seeds []string
Discovery bool
GossipInterval time.Duration
// contains filtered or unexported fields
}
Config holds runtime configuration for a gossipkv node. Users typically set it via Option helpers.
type DB ¶
DB represents a running gossipkv node. It is safe for concurrent use by multiple goroutines. K must be a string or a type with underlying string.
func New ¶
New creates a new gossipkv node with the provided options. The returned instance uses an in-memory store in the current version. K must be provided explicitly because it cannot be inferred from arguments.
func (*DB[K, V]) Close ¶
Close releases resources and marks the DB as closed. Further operations will return ErrClosed. The provided context allows cancellation of the close operation.
type GobCodec ¶
type GobCodec[V any] struct{}
GobCodec uses encoding/gob for serialization. It works with most Go types without extra registration.
type Option ¶
Option configures the database on creation. Return an error to reject an invalid option value.
func WithBindAddr ¶
WithBindAddr sets the local bind address in host:port form. It is validated with net.SplitHostPort.
func WithDiscovery ¶
WithDiscovery enables or disables discovery mechanisms like mDNS.
func WithErrorHandler ¶
WithErrorHandler sets a callback for internal errors (serialization, network). It is best-effort and must be fast and non-blocking.
func WithGossipInterval ¶
WithGossipInterval sets how often the node exchanges gossip updates.
func WithNodeID ¶
WithNodeID sets a stable node identifier used in gossip metadata. If omitted, a random ID is generated.
type StringCodec ¶
type StringCodec struct{}
StringCodec encodes strings as raw bytes. It allocates on marshal/unmarshal to keep data immutable.
Directories
¶
| Path | Synopsis |
|---|---|
|
examples
|
|
|
basic
command
|
|
|
bytes_codec
command
|
|
|
custom_type
command
|
|
|
gossip_peer
command
|
|
|
string_codec
command
|
|
|
internal
|
|