Documentation ¶
Index ¶
- Variables
- func NewCurrentTenant(ctx context.Context, id, name string) context.Context
- func NewCurrentTenantInfo(ctx context.Context, info TenantInfo) context.Context
- func NewTenantConfigContext(ctx context.Context, tenantId string, cfg *TenantConfig) context.Context
- func NewTenantResolveRes(ctx context.Context, t *TenantResolveResult) context.Context
- func WithDatabaseStyle(databaseStyle DatabaseStyleType) option
- func WithEnabled(isEnabled bool) option
- type BasicTenantInfo
- type Cache
- func (c *Cache[K, V]) Delete(key K)
- func (c *Cache[K, V]) Flush() error
- func (c *Cache[K, V]) Get(key K) (zero V, _ bool)
- func (c *Cache[K, V]) GetOrSet(key K, factory func() (V, error)) (zero V, set bool, err error)
- func (c *Cache[K, V]) Keys() []K
- func (c *Cache[K, V]) Len() int
- func (c *Cache[K, V]) Set(key K, val V)
- type ClientProvider
- type ClientProviderFunc
- type ConnStrGenerator
- type Context
- type ContextContrib
- type DatabaseStyleType
- type DbProvider
- type DefaultConnStrGenerator
- type DefaultDbProvider
- type DefaultTenantConfigProvider
- type DefaultTenantResolver
- type MemoryTenantStore
- type MultiTenancyConnStrResolver
- type MultiTenancyOption
- type MultiTenancySide
- type Option
- type ResolveOption
- type TenantConfig
- type TenantConfigProvider
- type TenantInfo
- type TenantNormalizerContrib
- type TenantResolveContrib
- type TenantResolveOption
- type TenantResolveResult
- type TenantResolver
- type TenantStore
Constants ¶
This section is empty.
Variables ¶
var (
ErrTenantNotFound = errors.New("tenant not found")
)
Functions ¶
func NewCurrentTenantInfo ¶
func NewCurrentTenantInfo(ctx context.Context, info TenantInfo) context.Context
func NewTenantConfigContext ¶
func NewTenantResolveRes ¶
func NewTenantResolveRes(ctx context.Context, t *TenantResolveResult) context.Context
func WithDatabaseStyle ¶
func WithDatabaseStyle(databaseStyle DatabaseStyleType) option
WithDatabaseStyle database style, support Single/PerTenant/Multi
Types ¶
type BasicTenantInfo ¶
func NewBasicTenantInfo ¶
func NewBasicTenantInfo(id string, name string) *BasicTenantInfo
func (*BasicTenantInfo) GetId ¶
func (b *BasicTenantInfo) GetId() string
func (*BasicTenantInfo) GetName ¶
func (b *BasicTenantInfo) GetName() string
type Cache ¶
type Cache[K comparable, V io.Closer] struct { // contains filtered or unexported fields }
Cache is used a LRU (Least recently used) cache replacement policy. adapted from https://github.com/Code-Hex/go-generics-cache/blob/main/policy/lru/lru.go
Discards the least recently used items first. This algorithm requires keeping track of what was used when, which is expensive if one wants to make sure the algorithm always discards the least recently used item.
func NewCache ¶
func NewCache[K comparable, V io.Closer](opts ...Option) *Cache[K, V]
NewCache creates a new thread safe LRU cache whose capacity is the default size (128).
func (*Cache[K, V]) Delete ¶
func (c *Cache[K, V]) Delete(key K)
Delete deletes the item with provided key from the cache.
func (*Cache[K, V]) Keys ¶
func (c *Cache[K, V]) Keys() []K
Keys returns the keys of the cache. the order is from oldest to newest.
type ClientProvider ¶
type ClientProvider[TClient interface{}] interface {
Get(ctx context.Context, dsn string) (TClient, error)
}
ClientProvider resolve by dsn string (connection string)
type ClientProviderFunc ¶
ClientProviderFunc see ClientProvider
type ConnStrGenerator ¶
type ConnStrGenerator interface {
Gen(ctx context.Context, tenant TenantInfo) (string, error)
}
ConnStrGenerator generate connection string for tenant. useful for tenant creation
type Context ¶
type Context struct { TenantIdOrName string // HasHandled field to handle host side unresolved or resolved HasHandled bool // contains filtered or unexported fields }
func NewTenantResolveContext ¶
func (*Context) HasResolved ¶
func (*Context) WithContext ¶
type ContextContrib ¶
type ContextContrib struct { }
ContextContrib resolve from current context
func (*ContextContrib) Name ¶
func (c *ContextContrib) Name() string
func (*ContextContrib) Resolve ¶
func (c *ContextContrib) Resolve(ctx *Context) error
type DatabaseStyleType ¶
type DatabaseStyleType int32
const ( Single DatabaseStyleType = 1 << 0 PerTenant DatabaseStyleType = 1 << 1 Multi DatabaseStyleType = 1 << 2 )
type DbProvider ¶
type DbProvider[TClient interface{}] interface { // Get instance by key Get(ctx context.Context, key string) TClient }
DbProvider resolve TClient from user friendly key
type DefaultConnStrGenerator ¶
type DefaultConnStrGenerator struct {
// contains filtered or unexported fields
}
func NewConnStrGenerator ¶
func NewConnStrGenerator(format string) *DefaultConnStrGenerator
func (*DefaultConnStrGenerator) Gen ¶
func (d *DefaultConnStrGenerator) Gen(ctx context.Context, tenant TenantInfo) (string, error)
type DefaultDbProvider ¶
type DefaultDbProvider[TClient interface{}] struct {
// contains filtered or unexported fields
}
DefaultDbProvider resolve dsn from user friendly key by data.ConnStrResolver, then resolve TClient from dsn by ClientProvider
func NewDbProvider ¶
func NewDbProvider[TClient interface{}](cs data.ConnStrResolver, cp ClientProvider[TClient]) (d *DefaultDbProvider[TClient])
type DefaultTenantConfigProvider ¶
type DefaultTenantConfigProvider struct {
// contains filtered or unexported fields
}
func (*DefaultTenantConfigProvider) Get ¶
func (d *DefaultTenantConfigProvider) Get(ctx context.Context) (TenantConfig, context.Context, error)
Get read from context FromTenantConfigContext first, fallback with TenantStore and return new context with cached value
type DefaultTenantResolver ¶
type DefaultTenantResolver struct {
// contains filtered or unexported fields
}
func (*DefaultTenantResolver) Resolve ¶
func (d *DefaultTenantResolver) Resolve(ctx context.Context) (TenantResolveResult, context.Context, error)
type MemoryTenantStore ¶
type MemoryTenantStore struct {
TenantConfig []TenantConfig
}
func NewMemoryTenantStore ¶
func NewMemoryTenantStore(t []TenantConfig) *MemoryTenantStore
func (*MemoryTenantStore) GetByNameOrId ¶
func (m *MemoryTenantStore) GetByNameOrId(_ context.Context, nameOrId string) (*TenantConfig, error)
type MultiTenancyConnStrResolver ¶
type MultiTenancyConnStrResolver struct {
// contains filtered or unexported fields
}
func NewMultiTenancyConnStrResolver ¶
func NewMultiTenancyConnStrResolver(ts TenantStore, fallback data.ConnStrResolver) *MultiTenancyConnStrResolver
NewMultiTenancyConnStrResolver from tenant
type MultiTenancyOption ¶
type MultiTenancyOption struct { IsEnabled bool DatabaseStyle DatabaseStyleType }
func DefaultMultiTenancyOption ¶
func DefaultMultiTenancyOption() *MultiTenancyOption
func NewMultiTenancyOption ¶
func NewMultiTenancyOption(opts ...option) *MultiTenancyOption
type MultiTenancySide ¶
type MultiTenancySide int32
const ( Tenant MultiTenancySide = 1 << 0 Host MultiTenancySide = 1 << 1 Both = Tenant | Host )
func GetMultiTenantSide ¶
func GetMultiTenantSide(ctx context.Context) MultiTenancySide
type Option ¶
type Option func(*options)
Option is an option for LRU cache.
func WithCapacity ¶
WithCapacity is an option to set cache capacity.
type ResolveOption ¶
type ResolveOption func(resolveOption *TenantResolveOption)
func AppendContribs ¶
func AppendContribs(c ...TenantResolveContrib) ResolveOption
func RemoveContribs ¶
func RemoveContribs(c ...TenantResolveContrib) ResolveOption
type TenantConfig ¶
type TenantConfig struct { ID string `json:"id"` Name string `json:"name"` Region string `json:"region"` PlanKey string `json:"planKey"` Conn data.ConnStrings `json:"conn"` }
func FromTenantConfigContext ¶
func FromTenantConfigContext(ctx context.Context, tenantId string) (*TenantConfig, bool)
func NewTenantConfig ¶
func NewTenantConfig(id, name, region, planKey string) *TenantConfig
type TenantConfigProvider ¶
type TenantConfigProvider interface { // Get tenant config Get(ctx context.Context) (TenantConfig, context.Context, error) }
TenantConfigProvider resolve tenant config from current context
func NewDefaultTenantConfigProvider ¶
func NewDefaultTenantConfigProvider(tr TenantResolver, ts TenantStore) TenantConfigProvider
type TenantInfo ¶
func FromCurrentTenant ¶
func FromCurrentTenant(ctx context.Context) (TenantInfo, bool)
type TenantNormalizerContrib ¶
type TenantNormalizerContrib struct {
// contains filtered or unexported fields
}
TenantNormalizerContrib normalize tenant id or name into tenant id
func NewTenantNormalizerContrib ¶
func NewTenantNormalizerContrib(ts TenantStore) *TenantNormalizerContrib
func (*TenantNormalizerContrib) Name ¶
func (t *TenantNormalizerContrib) Name() string
func (*TenantNormalizerContrib) Resolve ¶
func (t *TenantNormalizerContrib) Resolve(ctx *Context) error
type TenantResolveContrib ¶
type TenantResolveOption ¶
type TenantResolveOption struct {
Resolvers []TenantResolveContrib
}
func NewTenantResolveOption ¶
func NewTenantResolveOption(c ...TenantResolveContrib) *TenantResolveOption
func (*TenantResolveOption) AppendContribs ¶
func (opt *TenantResolveOption) AppendContribs(c ...TenantResolveContrib)
func (*TenantResolveOption) RemoveContribs ¶
func (opt *TenantResolveOption) RemoveContribs(c ...TenantResolveContrib)
type TenantResolveResult ¶
func FromTenantResolveRes ¶
func FromTenantResolveRes(ctx context.Context) *TenantResolveResult
type TenantResolver ¶
type TenantResolver interface {
Resolve(ctx context.Context) (TenantResolveResult, context.Context, error)
}
func NewDefaultTenantResolver ¶
func NewDefaultTenantResolver(opt ...ResolveOption) TenantResolver
type TenantStore ¶
type TenantStore interface { // GetByNameOrId return nil and ErrTenantNotFound if tenant not found GetByNameOrId(ctx context.Context, nameOrId string) (*TenantConfig, error) }
Source Files ¶
- basic_tenant_info.go
- cache.go
- conn_str_generator.go
- context.go
- multi_tenancy_conn_str_resolver.go
- multi_tenancy_option.go
- multi_tenancy_side.go
- provider.go
- tenant_config.go
- tenant_config_provider.go
- tenant_resolve_context.go
- tenant_resolve_contrib.go
- tenant_resolve_option.go
- tenant_resolve_result.go
- tenant_resolver.go
- tenant_store.go
- utils.go