Documentation
¶
Index ¶
- Variables
- func GetTenant(ctx context.Context) (string, bool)
- func SetTenant(ctx context.Context, tenantID string) context.Context
- type CacheOption
- type CompositeResolver
- type ConnectionOption
- type HeaderResolver
- type SubdomainResolver
- type TenantConfigCache
- type TenantConfigProvider
- type TenantConnectionManager
- func (m *TenantConnectionManager) CleanupIdleConnections()
- func (m *TenantConnectionManager) Close() error
- func (m *TenantConnectionManager) GetDatabase(ctx context.Context, tenantID string) (database.Interface, error)
- func (m *TenantConnectionManager) RefreshTenant(_ context.Context, tenantID string) error
- func (m *TenantConnectionManager) StartCleanup(interval time.Duration)
- func (m *TenantConnectionManager) StopCleanup()
- type TenantMessagingConfig
- type TenantResolver
- type ValidatingResolver
Constants ¶
This section is empty.
Variables ¶
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 ¶
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.
type CompositeResolver ¶
type CompositeResolver struct {
Resolvers []TenantResolver
TenantRegex *regexp.Regexp // Optional validation pattern
}
CompositeResolver tries multiple resolvers until one succeeds.
func (*CompositeResolver) ResolveTenant ¶
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 ¶
ResolveTenant implements TenantResolver.
type SubdomainResolver ¶
SubdomainResolver extracts the tenant identifier from the request host.
func (*SubdomainResolver) ResolveTenant ¶
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 ¶
ResolveTenant implements TenantResolver with validation.