Documentation
¶
Index ¶
- type BudgetData
- type BudgetStore
- type DeductResult
- type EntityWiseBudgets
- type MemoryBudgetStore
- func (s *MemoryBudgetStore) Check(ctx context.Context, budgetID string, cost float64) (bool, float64, error)
- func (s *MemoryBudgetStore) Close() error
- func (s *MemoryBudgetStore) CollectBillingBudgets(ctx context.Context, vk *VKHierarchyData) (EntityWiseBudgets, error)
- func (s *MemoryBudgetStore) Deduct(ctx context.Context, budgetID string, cost float64) (*DeductResult, error)
- func (s *MemoryBudgetStore) Delete(ctx context.Context, budgetID string) error
- func (s *MemoryBudgetStore) Get(ctx context.Context, budgetID string) (*BudgetData, error)
- func (s *MemoryBudgetStore) GetAllBudgetIDs(ctx context.Context) ([]string, error)
- func (s *MemoryBudgetStore) GetBudgetIDsByEntity(ctx context.Context, entityType string, entityID string) ([]string, error)
- func (s *MemoryBudgetStore) GetVKHierarchy(ctx context.Context, vkToken string) (*VKHierarchyData, error)
- func (s *MemoryBudgetStore) MGet(ctx context.Context, budgetIDs []string) (map[string]*BudgetData, error)
- func (s *MemoryBudgetStore) Ping(ctx context.Context) error
- func (s *MemoryBudgetStore) Reset(ctx context.Context, budgetID string) error
- func (s *MemoryBudgetStore) Set(ctx context.Context, budgetID string, budget *configstoreTables.TableBudget) error
- func (s *MemoryBudgetStore) SetBudgetIDsByEntity(ctx context.Context, entityType string, entityID string, budgetIDs []string) error
- func (s *MemoryBudgetStore) SetVKHierarchy(ctx context.Context, vkToken string, data *VKHierarchyData) error
- type RedisBudgetStore
- func (s *RedisBudgetStore) Check(ctx context.Context, budgetID string, cost float64) (bool, float64, error)
- func (s *RedisBudgetStore) Close() error
- func (s *RedisBudgetStore) CollectBillingBudgets(ctx context.Context, vk *VKHierarchyData) (EntityWiseBudgets, error)
- func (s *RedisBudgetStore) Deduct(ctx context.Context, budgetID string, cost float64) (*DeductResult, error)
- func (s *RedisBudgetStore) Delete(ctx context.Context, budgetID string) error
- func (s *RedisBudgetStore) Get(ctx context.Context, budgetID string) (*BudgetData, error)
- func (s *RedisBudgetStore) GetAllBudgetIDs(ctx context.Context) ([]string, error)
- func (s *RedisBudgetStore) GetBudgetIDsByEntity(ctx context.Context, entityType string, entityID string) ([]string, error)
- func (s *RedisBudgetStore) GetVKHierarchy(ctx context.Context, vkToken string) (*VKHierarchyData, error)
- func (s *RedisBudgetStore) MGet(ctx context.Context, budgetIDs []string) (map[string]*BudgetData, error)
- func (s *RedisBudgetStore) Ping(ctx context.Context) error
- func (s *RedisBudgetStore) Reset(ctx context.Context, budgetID string) error
- func (s *RedisBudgetStore) Set(ctx context.Context, budgetID string, data *configstoreTables.TableBudget) error
- func (s *RedisBudgetStore) SetBudgetIDsByEntity(ctx context.Context, entityType string, entityID string, budgetIDs []string) error
- func (s *RedisBudgetStore) SetVKHierarchy(ctx context.Context, vkToken string, data *VKHierarchyData) error
- type RedisConfig
- type VKHierarchyData
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type BudgetData ¶
type BudgetData configstoreTables.TableBudget
BudgetData 表示一个 billing budget 的完整数据 存储在 Redis Hash 或内存 map 中
func (BudgetData) EntityType ¶
func (b BudgetData) EntityType() string
EntityType returns the entity type ("team", "customer", "user") for a budget
type BudgetStore ¶
type BudgetStore interface {
// Check 检查是否存在有余额的budget不扣费)
Check(ctx context.Context, budgetID string, cost float64) (bool, float64, error)
// Deduct 不检查直接扣除
Deduct(ctx context.Context, budgetID string, cost float64) (*DeductResult, error)
// Get 获取 budget 数据
Get(ctx context.Context, budgetID string) (*BudgetData, error)
// MGet 批量获取 budget 数据
MGet(ctx context.Context, budgetIDs []string) (map[string]*BudgetData, error)
// Set 更新 budget 配置(admin 改 max_limit 等)
Set(ctx context.Context, budgetID string, data *configstoreTables.TableBudget) error
// Delete 删除 budget
Delete(ctx context.Context, budgetID string) error
// Reset 重置 budget usage(周期重置)
Reset(ctx context.Context, budgetID string) error
// SetVKHierarchy 设置 VK 层级缓存
SetVKHierarchy(ctx context.Context, vkToken string, data *VKHierarchyData) error
// GetVKHierarchy 获取 VK 层级缓存
GetVKHierarchy(ctx context.Context, vkToken string) (*VKHierarchyData, error)
// SetBudgetIDsByEntity 设置实体(VK/Team/Customer/User)关联的 billing budget ID 列表
SetBudgetIDsByEntity(ctx context.Context, entityType string, entityID string, budgetIDs []string) error
// GetBudgetIDsByEntity 获取实体关联的 billing budget ID 列表
GetBudgetIDsByEntity(ctx context.Context, entityType string, entityID string) ([]string, error)
// CollectBillingBudgets 收集 VK 层级的所有 billing budget 数据
// 通过 VK → Team/Customer/User → Budget 层级关系收集
CollectBillingBudgets(ctx context.Context, vk *VKHierarchyData) (EntityWiseBudgets, error)
// GetAllBudgetIDs 获取所有 billing budget ID(用于后台 worker 遍历)
GetAllBudgetIDs(ctx context.Context) ([]string, error)
// Ping 检查存储是否可用
Ping(ctx context.Context) error
// Close 关闭存储连接
Close() error
}
BudgetStore 是 billing 插件的存储抽象 Redis 实现保证 check+deduct 的原子性(Lua 脚本) 内存实现用于测试和单实例 fallback
type DeductResult ¶
type DeductResult struct {
Allowed bool `json:"allowed"` // 是否允许(check+deduct合一)
CurrentUsage float64 `json:"current_usage"` // 扣费后的 usage
MaxLimit float64 `json:"max_limit"` // budget 限额
}
DeductResult 是 Deduct 的返回值
type EntityWiseBudgets ¶
type EntityWiseBudgets map[string][]*BudgetData
type MemoryBudgetStore ¶
type MemoryBudgetStore struct {
// contains filtered or unexported fields
}
MemoryBudgetStore implements BudgetStore using in-memory data structures. It is intended for testing and as a fallback when Redis is unavailable. Atomicity is achieved via per-budget fine-grained mutexes (CAS-style).
func NewMemoryBudgetStore ¶
func NewMemoryBudgetStore(logger schemas.Logger) *MemoryBudgetStore
NewMemoryBudgetStore creates a new in-memory BudgetStore.
func (*MemoryBudgetStore) Check ¶
func (s *MemoryBudgetStore) Check(ctx context.Context, budgetID string, cost float64) (bool, float64, error)
Check checks whether the budget has enough remaining capacity (no deduction).
func (*MemoryBudgetStore) Close ¶
func (s *MemoryBudgetStore) Close() error
Close releases resources (no-op for in-memory store).
func (*MemoryBudgetStore) CollectBillingBudgets ¶
func (s *MemoryBudgetStore) CollectBillingBudgets(ctx context.Context, vk *VKHierarchyData) (EntityWiseBudgets, error)
CollectBillingBudgets collects all billing budgets for a VK hierarchy. Budgets are filtered by entity ownership:
- user budgets: only budgets whose (team_id or customer_id) matches the VK's context
- team budgets: budgets owned by the VK's team
- customer budgets: budgets owned by the VK's customer
func (*MemoryBudgetStore) Deduct ¶
func (s *MemoryBudgetStore) Deduct(ctx context.Context, budgetID string, cost float64) (*DeductResult, error)
Deduct atomically checks and deducts cost from the budget. Uses a per-budget mutex to guarantee check+deduct atomicity.
func (*MemoryBudgetStore) Delete ¶
func (s *MemoryBudgetStore) Delete(ctx context.Context, budgetID string) error
Delete removes a budget and cleans up entity associations and the global ID set.
func (*MemoryBudgetStore) Get ¶
func (s *MemoryBudgetStore) Get(ctx context.Context, budgetID string) (*BudgetData, error)
Get retrieves a single budget by ID.
func (*MemoryBudgetStore) GetAllBudgetIDs ¶
func (s *MemoryBudgetStore) GetAllBudgetIDs(ctx context.Context) ([]string, error)
GetAllBudgetIDs returns all known billing budget IDs.
func (*MemoryBudgetStore) GetBudgetIDsByEntity ¶
func (s *MemoryBudgetStore) GetBudgetIDsByEntity(ctx context.Context, entityType string, entityID string) ([]string, error)
GetBudgetIDsByEntity retrieves the budget IDs associated with an entity.
func (*MemoryBudgetStore) GetVKHierarchy ¶
func (s *MemoryBudgetStore) GetVKHierarchy(ctx context.Context, vkToken string) (*VKHierarchyData, error)
GetVKHierarchy retrieves cached VK hierarchy data for a given VK token.
func (*MemoryBudgetStore) MGet ¶
func (s *MemoryBudgetStore) MGet(ctx context.Context, budgetIDs []string) (map[string]*BudgetData, error)
MGet retrieves multiple budgets by ID.
func (*MemoryBudgetStore) Ping ¶
func (s *MemoryBudgetStore) Ping(ctx context.Context) error
Ping checks if the store is available (always nil for in-memory store).
func (*MemoryBudgetStore) Reset ¶
func (s *MemoryBudgetStore) Reset(ctx context.Context, budgetID string) error
Reset resets the budget's current usage to zero and updates LastReset.
func (*MemoryBudgetStore) Set ¶
func (s *MemoryBudgetStore) Set(ctx context.Context, budgetID string, budget *configstoreTables.TableBudget) error
Set stores or updates a budget. It also registers the budget ID and entity associations.
func (*MemoryBudgetStore) SetBudgetIDsByEntity ¶
func (s *MemoryBudgetStore) SetBudgetIDsByEntity(ctx context.Context, entityType string, entityID string, budgetIDs []string) error
SetBudgetIDsByEntity associates a set of budget IDs with an entity (e.g., team, customer, user).
func (*MemoryBudgetStore) SetVKHierarchy ¶
func (s *MemoryBudgetStore) SetVKHierarchy(ctx context.Context, vkToken string, data *VKHierarchyData) error
SetVKHierarchy caches VK hierarchy data for a given VK token.
type RedisBudgetStore ¶
type RedisBudgetStore struct {
// contains filtered or unexported fields
}
RedisBudgetStore implements BudgetStore using Redis.
func NewRedisBudgetStore ¶
func NewRedisBudgetStore(ctx context.Context, logger schemas.Logger, redisCli redis.Cmdable) (*RedisBudgetStore, error)
NewRedisBudgetStore creates a new Redis-backed BudgetStore.
func (*RedisBudgetStore) Check ¶
func (s *RedisBudgetStore) Check(ctx context.Context, budgetID string, cost float64) (bool, float64, error)
Check checks if the budget has enough capacity without deducting.
func (*RedisBudgetStore) Close ¶
func (s *RedisBudgetStore) Close() error
Close closes the Redis connection.
func (*RedisBudgetStore) CollectBillingBudgets ¶
func (s *RedisBudgetStore) CollectBillingBudgets(ctx context.Context, vk *VKHierarchyData) (EntityWiseBudgets, error)
CollectBillingBudgets collects all billing budgets for a VK hierarchy. Budgets are filtered by entity ownership:
- user budgets: only budgets whose (team_id or customer_id) matches the VK's context
- team budgets: budgets owned by the VK's team
- customer budgets: budgets owned by the VK's customer
func (*RedisBudgetStore) Deduct ¶
func (s *RedisBudgetStore) Deduct(ctx context.Context, budgetID string, cost float64) (*DeductResult, error)
Deduct atomically checks and deducts budget usage via Lua script. Returns DeductResult with Allowed=true if deduction succeeded, Allowed=false if over limit (not an error), and error only on Redis failures.
func (*RedisBudgetStore) Delete ¶
func (s *RedisBudgetStore) Delete(ctx context.Context, budgetID string) error
Delete removes a budget from Redis.
func (*RedisBudgetStore) Get ¶
func (s *RedisBudgetStore) Get(ctx context.Context, budgetID string) (*BudgetData, error)
Get retrieves a single budget by ID.
func (*RedisBudgetStore) GetAllBudgetIDs ¶
func (s *RedisBudgetStore) GetAllBudgetIDs(ctx context.Context) ([]string, error)
GetAllBudgetIDs returns all billing budget IDs.
func (*RedisBudgetStore) GetBudgetIDsByEntity ¶
func (s *RedisBudgetStore) GetBudgetIDsByEntity(ctx context.Context, entityType string, entityID string) ([]string, error)
GetBudgetIDsByEntity retrieves the list of budget IDs associated with an entity.
func (*RedisBudgetStore) GetVKHierarchy ¶
func (s *RedisBudgetStore) GetVKHierarchy(ctx context.Context, vkToken string) (*VKHierarchyData, error)
GetVKHierarchy retrieves cached VK hierarchy data from Redis.
func (*RedisBudgetStore) MGet ¶
func (s *RedisBudgetStore) MGet(ctx context.Context, budgetIDs []string) (map[string]*BudgetData, error)
MGet retrieves multiple budgets by IDs using a pipeline.
func (*RedisBudgetStore) Ping ¶
func (s *RedisBudgetStore) Ping(ctx context.Context) error
Ping checks if the Redis connection is alive.
func (*RedisBudgetStore) Reset ¶
func (s *RedisBudgetStore) Reset(ctx context.Context, budgetID string) error
Reset resets a budget's current usage to zero.
func (*RedisBudgetStore) Set ¶
func (s *RedisBudgetStore) Set(ctx context.Context, budgetID string, data *configstoreTables.TableBudget) error
Set stores budget data in Redis.
func (*RedisBudgetStore) SetBudgetIDsByEntity ¶
func (s *RedisBudgetStore) SetBudgetIDsByEntity(ctx context.Context, entityType string, entityID string, budgetIDs []string) error
SetBudgetIDsByEntity sets the list of budget IDs associated with an entity.
func (*RedisBudgetStore) SetVKHierarchy ¶
func (s *RedisBudgetStore) SetVKHierarchy(ctx context.Context, vkToken string, data *VKHierarchyData) error
SetVKHierarchy caches VK hierarchy data in Redis.
type RedisConfig ¶
RedisConfig holds Redis connection configuration.