Documentation
¶
Overview ¶
Package persistence owns session records, guarded writes, and retirement. Cookies, events, and runtime session provenance remain with the caller.
Index ¶
- type Record
- type Store
- func (store Store) Delete(ctx context.Context, id string) error
- func (store Store) Get(ctx context.Context, id string) (value Record, found bool, err error)
- func (store Store) Lock(ctx context.Context, id string) (*cache.Lease, error)
- func (store Store) Match(ctx context.Context, id string) (cache.Match, bool, error)
- func (store Store) Save(ctx context.Context, value Record, loaded bool, ttl time.Duration, ...) (bool, error)
- func (store Store) Tombstone(ctx context.Context, id string, ttl time.Duration) error
- func (store Store) Unlock(ctx context.Context, lease *cache.Lease)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Record ¶
type Record struct {
// ID is the session identifier associated with this stored record.
ID string `json:"id"`
// CreatedAt anchors maximum-lifetime checks across session renewals.
CreatedAt time.Time `json:"created_at"`
// ExpiresAt is the current idle-expiration deadline persisted with the
// session.
ExpiresAt time.Time `json:"expires_at"`
// Storage contains session values; the decoding view retains raw JSON until
// typed lookup.
Storage map[string]any `json:"storage"`
// Tombstone marks an ID that was retired by logout, destroy, or
// regeneration. Records written before tombstones existed lack the field
// and load as live sessions.
Tombstone bool `json:"tombstone,omitempty"`
}
Record is the stored form of a session.
type Store ¶
type Store struct {
// driver holds the session records.
Driver cache.Driver
// locks reduces conflicts between saves and tombstones. Atomic guarded
// writes, not these expiring leases, enforce retirement correctness.
Locks *cache.Cache
// space names JSON records without changing the asymmetric session codec.
Space cache.Namespace[jsontext.Value]
}
Store persists session records in a cache driver.
func (Store) Get ¶
Get loads the session stored under id. found is false when the cache has no record or only a tombstone, which starts a fresh session; other errors are storage failures.
func (Store) Match ¶
Match captures the exact live record that authorizes a loaded-session save. Missing, unreadable, or tombstoned records return false; backend failures are verification errors. The returned bytes come from the driver's owned copy and are passed unchanged to CompareAndPut, never decoded and re-encoded.
func (Store) Save ¶
func (store Store) Save(ctx context.Context, value Record, loaded bool, ttl time.Duration, match cache.Match) (bool, error)
Save stores value under its current ID for ttl. A loaded session requires the exact original-record match obtained during verification; checking that record and writing the current ID are one atomic operation, even after regeneration. A stale match returns false without writing. Fresh sessions ignore match and use Put because no earlier record authorizes their creation. A nonpositive remaining lifetime is discarded rather than passing cache's zero-TTL/no-expiry policy through to a session record.
func (Store) Tombstone ¶
Tombstone retires id by overwriting its record with a tombstone for ttl, so a request that loaded the session earlier cannot save it back to life. ttl retains the marker for the configured absolute lifetime (idle TTL when that maximum is disabled). CompareAndPut rejects both tombstoned and missing original records, so safety does not depend on the marker outliving a pending writer. The lease reduces contention, not the atomic retirement guarantee.
func (Store) Unlock ¶
Unlock attempts release independently of ctx's cancellation, but for at most one lockTTL interval. A cooperative backend therefore cannot hold response commitment indefinitely after persistence. Errors remain best effort because the lease expires by itself; the cleanup context retains request trace values.