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
- Variables
- func Migrate(ctx context.Context, config Config) error
- type AuditEvent
- type AuditEventPage
- type Config
- type Digest
- type Document
- type Store
- func (store *Store) AppendAuditEvent(ctx context.Context, scope, eventID string, value []byte) (AuditEvent, error)
- func (store *Store) Close()
- func (store *Store) Delete(ctx context.Context, scope, key string, expectedRevision int64) error
- func (store *Store) Digest(ctx context.Context, scope, key string) (Digest, error)
- func (store *Store) ListAuditEvents(ctx context.Context, scope, afterID string, pageSize int) (AuditEventPage, error)
- func (store *Store) Load(ctx context.Context, scope, key string) (Document, error)
- func (store *Store) ReadAuditEvent(ctx context.Context, scope, eventID string) (AuditEvent, error)
- func (store *Store) Ready(ctx context.Context) error
- func (store *Store) Save(ctx context.Context, scope, key string, expectedRevision int64, value []byte) (Document, error)
- func (store *Store) VerifyApplicationIdentity(ctx context.Context, ownerRole, applicationRole string) error
Constants ¶
const ( // MaximumAuditEventBytes is the maximum canonical JSON payload accepted by // AppendAuditEvent. MaximumAuditEventBytes = 256 * 1024 // MaximumAuditPageSize bounds every audit-journal read. MaximumAuditPageSize = 100 )
Variables ¶
Functions ¶
func Migrate ¶
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 ¶
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 ¶
Digest is safe one-server-loss probe material. It contains no document content or deployment identity.
type Document ¶
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 ¶
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) Digest ¶
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) ReadAuditEvent ¶
ReadAuditEvent reads exactly one event within the supplied isolation scope.
func (*Store) Ready ¶
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.