Documentation
¶
Index ¶
- Constants
- Variables
- func BuildKey(tenant, scope string, parts ...any) (string, error)
- func BuildKeyFromMap(tenant, scope string, m map[string]any) (string, error)
- func ValidateKey(key string) error
- type BeginResult
- type Clock
- type KeyParts
- type MemoryStore
- func (s *MemoryStore) Complete(ctx context.Context, key string, ownerToken string, result []byte) (Record, error)
- func (s *MemoryStore) Delete(ctx context.Context, key string) error
- func (s *MemoryStore) Fail(ctx context.Context, key string, ownerToken string, code string, msg string) (Record, error)
- func (s *MemoryStore) Get(ctx context.Context, key string) (Record, bool, error)
- func (s *MemoryStore) Sweep(ctx context.Context) (int, error)
- func (s *MemoryStore) Touch(ctx context.Context, key string, ownerToken string, ttl time.Duration) (Record, error)
- func (s *MemoryStore) TryBegin(ctx context.Context, key string, ttl time.Duration) (BeginResult, error)
- type Options
- type Record
- type Store
Constants ¶
const ( KeyVersion = "v1" MaxTenantLen = 64 MaxScopeLen = 32 MaxKeyLen = 256 MaxParts = 32 MaxBytes = 32 * 1024 // 32 KiB input cap for hashing )
Variables ¶
Functions ¶
func BuildKey ¶
BuildKey computes a deterministic key for a tenant+scope from ordered parts. Parts are encoded deterministically and hashed with SHA-256.
func BuildKeyFromMap ¶
BuildKeyFromMap computes a deterministic key from a map by sorting keys. Useful when you have named inputs rather than ordered parts.
func ValidateKey ¶
ValidateKey checks format and returns nil if valid.
Types ¶
type BeginResult ¶
type KeyParts ¶
type KeyParts struct {
Version string `json:"version"`
Tenant string `json:"tenant"`
Scope string `json:"scope"`
Hash string `json:"hash"` // lowercase hex sha256
}
KeyParts is the parsed representation.
type MemoryStore ¶
type MemoryStore struct {
// contains filtered or unexported fields
}
MemoryStore is a reference in-memory implementation (suitable for local/dev). It is not durable.
func NewMemoryStore ¶
func NewMemoryStore(opts Options) *MemoryStore
NewMemoryStore constructs an in-memory store with defaults applied.
func (*MemoryStore) TryBegin ¶
func (s *MemoryStore) TryBegin(ctx context.Context, key string, ttl time.Duration) (BeginResult, error)
type Record ¶
type Record struct {
Key string `json:"key"`
State State `json:"state"`
// OwnerToken is set when in_progress and must match to Complete/Fail/Touch.
OwnerToken string `json:"owner_token,omitempty"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
ExpiresAt time.Time `json:"expires_at"`
// Result (for complete)
ResultHash string `json:"result_hash,omitempty"` // sha256 hex of ResultBytes
ResultBytes []byte `json:"result_bytes,omitempty"`
// Failure (for failed)
ErrorCode string `json:"error_code,omitempty"`
ErrorMsg string `json:"error_msg,omitempty"`
}
Record is the canonical idempotency entry. ResultBytes is optional; some services may store a pointer (URI) elsewhere and only store hashes here later.
type Store ¶
type Store interface {
// TryBegin attempts to acquire a lease for key. If key is complete, returns Fresh=false with the record.
// If key is in_progress and not expired, returns Fresh=false with ErrConflict.
// If key is expired, it is replaced and Fresh=true.
TryBegin(ctx context.Context, key string, ttl time.Duration) (BeginResult, error)
// Touch extends the lease if caller holds ownership (in_progress only).
Touch(ctx context.Context, key string, ownerToken string, ttl time.Duration) (Record, error)
// Complete marks the key complete (requires owner token).
Complete(ctx context.Context, key string, ownerToken string, result []byte) (Record, error)
// Fail marks the key failed (requires owner token).
Fail(ctx context.Context, key string, ownerToken string, code string, msg string) (Record, error)
// Get fetches record.
Get(ctx context.Context, key string) (Record, bool, error)
// Delete removes record (admin/debug).
Delete(ctx context.Context, key string)
// Sweep removes expired entries. Deterministic, no background goroutine.
Sweep(ctx context.Context) (removed int, err error)
}
Store defines the idempotency persistence contract.