sessions

package
v0.6.5 Latest Latest
Warning

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

Go to latest
Published: Feb 26, 2024 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInSession              = errors.New("cannot begin session, already in a session")
	ErrNotInSession           = errors.New("cannot commit, not in a session")
	ErrIdempotencyKeyMismatch = errors.New("idempotency key mismatch")
)

Functions

This section is empty.

Types

type Committable

type Committable interface {
	// Begin signals that a session is starting, and that the datastores
	// should begin allowing writes.
	// Commit should be called to signal that the session is complete.
	Begin(ctx context.Context, idempotencyKey []byte) error
	// BeginRecovery signals that the server is recovering from a crash.
	// It should be called instead of Begin, and Commit should be called
	// to signal that the recovery is complete.
	BeginRecovery(ctx context.Context, idempotencyKey []byte) error
	// Commit signals that the session is complete.
	// It returns a unique identifier for the session that
	// is generated deterministically from the applied changes.
	Commit(ctx context.Context, idempotencyKey []byte) ([]byte, error)
	// Cancel signals that the session is cancelled.
	// If a session is cancelled, it will be reset to the state that
	// it was in before Begin was called.
	Cancel(ctx context.Context) error
}

Committable is a datastore that can be included in a commit session.

type CommitterOpt

type CommitterOpt func(*MultiCommitter)

func WithLogger

func WithLogger(logger log.Logger) CommitterOpt

WithLogger sets the logger to use for the committer.

type KV

type KV interface {
	Get(key []byte) ([]byte, error)
	Set(key []byte, value []byte) error
	Delete(key []byte) error
}

type MultiCommitter

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

MultiCommitter combines multiple committables into one. It does not implement Committable itself, but can be used to begin and commit multiple committables at once. It will persist the idempotency key in the KV store before beginning the session, and will delete it after the session. If it comes online and still has a session in progress, it will check to ensure the idempotency keys match, and if so, will begin recovery.

func NewCommitter

func NewCommitter(kv KV, committables map[string]Committable, opts ...CommitterOpt) *MultiCommitter

NewCommitter creates a new committer.

func (*MultiCommitter) Begin

func (m *MultiCommitter) Begin(ctx context.Context, idempotencyKey []byte) (err error)

Begin begins a session for the committables. It will automatically detect if the idempotency key has been used before, and if so, will handle the recovery automatically.

func (*MultiCommitter) Commit

func (m *MultiCommitter) Commit(ctx context.Context, idempotencyKey []byte) (id []byte, err error)

Commit commits the session for the committables. It returns a unique identifier for the session that is generated deterministically from the applied changes.

func (*MultiCommitter) Register

func (m *MultiCommitter) Register(name string, committable Committable) error

Register registers a committable with the committer.

Directories

Path Synopsis
Package committable provides an easy to use interface for creating committables.
Package committable provides an easy to use interface for creating committables.

Jump to

Keyboard shortcuts

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