Help us shape the future of Go! Take the 2026 Go Developer Survey

shared

package
v0.1.0-alpha.3 Latest Latest
Warning

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

Go to latest
Published: Oct 25, 2026 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Overview

Package shared is the state several `flowi serve` instances can share through a Valkey (or Redis) server: which schedule tick has been taken and how a run ended, so that GET /runs/<id> works on any instance.

Everything in it is optional and fails open. The Valkey client is a small RESP client over the standard library; when the server cannot be reached it reports ErrUnavailable at once for a quiet period instead of waiting on every call, so a down cache never slows a run or a tick.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrUnavailable means the shared server could not be reached. It is
	// returned at once during the quiet period that follows a failure.
	ErrUnavailable = errors.New("the shared store is unavailable")
	// ErrTooLarge is returned for a value above maxValue.
	ErrTooLarge = errors.New("the value is too large for the shared store")
)

Errors a Store can return.

Functions

This section is empty.

Types

type Memory

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

Memory is a Store in this process's memory: for tests, and for running the same code paths without a server.

func NewMemory

func NewMemory(now func() time.Time) *Memory

NewMemory returns an empty in-memory store; now defaults to time.Now.

func (*Memory) Claim

func (m *Memory) Claim(_ context.Context, key string, ttl time.Duration) (bool, error)

Claim implements Store.

func (*Memory) Close

func (m *Memory) Close() error

Close implements Store.

func (*Memory) Get

func (m *Memory) Get(_ context.Context, key string) ([]byte, bool, error)

Get implements Store.

func (*Memory) Put

func (m *Memory) Put(_ context.Context, key string, value []byte, ttl time.Duration) error

Put implements Store.

type Records

type Records struct{ Store Store }

Records publishes run records in a Store; it is a runs.SharedRecords.

func (Records) LoadRecord

func (r Records) LoadRecord(ctx context.Context, id string) (runs.Record, bool, error)

LoadRecord implements runs.SharedRecords.

func (Records) SaveRecord

func (r Records) SaveRecord(ctx context.Context, rec runs.Record) error

SaveRecord implements runs.SharedRecords.

type Store

type Store interface {
	// Claim reports whether the caller is the first to claim key; the claim
	// lapses after ttl. It is how one instance out of several takes a
	// schedule tick.
	Claim(ctx context.Context, key string, ttl time.Duration) (bool, error)
	// Put stores value under key for ttl.
	Put(ctx context.Context, key string, value []byte, ttl time.Duration) error
	// Get returns the value under key; ok is false when there is none.
	Get(ctx context.Context, key string) (value []byte, ok bool, err error)
	// Close releases the store's connection.
	Close() error
}

Store is the shared state of serve instances. Implementations are safe for concurrent use.

type Valkey

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

Valkey is a Store on a Valkey or Redis server, over RESP2.

It keeps one connection, opened on first use and reopened once if a call finds it closed. After a failure that a fresh connection does not cure it returns ErrUnavailable for quietPeriod without dialing.

func NewValkey

func NewValkey(cfg ValkeyConfig) (*Valkey, error)

NewValkey checks the configuration. It does not connect: a server that is down at start must not stop serve from starting.

func (*Valkey) Claim

func (v *Valkey) Claim(ctx context.Context, key string, ttl time.Duration) (bool, error)

Claim implements Store with SET key 1 NX PX ttl.

func (*Valkey) Close

func (v *Valkey) Close() error

Close drops the connection.

func (*Valkey) Get

func (v *Valkey) Get(ctx context.Context, key string) ([]byte, bool, error)

Get implements Store with GET.

func (*Valkey) Put

func (v *Valkey) Put(ctx context.Context, key string, value []byte, ttl time.Duration) error

Put implements Store with SET key value PX ttl.

type ValkeyConfig

type ValkeyConfig struct {
	Addr     string // host:port
	Password string // sent with AUTH when not empty; never logged
	TLS      bool
	Prefix   string        // put before every key, so workspaces do not meet
	Timeout  time.Duration // per connect and per command; default 2s
	Logger   *slog.Logger
	Now      func() time.Time // for the quiet period; default time.Now
}

ValkeyConfig configures a Valkey store.

Directories

Path Synopsis
Package sharedtest is a small in-process stand-in for a Valkey or Redis server, for tests of code that talks to one.
Package sharedtest is a small in-process stand-in for a Valkey or Redis server, for tests of code that talks to one.

Jump to

Keyboard shortcuts

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