transactionalstate

package
v0.1.0-c02.1 Latest Latest
Warning

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

Go to latest
Published: Sep 7, 2026 License: Apache-2.0 Imports: 17 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 transactionalstate provides PostgreSQL-backed document state and an append-only audit journal for CloudRING control-plane state. Documents expose optimistic revisions instead of hiding concurrent writers behind a last-write-wins API.

Index

Constants

View Source
const (
	// MaximumAuditEventBytes is the maximum canonical JSON payload accepted by
	// AppendAuditEvent.
	MaximumAuditEventBytes = 256 * 1024
	// MaximumAuditPageSize bounds every audit-journal read.
	MaximumAuditPageSize = 100
)

Variables

View Source
var (
	ErrConflict = errors.New("transactional state revision conflict")
	ErrNotFound = errors.New("transactional state document not found")
	ErrReadOnly = errors.New("transactional state database is read-only")
)

Functions

func Migrate

func Migrate(ctx context.Context, config Config) error

Migrate applies additive schema versions under a transaction-scoped advisory lock. It creates only a completely absent schema and otherwise verifies the recorded checksums, catalog shape, ownership, and effective privileges before applying the next version. Unexpected pre-existing objects are never repaired or blessed.

Types

type AuditEvent

type AuditEvent struct {
	Scope     string
	ID        string
	Value     []byte
	CreatedAt time.Time
}

AuditEvent is one immutable journal entry. ID is supplied by the caller and must remain stable across retries. Scope is the tenant or other isolation boundary chosen by the caller.

type AuditEventPage

type AuditEventPage struct {
	Events      []AuditEvent
	NextAfterID string
}

AuditEventPage is one deterministic ascending page. When NextAfterID is not empty, pass it as afterID to retrieve the next page.

type Config

type Config struct {
	DSN                   string
	ApplicationName       string
	MigrationOwnerRole    string
	ApplicationRole       string
	MaximumConnections    int32
	MinimumConnections    int32
	ConnectTimeout        time.Duration
	OperationTimeout      time.Duration
	AllowInsecureForTests bool
}

Config is safe to construct from deployment-private secret material. DSN is never included in returned errors and, in production, must be a self-contained URL with inline authentication and verified TLS. The explicit insecure switch exists only for an isolated test database.

type Digest

type Digest struct {
	Revision       int64
	DataSHA256     string
	ValidatedBytes int64
}

Digest is safe one-server-loss probe material. It contains no document content or deployment identity.

type Document

type Document struct {
	Scope     string
	Key       string
	Revision  int64
	Value     []byte
	UpdatedAt time.Time
}

Document is one immutable read snapshot. Revision starts at one and changes on every successful replacement.

type Store

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

Store is safe for concurrent use.

func Open

func Open(ctx context.Context, config Config) (*Store, error)

Open creates a bounded pool and verifies that it reaches a writable primary. Schema migrations are intentionally separate so steady-state application credentials do not need DDL privileges.

func (*Store) AppendAuditEvent

func (store *Store) AppendAuditEvent(ctx context.Context, scope, eventID string, value []byte) (AuditEvent, error)

AppendAuditEvent atomically appends an immutable event. Repeating an append with the same scope, ID, and canonical JSON is idempotent and returns the original event. Reusing an ID with different content returns ErrConflict.

func (*Store) Close

func (store *Store) Close()

Close releases every pooled connection.

func (*Store) Delete

func (store *Store) Delete(ctx context.Context, scope, key string, expectedRevision int64) error

Delete removes exactly one observed revision.

func (*Store) Digest

func (store *Store) Digest(ctx context.Context, scope, key string) (Digest, error)

Digest reads and hashes the same canonical complete document returned by Load. Callers may publish only this safe result, never Value.

func (*Store) ListAuditEvents

func (store *Store) ListAuditEvents(ctx context.Context, scope, afterID string, pageSize int) (AuditEventPage, error)

ListAuditEvents returns at most pageSize events in ascending bytewise ID order. afterID is exclusive and may be empty for the first page.

func (*Store) Load

func (store *Store) Load(ctx context.Context, scope, key string) (Document, error)

Load returns one canonical JSON snapshot.

func (*Store) ReadAuditEvent

func (store *Store) ReadAuditEvent(ctx context.Context, scope, eventID string) (AuditEvent, error)

ReadAuditEvent reads exactly one event within the supplied isolation scope.

func (*Store) Ready

func (store *Store) Ready(ctx context.Context) error

Ready proves that the selected endpoint currently resolves to a writable primary. A read-only replica is never accepted for state mutations.

func (*Store) Save

func (store *Store) Save(ctx context.Context, scope, key string, expectedRevision int64, value []byte) (Document, error)

Save creates a document when expectedRevision is zero, or atomically replaces the exact expected revision. It never performs last-write-wins.

func (*Store) VerifyApplicationIdentity

func (store *Store) VerifyApplicationIdentity(ctx context.Context, ownerRole, applicationRole string) error

VerifyApplicationIdentity checks effective login privileges and the migrated schema using the application's own connection. A DSN username alone does not establish that a serving process lacks migration or superuser privileges. This read-only check never executes migrations or needs the owner's secret.

Jump to

Keyboard shortcuts

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