Documentation
¶
Index ¶
- Constants
- Variables
- type Batch
- type Capacitor
- func (f *Capacitor) Close() error
- func (f *Capacitor) Exists(ctx context.Context, key string) (bool, error)
- func (f *Capacitor) Get(ctx context.Context, key string) (string, error)
- func (f *Capacitor) GetCount(ctx context.Context, key string) (int64, error)
- func (f *Capacitor) GetMetric(ctx context.Context, key string) (Metric, error)
- func (f *Capacitor) GetMetrics() []Summary
- func (f *Capacitor) GetScan(ctx context.Context, key string, dest any) error
- func (f *Capacitor) Increment(ctx context.Context, key string) (int64, error)
- func (f *Capacitor) IncrementBy(ctx context.Context, key string, delta int64) (int64, error)
- func (f *Capacitor) IncrementMetric(ctx context.Context, key string, delta float64) (Metric, error)
- func (f *Capacitor) IncrementMetricParallel(ctx context.Context, keys map[string]float64) (map[string]Metric, error)
- func (f *Capacitor) IncrementParallel(ctx context.Context, keys []string) (map[string]int64, error)
- func (f *Capacitor) IncrementSlidingWindow(ctx context.Context, key string, window time.Duration) (int64, error)
- func (f *Capacitor) Set(ctx context.Context, key string, value any, ttl time.Duration) error
- type Config
- type DeltaLog
- type HLC
- type Handshake
- type LogEntry
- type Logger
- type Message
- func (z *Message) DecodeMsg(dc *msgp.Reader) (err error)
- func (m *Message) Encode() ([]byte, error)
- func (z *Message) EncodeMsg(en *msgp.Writer) (err error)
- func (z *Message) MarshalMsg(b []byte) (o []byte, err error)
- func (z *Message) Msgsize() (s int)
- func (z *Message) UnmarshalMsg(bts []byte) (o []byte, err error)
- type Metric
- type MetricsTracker
- type MsgType
- type Stat
- type StreamClient
- type StreamServer
- type Summary
- type Timestamp
- func (t Timestamp) After(other Timestamp) bool
- func (z *Timestamp) DecodeMsg(dc *msgp.Reader) (err error)
- func (z Timestamp) EncodeMsg(en *msgp.Writer) (err error)
- func (t Timestamp) GreaterOrEqual(other Timestamp) bool
- func (z Timestamp) MarshalMsg(b []byte) (o []byte, err error)
- func (z Timestamp) Msgsize() (s int)
- func (t Timestamp) String() string
- func (z *Timestamp) UnmarshalMsg(bts []byte) (o []byte, err error)
Constants ¶
const DefaultMaxOffset = 500 * time.Millisecond
Variables ¶
var ErrClockSmash = errors.New("HLC clock smash detected")
Functions ¶
This section is empty.
Types ¶
type Batch ¶
type Batch struct {
FromNode string `json:"f" msg:"f"`
Entries [][]byte `json:"e" msg:"e"`
Handshake *Handshake `json:"h,omitempty" msg:"h,omitempty"`
}
func (*Batch) MarshalMsg ¶
MarshalMsg implements msgp.Marshaler
type Capacitor ¶
type Capacitor struct {
// contains filtered or unexported fields
}
Capacitor represents an active-active, local-first replicated caching node. It manages local key-value storage, cluster membership discovery, logical clocks, and replication orchestration.
func New ¶
New initializes, configures, and starts a local Capacitor node. This constructs the storage, starts the replication stream listener, and registers the node with the gossip cluster.
func (*Capacitor) Close ¶
Close gracefully shuts down the node, leaving the SWIM gossip group, shutting down active replication streams, and closing local storage engines.
func (*Capacitor) Exists ¶ added in v0.4.0
Exists checks if a key exists in the cache and has not expired.
func (*Capacitor) Get ¶
Get retrieves a key-value pair's serialized value from the local cache database. Returns an empty string and nil error if the key is not found or has expired.
func (*Capacitor) GetCount ¶
GetCount retrieves the converged aggregate sum of a distributed counter across all nodes.
func (*Capacitor) GetMetric ¶
GetMetric retrieves the aggregated Metric details (count, sum, average) for the specified key.
func (*Capacitor) GetMetrics ¶
GetMetrics returns a snapshot summary of all built-in metrics (latencies, counts).
func (*Capacitor) GetScan ¶ added in v0.3.0
GetScan retrieves a key's value and unmarshals it into the destination pointer dest (similar to json.Unmarshal or database rows.Scan).
func (*Capacitor) IncrementBy ¶
IncrementBy increments a distributed PN-Counter key by the specified delta. It tracks counts per-node to construct CRDT conflict-free convergence.
func (*Capacitor) IncrementMetric ¶
IncrementMetric records a floating-point update to a distributed aggregate metric (tracking both hit frequency and aggregated sums).
func (*Capacitor) IncrementMetricParallel ¶
func (f *Capacitor) IncrementMetricParallel(ctx context.Context, keys map[string]float64) (map[string]Metric, error)
IncrementMetricParallel updates multiple aggregate metrics concurrently.
func (*Capacitor) IncrementParallel ¶
IncrementParallel performs concurrent increment calls for multiple counter keys.
func (*Capacitor) IncrementSlidingWindow ¶
func (f *Capacitor) IncrementSlidingWindow(ctx context.Context, key string, window time.Duration) (int64, error)
IncrementSlidingWindow appends an event timestamp for rate limiting or hit tracking, and returns the count of active occurrences within the rolling window duration.
type Config ¶
type Config struct {
// NodeID is the unique identifier for this node in the cluster. If left empty,
// a hostname-based identifier will be generated automatically.
NodeID string
// BindAddr is the network address for the gossip memberlist to bind to.
BindAddr string
// BindPort is the port number utilized for gossip memberlist communications.
BindPort int
// StreamPort is the port number utilized for replication TCP streams.
StreamPort int
// AdvertiseAddr is the IP address advertised to other nodes for establishing
// replication TCP streams.
AdvertiseAddr string
// Peers is the initial list of bootstrap addresses ("IP:port") of active cluster nodes.
Peers []string
// DataPath is the local directory path where BadgerDB files are persistently stored.
DataPath string
// LogSize is the capacity (maximum number of entries) of the in-memory circular Delta Log.
LogSize uint64
// TLSConfig is the optional configuration used to secure node-to-node replication streams using mTLS.
TLSConfig *tls.Config
// AuthToken is a shared secret token used to authenticate gossip join requests and TCP streams.
AuthToken string
// Logger is the structured logging engine injected into the Capacitor instance.
Logger Logger
// DisableMetrics disables internal metrics latency tracking for maximum read/write performance.
DisableMetrics bool
}
Config defines the configuration parameters for a Capacitor node instance.
type DeltaLog ¶
type DeltaLog struct {
// contains filtered or unexported fields
}
DeltaLog is an in-memory binary circular buffer of operations.
func NewDeltaLog ¶
func (*DeltaLog) GetEntriesRaw ¶
type HLC ¶
type HLC struct {
// contains filtered or unexported fields
}
HLC (Hybrid Logical Clock) implements a causality-preserving clock.
func (*HLC) SetMaxOffset ¶
SetMaxOffset sets the maximum allowed clock drift.
type Handshake ¶
func (Handshake) MarshalMsg ¶
MarshalMsg implements msgp.Marshaler
type LogEntry ¶
type LogEntry struct {
Seq uint64 `json:"s" msg:"s"`
TS Timestamp `json:"t" msg:"t"`
BornAt int64 `json:"ba" msg:"ba"` // NEW: For end-to-end latency tracking
Op MsgType `json:"o" msg:"o"`
Key string `json:"k" msg:"k"`
NodeID string `json:"n,omitempty" msg:"n,omitempty"`
Value []byte `json:"v,omitempty" msg:"v,omitempty"`
Delta float64 `json:"d,omitempty" msg:"d,omitempty"`
TTL int64 `json:"ttl,omitempty" msg:"ttl,omitempty"`
}
LogEntry represents a single operation in the delta log.
func (*LogEntry) MarshalMsg ¶
MarshalMsg implements msgp.Marshaler
type Logger ¶
type Logger interface {
Debug(msg string, args ...any)
Info(msg string, args ...any)
Warn(msg string, args ...any)
Error(msg string, args ...any)
}
Logger is a generic structured logging interface.
type Message ¶
type Message struct {
Type MsgType `msg:"t"`
Key string `msg:"k"`
Value []byte `msg:"v,omitempty"`
ValueObj any `msg:"-"` // Used for lazy serialization
Delta float64 `msg:"d,omitempty"`
TTL int64 `msg:"ttl,omitempty"` // Seconds
Timestamp int64 `msg:"ts"` // UnixNano for LWW
NodeID string `msg:"n"` // Source node
}
Message is the container for all gossip data.
func (*Message) MarshalMsg ¶
MarshalMsg implements msgp.Marshaler
type MetricsTracker ¶
type MetricsTracker struct {
// Latencies
SetLat Stat
GetLat Stat
IncrLat Stat
ReplicateLat Stat // End-to-end (BornAt to Apply)
// Memory (Peak)
PeakAlloc uint64
PeakHeapAlloc uint64
PeakSys uint64
// contains filtered or unexported fields
}
func NewMetricsTracker ¶
func NewMetricsTracker() *MetricsTracker
func (*MetricsTracker) GetSummary ¶
func (m *MetricsTracker) GetSummary() []Summary
func (*MetricsTracker) Stop ¶
func (m *MetricsTracker) Stop()
type MsgType ¶
type MsgType byte
MsgType defines the type of gossip message.
func (MsgType) MarshalMsg ¶
MarshalMsg implements msgp.Marshaler
type StreamClient ¶
type StreamClient struct {
// contains filtered or unexported fields
}
func NewStreamClient ¶
func NewStreamClient(tlsConfig *tls.Config, authToken string) *StreamClient
func (*StreamClient) Close ¶
func (c *StreamClient) Close()
func (*StreamClient) CloseConn ¶
func (c *StreamClient) CloseConn(nodeID string)
type StreamServer ¶
type StreamServer struct {
// contains filtered or unexported fields
}
func NewStreamServer ¶
func (*StreamServer) Start ¶
func (s *StreamServer) Start()
func (*StreamServer) Stop ¶
func (s *StreamServer) Stop()
type Timestamp ¶
func (Timestamp) GreaterOrEqual ¶
GreaterOrEqual returns true if this timestamp is logically greater than or equal to the other timestamp.
func (Timestamp) MarshalMsg ¶
MarshalMsg implements msgp.Marshaler