sessionstore

package
v0.2.0-beta.3 Latest Latest
Warning

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

Go to latest
Published: Sep 4, 2026 License: Apache-2.0 Imports: 16 Imported by: 0

Documentation

Overview

Package sessionstore persists the fenced DeviceSession state machine.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNotFound        = errors.New("resource not found")
	ErrBusy            = errors.New("resource already leased")
	ErrFenced          = errors.New("stale lease generation or fence")
	ErrConflict        = errors.New("idempotency key conflict")
	ErrExpired         = errors.New("binding or lease expired")
	ErrInvalidState    = errors.New("invalid session state transition")
	ErrIdentityRevoked = errors.New("identity mapping is revoked")
	ErrInvalidArgument = errors.New("invalid request argument")
	ErrBackpressure    = errors.New("runtime backpressure limit reached")
)

Functions

This section is empty.

Types

type AcquireInput

type AcquireInput struct {
	TenantID              string
	PrincipalID           string
	AuthProfileID         string
	ChannelBindingSHA256  string
	RequestNonce          string
	BindingExpiresAt      time.Time
	ResourceID            string
	RequestedCapabilities []string
	IdempotencyKey        string
	ReleaseIdempotencyKey string
	LeaseDuration         time.Duration
	HeartbeatInterval     time.Duration
}

type ExecutionFailure

type ExecutionFailure struct {
	Code        string
	Retryable   bool
	SafeMessage string
}

type FrameContent

type FrameContent struct {
	Content     []byte
	ContentType string
	SHA256      string
}

type FrameContentRequest

type FrameContentRequest struct {
	SessionID            string
	TenantID             string
	PrincipalID          string
	ChannelBindingSHA256 string
	RequestNonce         string
	BindingExpiresAt     time.Time
	Generation           int64
	FencingTokenSHA256   string
	StreamEpoch          int64
	FrameSequence        int64
	ContentSHA256        string
}

type FrameData

type FrameData struct {
	Content     []byte
	ContentType string
	Orientation string
	Width       int
	Height      int
}

type FrameWork

type FrameWork struct {
	SessionID          string
	TenantID           string
	ResourceID         string
	Generation         int64
	FencingTokenSHA256 string
	StreamEpoch        int64
	ClaimedBy          string
	WorkerEpoch        int64
	ClaimGeneration    int64
}

type Identity

type Identity struct {
	CertificateFingerprint string     `json:"certificate_fingerprint_sha256"`
	TenantID               string     `json:"tenant_id"`
	PrincipalID            string     `json:"principal_id"`
	RevokedAt              *time.Time `json:"revoked_at,omitempty"`
}

type IdentityAdmin

type IdentityAdmin interface {
	UpsertIdentity(context.Context, Identity) error
	RevokeIdentity(context.Context, string, time.Time) error
	ListIdentities(context.Context) ([]Identity, error)
}

type InputWork

type InputWork struct {
	SessionID          string
	TenantID           string
	ResourceID         string
	RequestSequence    int64
	RequestID          string
	IdempotencyKey     string
	Generation         int64
	FencingTokenSHA256 string
	StreamEpoch        int64
	FrameSequence      int64
	Command            string
	CommandPayload     json.RawMessage
	ClaimedBy          string
	WorkerEpoch        int64
	ClaimGeneration    int64
	StartedAt          time.Time
}

type MutationInput

type MutationInput struct {
	SessionID             string
	TenantID              string
	PrincipalID           string
	ChannelBindingSHA256  string
	RequestNonce          string
	BindingExpiresAt      time.Time
	RequestID             string
	Type                  string
	IdempotencyKey        string
	Generation            int64
	FencingTokenSHA256    string
	Payload               json.RawMessage
	CommandPayload        json.RawMessage
	RequestedExtension    time.Duration
	LastAcknowledgedEvent int64
}

type NodeLease

type NodeLease struct {
	NodeID      string
	WorkerEpoch int64
}

NodeLease identifies one live worker process. WorkerEpoch changes whenever an expired node identity is taken over, fencing every operation issued by an older process that used the same NodeID.

type Postgres

type Postgres struct{ Pool *pgxpool.Pool }

func Open

func Open(ctx context.Context, databaseURL string) (*Postgres, error)

func (*Postgres) Acquire

func (store *Postgres) Acquire(ctx context.Context, input AcquireInput) (Result, error)

func (*Postgres) ActivateNode

func (store *Postgres) ActivateNode(ctx context.Context, lease NodeLease) error

func (*Postgres) Apply

func (store *Postgres) Apply(ctx context.Context, input MutationInput) (Result, error)

func (*Postgres) ApplySchema

func (store *Postgres) ApplySchema(ctx context.Context) error

func (*Postgres) ClaimFrame

func (store *Postgres) ClaimFrame(ctx context.Context, lease NodeLease, claimFor time.Duration) (FrameWork, error)

func (*Postgres) ClaimInput

func (store *Postgres) ClaimInput(ctx context.Context, lease NodeLease, claimFor time.Duration) (InputWork, error)

func (*Postgres) Close

func (store *Postgres) Close()

func (*Postgres) CompleteFrame

func (store *Postgres) CompleteFrame(ctx context.Context, work FrameWork, frame FrameData) error

func (*Postgres) CompleteInput

func (store *Postgres) CompleteInput(ctx context.Context, work InputWork, result string, latency time.Duration, failure *ExecutionFailure) error

func (*Postgres) CurrentTime

func (store *Postgres) CurrentTime(ctx context.Context) (time.Time, error)

func (*Postgres) DeactivateNode

func (store *Postgres) DeactivateNode(ctx context.Context, lease NodeLease) error

func (*Postgres) Events

func (store *Postgres) Events(ctx context.Context, tenantID, principalID, sessionID string, after int64) ([]devicesessionv1.Event, error)

func (*Postgres) ExpireSessions

func (store *Postgres) ExpireSessions(ctx context.Context, limit int) (int64, error)

func (*Postgres) FailFrame

func (store *Postgres) FailFrame(ctx context.Context, work FrameWork, code string, retryable bool, message string) error

func (*Postgres) FrameContent

func (store *Postgres) FrameContent(ctx context.Context, input FrameContentRequest) (FrameContent, error)

func (*Postgres) HeartbeatNode

func (store *Postgres) HeartbeatNode(ctx context.Context, lease NodeLease, leaseFor time.Duration) error

func (*Postgres) ListIdentities

func (store *Postgres) ListIdentities(ctx context.Context) ([]Identity, error)

func (*Postgres) MarkDisconnected

func (store *Postgres) MarkDisconnected(ctx context.Context, tenantID, principalID, sessionID, channelBinding, requestNonce string, bindingExpiresAt time.Time, generation int64, fence, reason string) error

MarkDisconnected records transport loss without releasing or changing the fence. Repeated observations are idempotent; reconnect remains client-driven.

func (*Postgres) Ping

func (store *Postgres) Ping(ctx context.Context) error

func (*Postgres) RecoverAmbiguousInputs

func (store *Postgres) RecoverAmbiguousInputs(ctx context.Context, lease NodeLease, olderThan time.Duration) (int64, error)

func (*Postgres) RegisterDevice

func (store *Postgres) RegisterDevice(ctx context.Context, tenantID, resourceID string, lease NodeLease, capabilities []string) error

func (*Postgres) RegisterNode

func (store *Postgres) RegisterNode(ctx context.Context, nodeID, address string, leaseFor time.Duration) (NodeLease, error)

func (*Postgres) RequireReadyNode

func (store *Postgres) RequireReadyNode(ctx context.Context, lease NodeLease) error

func (*Postgres) ReserveTokenNonce

func (store *Postgres) ReserveTokenNonce(ctx context.Context, fingerprint, nonce string, ttl time.Duration) (TokenWindow, error)

func (*Postgres) ResolveIdentity

func (store *Postgres) ResolveIdentity(ctx context.Context, fingerprint string) (Identity, error)

func (*Postgres) RevokeIdentity

func (store *Postgres) RevokeIdentity(ctx context.Context, fingerprint string, at time.Time) error

func (*Postgres) StartInput

func (store *Postgres) StartInput(ctx context.Context, work InputWork) error

StartInput is the durable no-return point. A stale claimed job may be retried; a stale executing job is never re-executed because the external mutation may already have happened.

func (*Postgres) UpsertIdentity

func (store *Postgres) UpsertIdentity(ctx context.Context, identity Identity) error

func (*Postgres) ValidateSessionAccess

func (store *Postgres) ValidateSessionAccess(ctx context.Context, tenantID, principalID, sessionID, channelBinding, requestNonce string, bindingExpiresAt time.Time, generation int64, fence string) error

func (*Postgres) WaitEvents

func (store *Postgres) WaitEvents(ctx context.Context, tenantID, principalID, sessionID string, after int64, wait time.Duration) ([]devicesessionv1.Event, bool, error)

func (*Postgres) WaitForExecutionQuiescence

func (store *Postgres) WaitForExecutionQuiescence(ctx context.Context, lease NodeLease, olderThan, poll time.Duration) error

func (*Postgres) WaitForWork

func (store *Postgres) WaitForWork(ctx context.Context, nodeID string, wait time.Duration) error

func (*Postgres) WaitInputActive

func (store *Postgres) WaitInputActive(ctx context.Context, work InputWork, poll time.Duration) (bool, error)

type Result

type Result struct {
	Session Session               `json:"session"`
	Event   devicesessionv1.Event `json:"event"`
	Replay  bool                  `json:"replay"`
	Queued  bool                  `json:"queued,omitempty"`
}

type Session

type Session struct {
	SessionID             string        `json:"session_id"`
	TenantID              string        `json:"tenant_id"`
	PrincipalID           string        `json:"principal_id"`
	AuthProfileID         string        `json:"auth_profile_id"`
	ChannelBindingSHA256  string        `json:"channel_binding_sha256"`
	RequestNonce          string        `json:"request_nonce"`
	BindingExpiresAt      time.Time     `json:"binding_expires_at"`
	ResourceID            string        `json:"resource_id"`
	OwnerNodeID           string        `json:"owner_node_id,omitempty"`
	OwnerWorkerEpoch      int64         `json:"owner_worker_epoch,omitempty"`
	LeaseID               string        `json:"lease_id"`
	Generation            int64         `json:"generation"`
	FencingTokenSHA256    string        `json:"fencing_token_sha256"`
	ReleaseIdempotencyKey string        `json:"release_idempotency_key"`
	Capabilities          []string      `json:"capabilities"`
	Status                string        `json:"status"`
	StreamEpoch           int64         `json:"stream_epoch"`
	AcquiredAt            time.Time     `json:"acquired_at"`
	LeaseExpiresAt        time.Time     `json:"lease_expires_at"`
	HeartbeatInterval     time.Duration `json:"heartbeat_interval"`
}

type TokenWindow

type TokenWindow struct {
	IssuedAt  time.Time
	ExpiresAt time.Time
}

Jump to

Keyboard shortcuts

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