Documentation
¶
Overview ¶
Package tenant provides multi-tenant primitives: a tenant store abstraction and a TTL-cached store that fronts it.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Base ¶
type Base struct {
ID uuid.UUID `json:"id" gorm:"primaryKey;type:uuid"`
Code string `json:"code" gorm:"type:varchar(50);uniqueIndex;not null"`
IsActive bool `json:"isActive" gorm:"not null"`
CreatedAt time.Time `json:"createdAt" gorm:"type:timestamp;not null"`
UpdatedAt time.Time `json:"updatedAt" gorm:"type:timestamp;not null"`
}
type CachedStore ¶
type CachedStore struct {
// contains filtered or unexported fields
}
CachedStore wraps a Store with an in-memory, time-bounded cache. It satisfies Store itself, so it can be passed wherever a Store is accepted.
store := tenant.NewCachedStore(dbStore, 5*time.Minute)
Both successful and "not found" lookups are cached for the configured TTL; WithNegativeTTL gives the second kind a shorter one. Call Invalidate when a tenant changes to drop its entry before the TTL expires. It is safe for concurrent use.
A wrapped store that returns a *typed* nil — (*YourTenant)(nil) rather than a plain nil — is caching a value that is not nil as an interface and will not be recognised as an absent tenant, here or by middleware.Tenant. The cache makes that bug outlive the request it happened on, so return an untyped nil.
func NewCachedStore ¶
func NewCachedStore(store Store, ttl time.Duration, opts ...Option) *CachedStore
NewCachedStore returns a CachedStore that caches lookups from store for ttl.
func (*CachedStore) Clear ¶ added in v0.22.0
func (c *CachedStore) Clear()
Clear drops every cached entry, forcing subsequent FindTenant calls to consult the wrapped store. It is safe for concurrent use.
func (*CachedStore) FindTenant ¶
FindTenant returns the tenant for id, serving from cache when a non-expired entry exists — including a remembered absence — and otherwise delegating to the wrapped store and caching the outcome.
A Store may report an absent tenant either way round, and both are treated as the same answer and held for the same negative TTL:
- (nil, nil), which is what middleware.Tenant turns into a 401 and is the spelling a Store implementation should prefer;
- (nil, errorx.NotFoundError), for a store that surfaces its repository's error directly.
A hit replays whichever one the store gave, so wrapping a Store in a CachedStore never changes what its caller sees.
func (*CachedStore) Invalidate ¶
func (c *CachedStore) Invalidate(id uuid.UUID)
Invalidate removes the cached entry for id, forcing the next FindTenant to consult the wrapped store.
type Option ¶
type Option func(*CachedStore)
Option customizes a CachedStore.
func WithClock ¶
func WithClock(clock core.TimeProvider) Option
WithClock injects the time source used for TTL expiry. Defaults to core.UTC. Pass a *core.FixedClock for deterministic tests.
func WithNegativeTTL ¶ added in v0.38.0
WithNegativeTTL sets how long "no such tenant" is remembered, which defaults to the store's positive TTL.
Caching the absence at all is what keeps a stream of requests carrying an unknown tenant ID costing one lookup instead of one per request — the difference between a misconfigured client being harmless and it being a load generator aimed at the tenant store. Shortening it trades some of that protection back for freshness, since a tenant created after a failed lookup stays invisible to this replica until the entry expires; a few seconds is usually enough to absorb a burst without being noticeable.
Pass 0 to switch negative caching off entirely, and call Invalidate on the create path when the creating replica also serves requests.
Only an absent tenant is remembered. A store that fails for any other reason — a timeout, a dropped connection — is never cached, whatever this is set to.