peerlog

package
v0.48.0 Latest Latest
Warning

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

Go to latest
Published: Sep 6, 2026 License: MIT Imports: 7 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package peerlog is what the hub remembers about a peer between attaches: that it exists, when it was first seen, when it last attached, and why it last went away. The directory answers "who is attached right now" and forgets a peer the moment it detaches, so without this a hub with nothing connected cannot say whether it ever had a peer, which names are taken, or when the last one dropped.

Storage is a pluggable Store (SQLite or PostgreSQL, the same engines the denylist and the directory use); a shared store makes the log fleet-wide. Every write is best-effort: a peer log that cannot be written must never fail an attach.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Dialect

type Dialect int

Dialect selects SQL syntax that differs between engines (parameter placeholders, mainly).

const (
	// SQLite uses ? placeholders.
	SQLite Dialect = iota
	// Postgres uses $1, $2, … placeholders.
	Postgres
)

type Log

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

Log is the peer log over a pluggable Store, with the best-effort semantics the hub needs on the attach path: writes are fire and forget, and a store error is the caller's to log, never to fail on.

func New

func New(st Store) *Log

New returns a Log over st.

func (*Log) Attached

func (l *Log) Attached(peer, version string) error

Attached records that peer attached, running version.

func (*Log) Detached

func (l *Log) Detached(peer, reason string) error

Detached records that peer went away, and why.

func (*Log) Enrolled

func (l *Log) Enrolled(peer string) error

Enrolled records a minted token for peer.

func (*Log) Forget

func (l *Log) Forget(ctx context.Context, peer string) error

Forget drops peer's record.

func (*Log) List

func (l *Log) List(ctx context.Context) ([]Record, error)

List returns every remembered peer, ordered by peer id.

type MemoryStore

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

MemoryStore is an in-process Store: correct for a single hub, and what a library consumer gets without a database. Safe for concurrent use. It is not durable, so a hub restart forgets everything, which is the whole problem the SQL stores exist to solve.

func NewMemoryStore

func NewMemoryStore() *MemoryStore

NewMemoryStore returns an empty in-memory peer log store.

func (*MemoryStore) Attached

func (m *MemoryStore) Attached(_ context.Context, peer, version string, atUnix int64) error

func (*MemoryStore) Detached

func (m *MemoryStore) Detached(_ context.Context, peer, reason string, atUnix int64) error

func (*MemoryStore) Enrolled

func (m *MemoryStore) Enrolled(_ context.Context, peer string, atUnix int64) error

func (*MemoryStore) Forget

func (m *MemoryStore) Forget(_ context.Context, peer string) error

func (*MemoryStore) List

func (m *MemoryStore) List(_ context.Context) ([]Record, error)

type Record

type Record struct {
	Peer string
	// PeerVersion is the version reported by the most recent attach,
	// empty for a peer that has only ever been enrolled.
	PeerVersion string
	// FirstSeenUnix is when the hub first heard of this peer, by
	// enrollment or by its first attach.
	FirstSeenUnix int64
	// LastAttachUnix is the most recent attach, 0 for a peer that has
	// never attached.
	LastAttachUnix int64
	// LastDetachUnix is the most recent detach, 0 for a peer that has
	// never detached. A peer attached right now keeps the detach time
	// of its previous session, so compare the two rather than reading
	// either alone: use the Registry for liveness.
	LastDetachUnix int64
	// LastReason is why the last detach happened ("superseded",
	// "connection-lost", an operator kill).
	LastReason string
}

Record is one peer the hub remembers.

type SQLOption

type SQLOption func(*SQLStore)

SQLOption configures a SQLStore.

func WithTable

func WithTable(name string) SQLOption

WithTable overrides the table name (default "known_peers").

type SQLStore

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

SQLStore is the SQL-backed Store. It imports only database/sql: the consumer brings the driver and hands in an opened *sql.DB.

func NewSQL

func NewSQL(db *sql.DB, dialect Dialect, opts ...SQLOption) *SQLStore

NewSQL returns a SQL peer log store over db. Call Migrate once to create the table.

func (*SQLStore) Attached

func (s *SQLStore) Attached(ctx context.Context, peer, version string, atUnix int64) error

Attached records an attach and the version the peer reported.

func (*SQLStore) Detached

func (s *SQLStore) Detached(ctx context.Context, peer, reason string, atUnix int64) error

Detached records a detach and why it happened.

func (*SQLStore) Enrolled

func (s *SQLStore) Enrolled(ctx context.Context, peer string, atUnix int64) error

Enrolled records a minted token, creating the record on first sight and leaving any attach history alone.

func (*SQLStore) Forget

func (s *SQLStore) Forget(ctx context.Context, peer string) error

Forget drops a peer's record.

func (*SQLStore) List

func (s *SQLStore) List(ctx context.Context) ([]Record, error)

List returns every remembered peer, ordered by peer id.

func (*SQLStore) Migrate

func (s *SQLStore) Migrate(ctx context.Context) error

Migrate creates the peer log table if it does not exist. Idempotent.

type Store

type Store interface {
	// Enrolled records that a token was minted for peer, creating the
	// record if this is the first the hub hears of it. It never
	// overwrites attach history: enrolling an existing peer again is a
	// no-op beyond keeping the record alive.
	Enrolled(ctx context.Context, peer string, atUnix int64) error
	// Attached records an attach, with the version the peer reported.
	Attached(ctx context.Context, peer, version string, atUnix int64) error
	// Detached records a detach and why it happened.
	Detached(ctx context.Context, peer, reason string, atUnix int64) error
	// List returns every remembered peer, ordered by peer id.
	List(ctx context.Context) ([]Record, error)
	// Forget drops a peer's record. The peer comes back the next time
	// it is enrolled or attaches.
	Forget(ctx context.Context, peer string) error
}

Store persists the peer log. NewSQL provides the SQL implementation for both supported dialects.

Directories

Path Synopsis
Package postgres is the PostgreSQL flavour of the SQL-backed peer log, so a fleet of hubs remembers one set of peers.
Package postgres is the PostgreSQL flavour of the SQL-backed peer log, so a fleet of hubs remembers one set of peers.
Package sqlite is the SQLite flavour of the SQL-backed peer log.
Package sqlite is the SQLite flavour of the SQL-backed peer log.

Jump to

Keyboard shortcuts

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