mirror of
https://github.com/Wei-Shaw/sub2api.git
synced 2026-10-16 07:33:06 +08:00
Group pricing (rate multiplier, peak windows, per-user overrides) and account cost (accounts.rate_multiplier) already live side by side, but nothing stops the scheduler from handing a request to an account whose cost multiplier exceeds what the group's pricing can profitably serve. Add an opt-in per-group profit gate that filters scheduling candidates by a margin rule, while ordering, scoring, stickiness and breakers keep working unchanged among qualified accounts. Admission rule: an account qualifies iff U <= D * (1 - min_margin - safety_buffer) within a small relative epsilon, where U is accounts.rate_multiplier (0 is legal; missing/negative/NaN/Inf are conservatively rejected as invalid) and D is the requester's effective downstream multiplier (user-group override ?? group default, times the group peak factor) frozen at the request's pricing instant. - groups gain profit_control_enabled / profit_min_margin / profit_safety_buffer (migration 191); the durable auth-cache invalidation trigger additionally watches the profit and pricing columns (migration 192) so out-of-band group edits cannot leave stale auth snapshots; GetByKeyForAuth explicitly projects the new columns and the API-key auth snapshot version is bumped to force a refresh of pre-existing snapshots - request-level pricing instant: token entry points install pricingAt into ctx; the profit threshold D and the RecordUsage peak factor read the same instant, so one request never changes price mid-flight across waits/retries/failover (media and unwired paths keep the existing record-time semantics) - the gate covers token requests on openai, anthropic, gemini, grok and antigravity groups: OpenAI-family handlers via WithOpenAIRequestPricingContext (responses incl. WS bridge, chat completions, messages, embeddings, alpha search), the shared gateway via WithGatewayTokenRequestPricing (messages, chat completions, responses, gemini model actions); composite groups cannot enable it directly; image/video/models/usage/count_tokens stay ungated and an explicit image-generation intent suppresses the gate end to end - post-slot recheck: after a slot is acquired the account is re-read via SchedulerSnapshotService.GetAccount (scheduler cache, then DB; only when both fail the check fails open with WARN + metric); a vetoed account releases its slot and joins the request's exclusion set for reselection; sticky bindings are written only after the final check passes, and an over-threshold sticky account is skipped, not deleted, so it comes back once its rate recovers - sticky-session cache contract: GatewayCache.GetSessionAccountID now returns ErrStickySessionNotFound on a miss (mapped from redis.Nil in the repository implementation, mirroring ErrRefreshTokenNotFound) so the profit sticky path can distinguish "no binding yet" from a real read failure without importing the cache driver in service code - cross-group re-entry (composite parent -> member group) resolves the gate against the member group and clears a stale parent gate instead of letting a foreign threshold veto accounts - per-platform/group activity counters (installs, threshold vetoes, invalid-rate vetoes, refresh failures) for observability - admin UI: profit-control section on the five platforms' group forms with percent input, validation and platform-switch reset; group create/update/duplicate normalize and validate the config at a single choke point - cmd/profit-preview: offline what-if tool that replays the production admission semantics over an exported config/account/override/model dump, reports per-model admitted-account counts under the default and the worst-case (lowest user override) D, and surfaces probe-sync staleness as warnings without affecting admission Tests: service unit coverage for gate resolution/veto/threshold epsilon/pricing instant/suppress marker/scheduler filtering and post-slot recheck (incl. -race on the profit surface), unit-tagged handler slot-recheck and capability-mapping regressions, sqlmock and real-PostgreSQL integration regressions for the GetByKeyForAuth projection and the migration-192 trigger watch list, API contract update, and frontend specs for the five-platform form helpers.
131 lines
7.2 KiB
Go
131 lines
7.2 KiB
Go
package service
|
|
|
|
import "time"
|
|
|
|
// APIKeyAuthSnapshot API Key 认证缓存快照(仅包含认证所需字段)
|
|
type APIKeyAuthSnapshot struct {
|
|
Version int `json:"version"`
|
|
APIKeyID int64 `json:"api_key_id"`
|
|
UserID int64 `json:"user_id"`
|
|
GroupID *int64 `json:"group_id,omitempty"`
|
|
Name string `json:"name"`
|
|
Status string `json:"status"`
|
|
IPWhitelist []string `json:"ip_whitelist,omitempty"`
|
|
IPBlacklist []string `json:"ip_blacklist,omitempty"`
|
|
User APIKeyAuthUserSnapshot `json:"user"`
|
|
Group *APIKeyAuthGroupSnapshot `json:"group,omitempty"`
|
|
|
|
// Quota fields for API Key independent quota feature
|
|
Quota float64 `json:"quota"` // Quota limit in USD (0 = unlimited)
|
|
QuotaUsed float64 `json:"quota_used"` // Used quota amount
|
|
|
|
// Expiration field for API Key expiration feature
|
|
ExpiresAt *time.Time `json:"expires_at,omitempty"` // Expiration time (nil = never expires)
|
|
|
|
// Rate limit configuration (only limits, not usage - usage read from Redis at check time)
|
|
RateLimit5h float64 `json:"rate_limit_5h"`
|
|
RateLimit1d float64 `json:"rate_limit_1d"`
|
|
RateLimit7d float64 `json:"rate_limit_7d"`
|
|
}
|
|
|
|
// APIKeyAuthUserSnapshot 用户快照
|
|
type APIKeyAuthUserSnapshot struct {
|
|
ID int64 `json:"id"`
|
|
Status string `json:"status"`
|
|
Role string `json:"role"`
|
|
Balance float64 `json:"balance"`
|
|
Concurrency int `json:"concurrency"`
|
|
AllowedGroups []int64 `json:"allowed_groups,omitempty"`
|
|
|
|
// Balance notification fields (required for CheckBalanceAfterDeduction)
|
|
Email string `json:"email"`
|
|
Username string `json:"username"`
|
|
BalanceNotifyEnabled bool `json:"balance_notify_enabled"`
|
|
BalanceNotifyThresholdType string `json:"balance_notify_threshold_type"`
|
|
BalanceNotifyThreshold *float64 `json:"balance_notify_threshold,omitempty"`
|
|
BalanceNotifyExtraEmails []NotifyEmailEntry `json:"balance_notify_extra_emails,omitempty"`
|
|
TotalRecharged float64 `json:"total_recharged"`
|
|
|
|
// RPMLimit 用户级每分钟请求数上限(0 = 不限制);用于 billing_cache_service.checkRPM 兜底判断。
|
|
RPMLimit int `json:"rpm_limit"`
|
|
|
|
// UserGroupRPMOverride 该 API Key 对应的 (user, group) 专属 RPM 覆盖值。
|
|
// nil = 无 override(回退到 group/user 级);0 = 不限流;>0 = 专属上限。
|
|
UserGroupRPMOverride *int `json:"user_group_rpm_override,omitempty"`
|
|
}
|
|
|
|
// APIKeyAuthGroupSnapshot 分组快照
|
|
type APIKeyAuthGroupSnapshot struct {
|
|
ID int64 `json:"id"`
|
|
Name string `json:"name"`
|
|
Platform string `json:"platform"`
|
|
IsExclusive bool `json:"is_exclusive"`
|
|
Status string `json:"status"`
|
|
SubscriptionType string `json:"subscription_type"`
|
|
RateMultiplier float64 `json:"rate_multiplier"`
|
|
DailyLimitUSD *float64 `json:"daily_limit_usd,omitempty"`
|
|
WeeklyLimitUSD *float64 `json:"weekly_limit_usd,omitempty"`
|
|
MonthlyLimitUSD *float64 `json:"monthly_limit_usd,omitempty"`
|
|
AllowImageGeneration bool `json:"allow_image_generation"`
|
|
AllowBatchImageGeneration bool `json:"allow_batch_image_generation"`
|
|
ImageRateIndependent bool `json:"image_rate_independent"`
|
|
ImageRateMultiplier float64 `json:"image_rate_multiplier"`
|
|
ImagePrice1K *float64 `json:"image_price_1k,omitempty"`
|
|
ImagePrice2K *float64 `json:"image_price_2k,omitempty"`
|
|
ImagePrice4K *float64 `json:"image_price_4k,omitempty"`
|
|
VideoRateIndependent bool `json:"video_rate_independent"`
|
|
VideoRateMultiplier float64 `json:"video_rate_multiplier"`
|
|
VideoPrice480P *float64 `json:"video_price_480p,omitempty"`
|
|
VideoPrice720P *float64 `json:"video_price_720p,omitempty"`
|
|
VideoPrice1080P *float64 `json:"video_price_1080p,omitempty"`
|
|
WebSearchPricePerCall *float64 `json:"web_search_price_per_call,omitempty"`
|
|
ClaudeCodeOnly bool `json:"claude_code_only"`
|
|
FallbackGroupID *int64 `json:"fallback_group_id,omitempty"`
|
|
FallbackGroupIDOnInvalidRequest *int64 `json:"fallback_group_id_on_invalid_request,omitempty"`
|
|
|
|
// Model routing is used by gateway account selection, so it must be part of auth cache snapshot.
|
|
// Only anthropic groups use these fields; others may leave them empty.
|
|
ModelRouting map[string][]int64 `json:"model_routing,omitempty"`
|
|
ModelRoutingEnabled bool `json:"model_routing_enabled"`
|
|
MCPXMLInject bool `json:"mcp_xml_inject"`
|
|
|
|
// 支持的模型系列(仅 antigravity 平台使用)
|
|
SupportedModelScopes []string `json:"supported_model_scopes,omitempty"`
|
|
|
|
// OpenAI Messages 调度配置(仅 openai 平台使用)
|
|
AllowMessagesDispatch bool `json:"allow_messages_dispatch"`
|
|
AllowLive bool `json:"allow_live"`
|
|
DefaultMappedModel string `json:"default_mapped_model,omitempty"`
|
|
MessagesDispatchModelConfig OpenAIMessagesDispatchModelConfig `json:"messages_dispatch_model_config,omitempty"`
|
|
ModelsListConfig GroupModelsListConfig `json:"models_list_config,omitempty"`
|
|
|
|
// RPMLimit 分组级每分钟请求数上限(0 = 不限制);用于 billing_cache_service.checkRPM 级联判断。
|
|
RPMLimit int `json:"rpm_limit"`
|
|
|
|
// MaxReasoningEffort OpenAI/Codex 请求的推理强度上限,空字符串表示不限制。
|
|
MaxReasoningEffort string `json:"max_reasoning_effort,omitempty"`
|
|
// ReasoningEffortMappings rewrites explicit effort values before the ceiling.
|
|
ReasoningEffortMappings []ReasoningEffortMapping `json:"reasoning_effort_mappings"`
|
|
|
|
// 高峰时段倍率:PeakRateEnabled 为 true 且请求时刻处于 [PeakStart, PeakEnd) 时,
|
|
// token 计费倍率额外乘以 PeakRateMultiplier(详见 Group.PeakMultiplierAt)。
|
|
// 必须随快照缓存,否则扣费路径拿到的 apiKey.Group 缺字段、高峰倍率失效。
|
|
PeakRateEnabled bool `json:"peak_rate_enabled"`
|
|
PeakStart string `json:"peak_start"`
|
|
PeakEnd string `json:"peak_end"`
|
|
PeakRateMultiplier float64 `json:"peak_rate_multiplier"`
|
|
|
|
// 分组利润控制:调度准入门按 schedulerSnapshot 实时读取分组配置,
|
|
// 不依赖本快照;此处随快照缓存只为保证 apiKey.Group 字段完整,
|
|
// 任何消费方都不会读到误导性的零值。
|
|
ProfitControlEnabled bool `json:"profit_control_enabled"`
|
|
ProfitMinMargin float64 `json:"profit_min_margin"`
|
|
ProfitSafetyBuffer float64 `json:"profit_safety_buffer"`
|
|
}
|
|
|
|
// APIKeyAuthCacheEntry 缓存条目,支持负缓存
|
|
type APIKeyAuthCacheEntry struct {
|
|
NotFound bool `json:"not_found"`
|
|
Snapshot *APIKeyAuthSnapshot `json:"snapshot,omitempty"`
|
|
}
|