Documentation
¶
Overview ¶
Package tenant 提供多租户支持
本包实现多租户隔离和管理:
- 租户隔离:数据和资源隔离
- 租户配额:资源使用限制
- 租户上下文:请求级租户识别
- 租户路由:基于租户的请求路由
设计借鉴:
- PostgreSQL: Row Level Security
- Kubernetes: Namespace 隔离
- AWS: Multi-tenant SaaS
Index ¶
- func TenantID(ctx context.Context) string
- func WithTenant(ctx context.Context, tenant *Tenant) context.Context
- type Manager
- func (m *Manager) Activate(ctx context.Context, id string) error
- func (m *Manager) Create(ctx context.Context, tenant *Tenant) error
- func (m *Manager) Delete(ctx context.Context, id string) error
- func (m *Manager) Get(ctx context.Context, id string) (*Tenant, error)
- func (m *Manager) GetQuotaEnforcer() *QuotaEnforcer
- func (m *Manager) GetUsageTracker() *UsageTracker
- func (m *Manager) List(ctx context.Context, filter TenantFilter) ([]*Tenant, error)
- func (m *Manager) Suspend(ctx context.Context, id string) error
- func (m *Manager) Update(ctx context.Context, tenant *Tenant) error
- type MemoryStorage
- func (s *MemoryStorage) Delete(ctx context.Context, id string) error
- func (s *MemoryStorage) Get(ctx context.Context, id string) (*Tenant, error)
- func (s *MemoryStorage) List(ctx context.Context, filter TenantFilter) ([]*Tenant, error)
- func (s *MemoryStorage) Save(ctx context.Context, tenant *Tenant) error
- type Middleware
- type QuotaEnforcer
- type QuotaError
- type Tenant
- type TenantExtractor
- type TenantExtractorFunc
- type TenantFilter
- type TenantQuota
- type TenantStatus
- type TenantStorage
- type TenantType
- type UsageData
- type UsageTracker
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Manager ¶
type Manager struct {
// contains filtered or unexported fields
}
Manager 租户管理器
func (*Manager) GetQuotaEnforcer ¶
func (m *Manager) GetQuotaEnforcer() *QuotaEnforcer
GetQuotaEnforcer 获取配额执行器
func (*Manager) GetUsageTracker ¶
func (m *Manager) GetUsageTracker() *UsageTracker
GetUsageTracker 获取使用量追踪器
type MemoryStorage ¶
type MemoryStorage struct {
// contains filtered or unexported fields
}
MemoryStorage 内存存储
func (*MemoryStorage) Delete ¶
func (s *MemoryStorage) Delete(ctx context.Context, id string) error
Delete 删除租户
func (*MemoryStorage) List ¶
func (s *MemoryStorage) List(ctx context.Context, filter TenantFilter) ([]*Tenant, error)
List 列出租户
type Middleware ¶
type Middleware struct {
// contains filtered or unexported fields
}
Middleware 租户中间件
func NewMiddleware ¶
func NewMiddleware(manager *Manager, extractor TenantExtractor) *Middleware
NewMiddleware 创建中间件
type QuotaEnforcer ¶
type QuotaEnforcer struct {
// contains filtered or unexported fields
}
QuotaEnforcer 配额执行器
func NewQuotaEnforcer ¶
func NewQuotaEnforcer(manager *Manager) *QuotaEnforcer
NewQuotaEnforcer 创建配额执行器
func (*QuotaEnforcer) CheckAndTrack ¶
CheckAndTrack 检查并追踪
type Tenant ¶
type Tenant struct {
// ID 租户唯一标识
ID string `json:"id"`
// Name 租户名称
Name string `json:"name"`
// DisplayName 显示名称
DisplayName string `json:"display_name,omitempty"`
// Type 租户类型
Type TenantType `json:"type"`
// Status 租户状态
Status TenantStatus `json:"status"`
// Plan 订阅计划
Plan string `json:"plan,omitempty"`
// Quota 配额
Quota *TenantQuota `json:"quota,omitempty"`
// Settings 租户设置
Settings map[string]any `json:"settings,omitempty"`
// Metadata 元数据
Metadata map[string]any `json:"metadata,omitempty"`
// CreatedAt 创建时间
CreatedAt time.Time `json:"created_at"`
// UpdatedAt 更新时间
UpdatedAt time.Time `json:"updated_at"`
// ExpiresAt 过期时间(可选)
ExpiresAt *time.Time `json:"expires_at,omitempty"`
}
Tenant 租户
func FromContextOrDefault ¶
FromContextOrDefault 从上下文获取租户,如果不存在则返回默认租户 这是 MustFromContext 的安全替代方案
func FromContextOrError ¶
FromContextOrError 从上下文获取租户,如果不存在则返回错误 这是 MustFromContext 的安全替代方案
func MustFromContext ¶
MustFromContext 从上下文获取租户(必须存在)
⚠️ 警告:如果上下文中不存在租户,此函数会 panic。 仅在确定租户一定存在时使用(如中间件已验证后)。 推荐使用 FromContext 进行安全检查,或使用 FromContextOrDefault 提供默认值。
使用场景:
- 在租户中间件之后的处理器中
- 在已确认租户存在的业务逻辑中
type TenantExtractor ¶
TenantExtractor 租户提取器接口
type TenantExtractorFunc ¶
TenantExtractorFunc 函数提取器
type TenantFilter ¶
type TenantFilter struct {
// Type 类型过滤
Type TenantType
// Status 状态过滤
Status TenantStatus
// Plan 计划过滤
Plan string
// Limit 限制数量
Limit int
// Offset 偏移量
Offset int
}
TenantFilter 租户过滤器
type TenantQuota ¶
type TenantQuota struct {
// MaxRequests 最大请求数/月
MaxRequests int64 `json:"max_requests"`
// MaxTokens 最大 Token 数/月
MaxTokens int64 `json:"max_tokens"`
// MaxAgents 最大 Agent 数
MaxAgents int `json:"max_agents"`
// MaxConcurrency 最大并发数
MaxConcurrency int `json:"max_concurrency"`
// MaxStorage 最大存储(字节)
MaxStorage int64 `json:"max_storage"`
// RateLimitPerMinute 每分钟请求限制
RateLimitPerMinute int `json:"rate_limit_per_minute"`
// CustomQuotas 自定义配额
CustomQuotas map[string]int64 `json:"custom_quotas,omitempty"`
}
TenantQuota 租户配额
func DefaultQuotaForType ¶
func DefaultQuotaForType(tenantType TenantType) *TenantQuota
DefaultQuotaForType 根据类型获取默认配额
type TenantStatus ¶
type TenantStatus string
TenantStatus 租户状态
const ( // TenantStatusActive 活跃 TenantStatusActive TenantStatus = "active" // TenantStatusSuspended 暂停 TenantStatusSuspended TenantStatus = "suspended" // TenantStatusInactive 不活跃 TenantStatusInactive TenantStatus = "inactive" // TenantStatusDeleted 已删除 TenantStatusDeleted TenantStatus = "deleted" )
type TenantStorage ¶
type TenantStorage interface {
// Get 获取租户
Get(ctx context.Context, id string) (*Tenant, error)
// Save 保存租户
Save(ctx context.Context, tenant *Tenant) error
// Delete 删除租户
Delete(ctx context.Context, id string) error
// List 列出租户
List(ctx context.Context, filter TenantFilter) ([]*Tenant, error)
}
TenantStorage 租户存储接口
type TenantType ¶
type TenantType string
TenantType 租户类型
const ( // TenantTypeFree 免费租户 TenantTypeFree TenantType = "free" // TenantTypeStandard 标准租户 TenantTypeStandard TenantType = "standard" // TenantTypePremium 高级租户 TenantTypePremium TenantType = "premium" // TenantTypeEnterprise 企业租户 TenantTypeEnterprise TenantType = "enterprise" )
type UsageData ¶
type UsageData struct {
// Current 当前使用量
Current int64 `json:"current"`
// Total 累计使用量
Total int64 `json:"total"`
// LastUpdated 最后更新时间
LastUpdated time.Time `json:"last_updated"`
// PeriodStart 计费周期开始
PeriodStart time.Time `json:"period_start"`
}
UsageData 使用量数据
type UsageTracker ¶
type UsageTracker struct {
// contains filtered or unexported fields
}
UsageTracker 使用量追踪器
func (*UsageTracker) GetAllUsage ¶
func (t *UsageTracker) GetAllUsage(tenantID string) map[string]*UsageData
GetAllUsage 获取租户所有使用量
func (*UsageTracker) GetUsage ¶
func (t *UsageTracker) GetUsage(tenantID, resource string) int64
GetUsage 获取使用量
func (*UsageTracker) ResetPeriod ¶
func (t *UsageTracker) ResetPeriod(tenantID string)
ResetPeriod 重置计费周期
func (*UsageTracker) Track ¶
func (t *UsageTracker) Track(tenantID, resource string, amount int64)
Track 追踪使用量