multitenant

package
v0.9.0-beta Latest Latest
Warning

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

Go to latest
Published: Sep 24, 2025 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrTenantNotFound indicates the tenant configuration was not found in the provider.
	ErrTenantNotFound = errors.New("tenant configuration not found")
	// ErrTenantResolutionFailed indicates the resolver could not determine the tenant ID.
	ErrTenantResolutionFailed = errors.New("tenant resolution failed")
)

Functions

func GetTenant

func GetTenant(ctx context.Context) (string, bool)

GetTenant extracts the tenant identifier from the context.

func SetTenant

func SetTenant(ctx context.Context, tenantID string) context.Context

SetTenant stores the tenant identifier in the provided context.

Types

type CacheOption

type CacheOption func(*cacheConfig)

CacheOption configures the tenant config cache.

func WithMaxSize

func WithMaxSize(maxSize int) CacheOption

WithMaxSize sets the maximum number of cached tenants.

func WithStaleGracePeriod

func WithStaleGracePeriod(period time.Duration) CacheOption

WithStaleGracePeriod sets how long stale data can be served on provider errors.

func WithTTL

func WithTTL(ttl time.Duration) CacheOption

WithTTL sets the cache TTL.

type CompositeResolver

type CompositeResolver struct {
	Resolvers   []TenantResolver
	TenantRegex *regexp.Regexp // Optional validation pattern
}

CompositeResolver tries multiple resolvers until one succeeds.

func (*CompositeResolver) ResolveTenant

func (r *CompositeResolver) ResolveTenant(ctx context.Context, req *http.Request) (string, error)

ResolveTenant implements TenantResolver.

type ConnectionOption

type ConnectionOption func(*connectionConfig)

ConnectionOption configures the tenant connection manager.

func WithIdleTTL

func WithIdleTTL(ttl time.Duration) ConnectionOption

WithIdleTTL sets the idle time-to-live before eviction.

func WithMaxTenants

func WithMaxTenants(maxTenants int) ConnectionOption

WithMaxTenants sets the max number of cached tenant connections.

type HeaderResolver

type HeaderResolver struct {
	HeaderName string
}

HeaderResolver extracts the tenant identifier from a configured request header.

func (*HeaderResolver) ResolveTenant

func (r *HeaderResolver) ResolveTenant(ctx context.Context, req *http.Request) (string, error)

ResolveTenant implements TenantResolver.

type SubdomainResolver

type SubdomainResolver struct {
	RootDomain   string
	TrustProxies bool
}

SubdomainResolver extracts the tenant identifier from the request host.

func (*SubdomainResolver) ResolveTenant

func (r *SubdomainResolver) ResolveTenant(ctx context.Context, req *http.Request) (string, error)

ResolveTenant implements TenantResolver.

type TenantConfigCache

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

TenantConfigCache caches tenant configurations to avoid repeated provider calls.

func NewTenantConfigCache

func NewTenantConfigCache(provider TenantConfigProvider, opts ...CacheOption) *TenantConfigCache

NewTenantConfigCache creates a new cache using the provided options.

func (*TenantConfigCache) GetDatabase

func (c *TenantConfigCache) GetDatabase(ctx context.Context, tenantID string) (*config.DatabaseConfig, error)

GetDatabase returns the cached or freshly fetched database configuration for the tenant.

func (*TenantConfigCache) GetMessaging

func (c *TenantConfigCache) GetMessaging(ctx context.Context, tenantID string) (*TenantMessagingConfig, error)

GetMessaging returns the cached or freshly fetched messaging configuration for the tenant.

type TenantConfigProvider

type TenantConfigProvider interface {
	GetDatabase(ctx context.Context, tenantID string) (*config.DatabaseConfig, error)
	GetMessaging(ctx context.Context, tenantID string) (*TenantMessagingConfig, error)
}

TenantConfigProvider fetches tenant specific configuration from an external source.

type TenantConnectionManager

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

TenantConnectionManager manages tenant-specific database connections.

func NewTenantConnectionManager

func NewTenantConnectionManager(provider TenantConfigProvider, cache *TenantConfigCache, log logger.Logger, opts ...ConnectionOption) *TenantConnectionManager

NewTenantConnectionManager creates a new manager with the provided cache and options.

func (*TenantConnectionManager) CleanupIdleConnections

func (m *TenantConnectionManager) CleanupIdleConnections()

CleanupIdleConnections removes connections that have been idle for too long

func (*TenantConnectionManager) Close

func (m *TenantConnectionManager) Close() error

Close closes all tenant connections

func (*TenantConnectionManager) GetDatabase

func (m *TenantConnectionManager) GetDatabase(ctx context.Context, tenantID string) (database.Interface, error)

GetDatabase returns a tenant-specific database interface.

func (*TenantConnectionManager) RefreshTenant

func (m *TenantConnectionManager) RefreshTenant(_ context.Context, tenantID string) error

RefreshTenant closes and recreates the connection for a specific tenant

func (*TenantConnectionManager) StartCleanup

func (m *TenantConnectionManager) StartCleanup(interval time.Duration)

StartCleanup starts the periodic cleanup of idle connections. If interval is 0 or negative, cleanup is disabled.

func (*TenantConnectionManager) StopCleanup

func (m *TenantConnectionManager) StopCleanup()

StopCleanup stops the periodic cleanup of idle connections.

type TenantMessagingConfig

type TenantMessagingConfig struct {
	URL string
}

TenantMessagingConfig holds the minimal messaging configuration resolved per tenant.

type TenantResolver

type TenantResolver interface {
	ResolveTenant(ctx context.Context, req *http.Request) (string, error)
}

TenantResolver resolves the tenant identifier from an incoming request.

type ValidatingResolver

type ValidatingResolver struct {
	Resolver    TenantResolver
	TenantRegex *regexp.Regexp
}

ValidatingResolver wraps any resolver with tenant ID validation.

func (*ValidatingResolver) ResolveTenant

func (r *ValidatingResolver) ResolveTenant(ctx context.Context, req *http.Request) (string, error)

ResolveTenant implements TenantResolver with validation.

Jump to

Keyboard shortcuts

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