Documentation
¶
Overview ¶
Package store defines tenant metadata storage contracts and memory, cached, and SQL implementations.
Index ¶
- Constants
- Variables
- type Cache
- type CachedStore
- func (store *CachedStore) CompareAndSwap(ctx context.Context, expected types.Tenant, updated types.Tenant) error
- func (store *CachedStore) Create(ctx context.Context, tenant types.Tenant) error
- func (store *CachedStore) Delete(ctx context.Context, id types.TenantID) error
- func (store *CachedStore) Get(ctx context.Context, id types.TenantID) (types.Tenant, error)
- func (store *CachedStore) List(ctx context.Context, filter ListFilter) ([]types.Tenant, error)
- func (store *CachedStore) Update(ctx context.Context, tenant types.Tenant) error
- type CompareAndSwapStore
- type ListFilter
- type MemoryCache
- func (cache *MemoryCache) Delete(ctx context.Context, id types.TenantID) error
- func (cache *MemoryCache) Get(ctx context.Context, id types.TenantID) (types.Tenant, bool, error)
- func (cache *MemoryCache) Invalidate(ctx context.Context) error
- func (cache *MemoryCache) Set(ctx context.Context, tenant types.Tenant, ttl time.Duration) error
- type MemoryStore
- func (store *MemoryStore) CompareAndSwap(ctx context.Context, expected types.Tenant, updated types.Tenant) error
- func (store *MemoryStore) Create(ctx context.Context, tenant types.Tenant) error
- func (store *MemoryStore) Delete(ctx context.Context, id types.TenantID) error
- func (store *MemoryStore) Get(ctx context.Context, id types.TenantID) (types.Tenant, error)
- func (store *MemoryStore) List(ctx context.Context, filter ListFilter) ([]types.Tenant, error)
- func (store *MemoryStore) ListPage(ctx context.Context, filter PageFilter) ([]types.Tenant, error)
- func (store *MemoryStore) Update(ctx context.Context, tenant types.Tenant) error
- type PageFilter
- type PagedStore
- type SQLDialect
- type SQLStore
- func (store *SQLStore) CompareAndSwap(ctx context.Context, expected types.Tenant, updated types.Tenant) (err error)
- func (store *SQLStore) Create(ctx context.Context, tenant types.Tenant) error
- func (store *SQLStore) Delete(ctx context.Context, id types.TenantID) error
- func (store *SQLStore) Get(ctx context.Context, id types.TenantID) (types.Tenant, error)
- func (store *SQLStore) List(ctx context.Context, filter ListFilter) (tenants []types.Tenant, err error)
- func (store *SQLStore) ListPage(ctx context.Context, filter PageFilter) (tenants []types.Tenant, err error)
- func (store *SQLStore) Update(ctx context.Context, tenant types.Tenant) error
- type SQLStoreOption
- type Store
Constants ¶
const (
// DefaultSQLTableName is the default tenant metadata table name.
DefaultSQLTableName = "tenants"
)
Variables ¶
var ( // ErrTenantNotFound reports that a tenant does not exist in the store. ErrTenantNotFound = errors.New("gotenancy/store: tenant not found") // ErrTenantAlreadyExists reports that a tenant already exists in the store. ErrTenantAlreadyExists = errors.New("gotenancy/store: tenant already exists") // ErrTenantConflict reports that tenant metadata changed after it was read. ErrTenantConflict = errors.New("gotenancy/store: tenant update conflict") // ErrInvalidTenant reports that tenant metadata is not valid for persistence. ErrInvalidTenant = errors.New("gotenancy/store: invalid tenant") // ErrInvalidListFilter reports an invalid tenant list filter. ErrInvalidListFilter = errors.New("gotenancy/store: invalid list filter") // ErrNilStore reports that a store dependency is nil. ErrNilStore = errors.New("gotenancy/store: nil store") // ErrNilCache reports that a cache dependency is nil. ErrNilCache = errors.New("gotenancy/store: nil cache") // ErrNilDB reports that a SQL store was created with a nil database handle. ErrNilDB = errors.New("gotenancy/store: nil db") // ErrInvalidTableName reports an unsafe SQL table name. ErrInvalidTableName = errors.New("gotenancy/store: invalid table name") // ErrUnsupportedSQLDialect reports an unsupported SQLStore dialect. ErrUnsupportedSQLDialect = errors.New("gotenancy/store: unsupported sql dialect") // ErrInvalidCacheSize reports an invalid bounded memory cache size. ErrInvalidCacheSize = errors.New("gotenancy/store: invalid cache size") )
Functions ¶
This section is empty.
Types ¶
type Cache ¶
type Cache interface {
Get(ctx context.Context, id types.TenantID) (types.Tenant, bool, error)
Set(ctx context.Context, tenant types.Tenant, ttl time.Duration) error
Delete(ctx context.Context, id types.TenantID) error
Invalidate(ctx context.Context) error
}
Cache stores tenant metadata for faster lookups.
type CachedStore ¶
type CachedStore struct {
// contains filtered or unexported fields
}
CachedStore wraps a Store with cache-aside reads and write-through invalidation. Read fills and writes are ordered across in-process wrappers that share the same non-nil pointer-backed Cache instance, even when their Store wrappers differ. Non-pointer Cache values are used in full-bypass mode because they do not provide a stable identity for coordinating wrappers. Applications with wrappers in different processes should configure a finite TTL or a cache with its own versioning/invalidation protocol. A non-positive TTL is safe across processes only when the cache provides that stronger coherence mechanism.
func NewCachedStore ¶
NewCachedStore creates a cached store decorator.
func (*CachedStore) CompareAndSwap ¶ added in v0.1.8
func (store *CachedStore) CompareAndSwap(ctx context.Context, expected types.Tenant, updated types.Tenant) error
CompareAndSwap atomically replaces expected tenant metadata and refreshes the cache. When the wrapped store does not implement CompareAndSwapStore, CachedStore provides a process-local read/compare/update fallback under its write lock.
func (*CachedStore) Get ¶
Get returns tenant metadata from cache when available, otherwise from the wrapped store.
func (*CachedStore) List ¶
func (store *CachedStore) List(ctx context.Context, filter ListFilter) ([]types.Tenant, error)
List delegates to the wrapped store.
type CompareAndSwapStore ¶ added in v0.1.8
type CompareAndSwapStore interface {
Store
CompareAndSwap(ctx context.Context, expected types.Tenant, updated types.Tenant) error
}
CompareAndSwapStore extends Store with an atomic conditional replacement. CompareAndSwap replaces expected with updated only when the persisted tenant still equals expected. Implementations return ErrTenantConflict when another writer changed the tenant first.
type ListFilter ¶
type ListFilter struct {
Statuses []types.TenantStatus
Limit int
Offset int
}
ListFilter restricts tenant list queries.
type MemoryCache ¶
type MemoryCache struct {
// contains filtered or unexported fields
}
MemoryCache is a thread-safe in-memory tenant cache with TTL support.
func NewBoundedMemoryCache ¶
func NewBoundedMemoryCache(maxEntries int) (*MemoryCache, error)
NewBoundedMemoryCache creates a memory cache with a maximum number of entries.
func NewMemoryCache ¶
func NewMemoryCache() *MemoryCache
NewMemoryCache creates an empty memory cache.
func (*MemoryCache) Invalidate ¶
func (cache *MemoryCache) Invalidate(ctx context.Context) error
Invalidate removes all cached tenant metadata.
type MemoryStore ¶
type MemoryStore struct {
// contains filtered or unexported fields
}
MemoryStore is a thread-safe in-memory tenant metadata store.
func NewMemoryStore ¶
func NewMemoryStore() *MemoryStore
NewMemoryStore creates an empty memory store.
func (*MemoryStore) CompareAndSwap ¶ added in v0.1.8
func (store *MemoryStore) CompareAndSwap(ctx context.Context, expected types.Tenant, updated types.Tenant) error
CompareAndSwap atomically replaces expected tenant metadata with updated.
func (*MemoryStore) List ¶
func (store *MemoryStore) List(ctx context.Context, filter ListFilter) ([]types.Tenant, error)
List returns tenants matching filter.
func (*MemoryStore) ListPage ¶ added in v0.1.6
func (store *MemoryStore) ListPage(ctx context.Context, filter PageFilter) ([]types.Tenant, error)
ListPage returns tenants after the cursor while preserving List filtering semantics.
type PageFilter ¶ added in v0.1.6
type PageFilter struct {
Statuses []types.TenantStatus
Limit int
Offset int
// Cursor returns rows ordered after the tenant ID cursor.
Cursor types.TenantID
}
PageFilter restricts cursor-based tenant list queries.
type PagedStore ¶ added in v0.1.6
type PagedStore interface {
Store
ListPage(ctx context.Context, filter PageFilter) ([]types.Tenant, error)
}
PagedStore extends Store with cursor-based tenant listing.
type SQLDialect ¶
type SQLDialect string
SQLDialect controls SQL placeholder rendering for SQLStore.
const ( // SQLDialectMySQL uses question-mark placeholders and is the default. SQLDialectMySQL SQLDialect = "mysql" // SQLDialectSQLite uses question-mark placeholders. SQLDialectSQLite SQLDialect = "sqlite" // SQLDialectPostgres uses numbered placeholders. SQLDialectPostgres SQLDialect = "postgres" )
type SQLStore ¶
type SQLStore struct {
// contains filtered or unexported fields
}
SQLStore persists tenant metadata through database/sql.
The table is expected to contain these columns: id, name, status, plan_id, config. The config column stores a JSON object with string keys and values.
func NewSQLStore ¶
func NewSQLStore(db *sql.DB, opts ...SQLStoreOption) (*SQLStore, error)
NewSQLStore creates a SQL-backed tenant metadata store.
func (*SQLStore) CompareAndSwap ¶ added in v0.1.8
func (store *SQLStore) CompareAndSwap(ctx context.Context, expected types.Tenant, updated types.Tenant) (err error)
CompareAndSwap atomically replaces expected tenant metadata with updated.
func (*SQLStore) List ¶
func (store *SQLStore) List(ctx context.Context, filter ListFilter) (tenants []types.Tenant, err error)
List returns tenants matching filter.
type SQLStoreOption ¶
SQLStoreOption configures SQLStore.
func WithSQLDialect ¶
func WithSQLDialect(dialect SQLDialect) SQLStoreOption
WithSQLDialect configures SQL placeholder rendering.
func WithTableName ¶
func WithTableName(table string) SQLStoreOption
WithTableName overrides the default tenant metadata table name.
type Store ¶
type Store interface {
Get(ctx context.Context, id types.TenantID) (types.Tenant, error)
List(ctx context.Context, filter ListFilter) ([]types.Tenant, error)
Create(ctx context.Context, tenant types.Tenant) error
Update(ctx context.Context, tenant types.Tenant) error
Delete(ctx context.Context, id types.TenantID) error
}
Store persists tenant metadata.