guard

package
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Oct 18, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package guard enforces tenant-scoped uniqueness rules, such as "one user per e-mail address". A claim is keyed by (tenant, rule, value) and runs in the transaction open in the context, so it commits or rolls back together with the rest of the unit of work.

PgStore needs the guards table from docs/postgres/schema.sql. InMemoryStore follows postgres.InMemoryUnitOfWork rollbacks.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrTaken is returned when the key is already claimed for the Tenant.
	// The surrounding transaction stays usable.
	ErrTaken = errors.New("guard: key already claimed")
	// ErrNoTenant is returned when the context carries no Tenant.
	ErrNoTenant = errors.New("guard: no tenant in context")
	// ErrEmptyKey is returned when the rule or value of a Key is empty.
	ErrEmptyKey = errors.New("guard: empty rule or value")
)

Functions

This section is empty.

Types

type InMemoryStore

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

InMemoryStore claims keys in memory. Claims are visible at once and released when the surrounding postgres.InMemoryUnitOfWork rolls back.

func NewInMemoryStore

func NewInMemoryStore() *InMemoryStore

NewInMemoryStore creates an empty InMemoryStore.

func (*InMemoryStore) Claim

func (s *InMemoryStore) Claim(ctx context.Context, key Key) error

Claim reserves key for the Tenant in ctx. It returns ErrTaken if the key is already claimed, including by another open transaction.

type Key

type Key struct {
	Rule  string
	Value string
}

Key identifies a unique value within a rule. The Tenant comes from the context.

type PgOption

type PgOption func(*PgStore)

PgOption configures a PgStore.

func WithSchema

func WithSchema(schema string) PgOption

WithSchema sets the Postgres schema holding the guards table. Defaults to public.

type PgStore

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

PgStore claims keys in the guards table.

func NewPgStore

func NewPgStore(pool *pgxpool.Pool, opts ...PgOption) *PgStore

NewPgStore creates a PgStore that runs on the transaction in the context, or on pool outside one.

func (*PgStore) Claim

func (s *PgStore) Claim(ctx context.Context, key Key) error

Claim reserves key for the Tenant in ctx. It returns ErrTaken if the key is already claimed. A claim made by another open transaction blocks until that transaction ends. Rolling back the surrounding transaction releases the claim.

Jump to

Keyboard shortcuts

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