Documentation
¶
Index ¶
- Constants
- Variables
- func ContextWithAPIKey(ctx context.Context, key string) context.Context
- func ContextWithAccount(ctx context.Context, accountID string) context.Context
- func ContextWithPromptTokens(ctx context.Context, tokens *atomic.Int64) context.Context
- type AccountInfo
- type AccountPool
- func (p *AccountPool) AccountAllowed(ctx context.Context, accountID string) (exists bool, allowed bool)
- func (p *AccountPool) AccountReason(ctx context.Context, accountID string, modelID string) string
- func (p *AccountPool) AcquireForModel(ctx context.Context, sessionKey string, modelID string, ...) (*gemini.Client, string, func(), error)
- func (p *AccountPool) Add(client *gemini.Client, accountID string)
- func (p *AccountPool) BindSession(sessionKey string, accountID string)
- func (p *AccountPool) CatalogAccounts() []CatalogAccount
- func (p *AccountPool) Changed() <-chan struct{}
- func (p *AccountPool) ClearCooldown(accountID string, model string) error
- func (p *AccountPool) ClearSession(sessionKey string)
- func (p *AccountPool) Client(accountID string) *gemini.Client
- func (p *AccountPool) CoolExit(proxy string, until time.Time)
- func (p *AccountPool) Cooldowns() []Cooldown
- func (p *AccountPool) MarkFailed(accountID string, err error) bool
- func (p *AccountPool) MarkReady(accountID string)
- func (p *AccountPool) ModelAccounts() map[string]int
- func (p *AccountPool) ModelAvailability(ctx context.Context, modelID string) ModelAvailability
- func (p *AccountPool) NextForModelExcluding(sessionKey string, modelID string, excludedAccountIDs map[string]struct{}) (*gemini.Client, string)
- func (p *AccountPool) OnCaptcha(fn func(accountID string, proxy string, pooled bool))
- func (p *AccountPool) OnCooldownChange(save func([]Cooldown))
- func (p *AccountPool) OnTransportError(fn func())
- func (p *AccountPool) RecordError(accountID string, err error)
- func (p *AccountPool) Register(accountID string, info AccountInfo, disabled bool)
- func (p *AccountPool) Remove(accountID string)
- func (p *AccountPool) ReportFailure(accountID string, modelID string, err error)
- func (p *AccountPool) ReportSuccess(accountID string, modelID string)
- func (p *AccountPool) RestoreCooldowns(cooldowns []Cooldown)
- func (p *AccountPool) RetryAfter(ctx context.Context, modelID string) time.Time
- func (p *AccountPool) SessionIdle(sessionKey string, modelID string) bool
- func (p *AccountPool) SetAPIKeyAccounts(accounts map[string][]string)
- func (p *AccountPool) SetAccountOrder(accountIDs []string)
- func (p *AccountPool) SetClient(accountID string, client *gemini.Client)
- func (p *AccountPool) SetDisabled(accountID string, disabled bool)
- func (p *AccountPool) SetInfo(accountID string, info AccountInfo)
- func (p *AccountPool) SetModelAccounts(accounts map[string][]string)
- func (p *AccountPool) SetStrategy(strategy string)
- func (p *AccountPool) SetTimings(cooldown time.Duration, sessionTTL time.Duration)
- func (p *AccountPool) Size() int
- func (p *AccountPool) Status() PoolStatus
- func (p *AccountPool) TryAcquireIdle(ctx context.Context, modelID string, excludedAccountIDs map[string]struct{}, ...) (*gemini.Client, string, func())
- func (p *AccountPool) Usage(ctx context.Context, refresh bool) PoolUsageStatus
- type AccountStatus
- type AccountUsageStatus
- type CatalogAccount
- type Cooldown
- type ModelAvailability
- type ModelCooldown
- type PoolStatus
- type PoolUsageStatus
Constants ¶
const ( StateInitializing = "initializing" StateReady = "ready" StateCooldown = "cooldown" StateAuthRequired = "auth_required" StateDisabled = "disabled" )
账号状态
const ( // RoundRobin 在账号间轮流分配 RoundRobin = "round-robin" // FillFirst 优先使用排在前面且有空闲请求槽的账号 FillFirst = "fill-first" )
调度策略
const CaptchaCooldown = 2 * time.Hour
CaptchaCooldown 是出口被人机验证页拦截后停用该出口的时长
const SourceGuest = "guest"
SourceGuest 是访客会话的来源
Variables ¶
var ErrAccountNotFound = errors.New("账号不存在")
ErrAccountNotFound 表示账号池中没有该账号
Functions ¶
func ContextWithAPIKey ¶ added in v0.1.7
ContextWithAPIKey 返回带有请求所用 API key 的上下文,账号池只在该 key 绑定的账号中选号
func ContextWithAccount ¶ added in v0.2.0
ContextWithAccount 返回指定处理账号的上下文,账号池只在该账号上选号
Types ¶
type AccountInfo ¶ added in v0.1.3
type AccountInfo struct {
// Source 为 chrome(Chrome Profile 导入,可续签)、cookie(Cookie Header 导入)或 guest(未登录的访客会话)
Source string
// Profile 为 Chrome 导入时的 Profile 目录名
Profile string
// Proxy 为账号实际使用的代理 URL
Proxy string
// ProxySource 为代理来源:fixed(账号固定代理)、direct(直连)、pool(代理池)或 system(系统代理)
ProxySource string
// Path 为认证状态文件路径
Path string
}
AccountInfo 是账号的来源与出口
type AccountPool ¶
type AccountPool struct {
// contains filtered or unexported fields
}
AccountPool 在健康账号之间调度请求并维护账号状态、冷却与显式会话粘性
func NewAccountPool ¶
func NewAccountPool(cooldown time.Duration, sessionTTL time.Duration) *AccountPool
NewAccountPool 创建账号池,轮询从随机位置开始
func (*AccountPool) AccountAllowed ¶ added in v0.2.0
func (p *AccountPool) AccountAllowed(ctx context.Context, accountID string) (exists bool, allowed bool)
AccountAllowed 报告账号是否存在,以及请求所用 API key 是否可以使用它
func (*AccountPool) AccountReason ¶ added in v0.1.6
AccountReason 返回账号不能调度该模型的原因:不在请求 API key 或该模型的绑定账号中、冷却截止时间或账号状态
func (*AccountPool) AcquireForModel ¶ added in v0.1.6
func (p *AccountPool) AcquireForModel(ctx context.Context, sessionKey string, modelID string, excludedAccountIDs map[string]struct{}) (*gemini.Client, string, func(), error)
AcquireForModel 选择可调度该模型的账号并占用一个请求槽,返回客户端、账号与释放函数;同一排队键的请求按到达顺序排队,队首在任一账号空出、就绪或状态变化后重新选择,没有可调度账号时客户端为空
func (*AccountPool) Add ¶
func (p *AccountPool) Add(client *gemini.Client, accountID string)
Add 添加一个已经完成协议初始化的账号
func (*AccountPool) BindSession ¶
func (p *AccountPool) BindSession(sessionKey string, accountID string)
BindSession 将成功响应标识绑定到产生它的账号
func (*AccountPool) CatalogAccounts ¶ added in v0.1.6
func (p *AccountPool) CatalogAccounts() []CatalogAccount
CatalogAccounts 返回已就绪或已读取模型目录的账号,用于汇总模型目录
func (*AccountPool) Changed ¶ added in v0.1.6
func (p *AccountPool) Changed() <-chan struct{}
Changed 返回账号状态下一次变化或请求槽释放时关闭的通道
func (*AccountPool) ClearCooldown ¶ added in v0.1.6
func (p *AccountPool) ClearCooldown(accountID string, model string) error
ClearCooldown 清除账号的一项冷却并保存冷却记录,model 为空时清除账号级冷却,否则清除该模型的冷却
func (*AccountPool) ClearSession ¶
func (p *AccountPool) ClearSession(sessionKey string)
ClearSession 结束显式会话的账号粘性
func (*AccountPool) Client ¶ added in v0.1.3
func (p *AccountPool) Client(accountID string) *gemini.Client
Client 返回账号的协议客户端
func (*AccountPool) CoolExit ¶ added in v0.2.0
func (p *AccountPool) CoolExit(proxy string, until time.Time)
CoolExit 让经同一出口的账号与访客会话冷却到 until,原因为人机验证拦截,proxy 为出口代理 URL,直连为空
func (*AccountPool) Cooldowns ¶ added in v0.1.6
func (p *AccountPool) Cooldowns() []Cooldown
Cooldowns 返回尚未结束的账号与模型冷却,含尚未就绪账号待恢复的冷却
func (*AccountPool) MarkFailed ¶ added in v0.1.3
func (p *AccountPool) MarkFailed(accountID string, err error) bool
MarkFailed 把账号标为不可用或待认证并记录原因,返回状态是否变化
func (*AccountPool) MarkReady ¶ added in v0.1.3
func (p *AccountPool) MarkReady(accountID string)
MarkReady 把账号标为就绪并清除错误与冷却
func (*AccountPool) ModelAccounts ¶ added in v0.1.3
func (p *AccountPool) ModelAccounts() map[string]int
ModelAccounts 返回每个模型当前可调度的账号数,模型绑定了账号时只计绑定的账号
func (*AccountPool) ModelAvailability ¶ added in v0.1.6
func (p *AccountPool) ModelAvailability(ctx context.Context, modelID string) ModelAvailability
ModelAvailability 按请求 API key 与目标模型可用账号的模型目录返回该模型的调度情况
func (*AccountPool) NextForModelExcluding ¶
func (p *AccountPool) NextForModelExcluding(sessionKey string, modelID string, excludedAccountIDs map[string]struct{}) (*gemini.Client, string)
NextForModelExcluding 选择会话绑定的账号或未排除的同能力账号,会话绑定的账号被排除或不可调度时客户端为空并返回该账号
func (*AccountPool) OnCaptcha ¶ added in v0.2.0
func (p *AccountPool) OnCaptcha(fn func(accountID string, proxy string, pooled bool))
OnCaptcha 设置账号被人机验证页拦截时调用的函数,参数为账号 ID、出口代理(直连为空)与出口是否来自代理池
func (*AccountPool) OnCooldownChange ¶ added in v0.1.6
func (p *AccountPool) OnCooldownChange(save func([]Cooldown))
OnCooldownChange 登记冷却变化后保存全部冷却的函数
func (*AccountPool) OnTransportError ¶ added in v0.1.5
func (p *AccountPool) OnTransportError(fn func())
OnTransportError 设置账号遇到连接、TLS 或代理错误时调用的函数
func (*AccountPool) RecordError ¶ added in v0.1.3
func (p *AccountPool) RecordError(accountID string, err error)
RecordError 记录账号最近一次后台操作错误,不改变调度状态
func (*AccountPool) Register ¶ added in v0.1.3
func (p *AccountPool) Register(accountID string, info AccountInfo, disabled bool)
Register 登记账号,disabled 为 true 时登记为停用,否则为初始化中
func (*AccountPool) Remove ¶ added in v0.1.3
func (p *AccountPool) Remove(accountID string)
Remove 删除账号并解除其会话绑定
func (*AccountPool) ReportFailure ¶
func (p *AccountPool) ReportFailure(accountID string, modelID string, err error)
ReportFailure 按错误类型把账号或该模型移入冷却,会话失效时把账号标为待认证,网络层失败只记录错误
func (*AccountPool) ReportSuccess ¶
func (p *AccountPool) ReportSuccess(accountID string, modelID string)
ReportSuccess 清除账号与该模型的失败状态
func (*AccountPool) RestoreCooldowns ¶ added in v0.1.6
func (p *AccountPool) RestoreCooldowns(cooldowns []Cooldown)
RestoreCooldowns 恢复上次运行保存的冷却,账号尚未就绪时在就绪时恢复
func (*AccountPool) RetryAfter ¶ added in v0.1.3
RetryAfter 返回请求 API key 与目标模型可用账号中该模型最早结束冷却的时间,有未冷却的登录账号或没有冷却中的账号时为零值
func (*AccountPool) SessionIdle ¶ added in v0.1.6
func (p *AccountPool) SessionIdle(sessionKey string, modelID string) bool
SessionIdle 判断会话绑定的账号可调度该模型且有空闲请求槽
func (*AccountPool) SetAPIKeyAccounts ¶ added in v0.1.7
func (p *AccountPool) SetAPIKeyAccounts(accounts map[string][]string)
SetAPIKeyAccounts 设置各 API key 可用的账号 ID,未列出的 key 可用全部账号
func (*AccountPool) SetAccountOrder ¶ added in v0.1.9
func (p *AccountPool) SetAccountOrder(accountIDs []string)
SetAccountOrder 设置 fill-first 依次使用的账号 ID,未列出的账号排在后面并保持登记顺序
func (*AccountPool) SetClient ¶ added in v0.1.3
func (p *AccountPool) SetClient(accountID string, client *gemini.Client)
SetClient 设置账号的协议客户端
func (*AccountPool) SetDisabled ¶ added in v0.1.3
func (p *AccountPool) SetDisabled(accountID string, disabled bool)
SetDisabled 停用或启用账号,启用后账号回到初始化中
func (*AccountPool) SetInfo ¶ added in v0.2.0
func (p *AccountPool) SetInfo(accountID string, info AccountInfo)
SetInfo 更新账号的来源、代理与认证文件信息,不改变账号状态
func (*AccountPool) SetModelAccounts ¶ added in v0.1.9
func (p *AccountPool) SetModelAccounts(accounts map[string][]string)
SetModelAccounts 设置各模型可用的账号 ID,未列出的模型可用全部账号
func (*AccountPool) SetStrategy ¶ added in v0.1.3
func (p *AccountPool) SetStrategy(strategy string)
SetStrategy 修改无会话请求的调度策略
func (*AccountPool) SetTimings ¶ added in v0.1.3
func (p *AccountPool) SetTimings(cooldown time.Duration, sessionTTL time.Duration)
SetTimings 修改失败冷却时长与显式会话存活时长
func (*AccountPool) TryAcquireIdle ¶ added in v0.1.7
func (p *AccountPool) TryAcquireIdle(ctx context.Context, modelID string, excludedAccountIDs map[string]struct{}, fallback string) (*gemini.Client, string, func())
TryAcquireIdle 立即占用一个没有进行中请求、可调度该模型且未排除的账号的请求槽,都没有时再试 fallback 账号的其余请求槽;没有可用请求槽时客户端为空
func (*AccountPool) Usage ¶
func (p *AccountPool) Usage(ctx context.Context, refresh bool) PoolUsageStatus
Usage 返回各账号最近读取的官网用量,refresh 为 true 时并发重新读取
type AccountStatus ¶
type AccountStatus struct {
AccountID string `json:"account_id"`
State string `json:"state"`
Enabled bool `json:"enabled"`
Source string `json:"source,omitempty"`
Profile string `json:"profile,omitempty"`
Proxy string `json:"proxy,omitempty"`
ProxySource string `json:"proxy_source,omitempty"`
FixedProxy string `json:"fixed_proxy,omitempty"`
Tier string `json:"tier,omitempty"`
ContextWindow int `json:"context_window,omitempty"`
Models []string `json:"models"`
Pending int `json:"pending"`
LastUsed *time.Time `json:"last_used,omitempty"`
ReadyAt *time.Time `json:"ready_at,omitempty"`
CooldownUntil *time.Time `json:"cooldown_until,omitempty"`
CooldownReason string `json:"cooldown_reason,omitempty"`
ModelCooldowns []ModelCooldown `json:"model_cooldowns,omitempty"`
ConsecutiveFails int `json:"consecutive_failures"`
LastError string `json:"last_error,omitempty"`
LastErrorAt *time.Time `json:"last_error_at,omitempty"`
Usage *gemini.UsageInfo `json:"usage,omitempty"`
}
AccountStatus 是账号的调度状态、层级、冷却与最近错误
type AccountUsageStatus ¶
type AccountUsageStatus struct {
AccountID string `json:"account_id"`
State string `json:"state"`
Usage *gemini.UsageInfo `json:"usage,omitempty"`
Error string `json:"error,omitempty"`
}
AccountUsageStatus 表示单个账号的官网用量
type CatalogAccount ¶ added in v0.1.6
type CatalogAccount struct {
ID string
Client *gemini.Client
Ready bool
// Guest 表示访客会话,只在没有可用账号时处理请求
Guest bool
}
CatalogAccount 是一个已就绪或已读取模型目录的账号,Ready 表示已就绪
type ModelAvailability ¶ added in v0.1.6
type ModelAvailability struct {
// Catalog 表示至少一个账号已就绪或已读取模型目录
Catalog bool
// Known 表示至少一个账号的模型目录含该模型
Known bool
// Ready 表示有可调度该模型的账号
Ready bool
// Reasons 为不可调度时支持该模型的各账号的原因
Reasons []string
// Pending 为尚未读取模型目录的未停用账号的状态
Pending []string
}
ModelAvailability 是目标模型在账号池中的调度情况
type ModelCooldown ¶ added in v0.1.3
type ModelCooldown struct {
Model string `json:"model"`
Until time.Time `json:"until"`
Reason string `json:"reason"`
}
ModelCooldown 是单个模型的冷却
type PoolStatus ¶
type PoolStatus struct {
Total int `json:"total"`
Available int `json:"available"`
CoolingDown int `json:"cooling_down"`
Initializing int `json:"initializing"`
AuthRequired int `json:"auth_required"`
Disabled int `json:"disabled"`
Accounts []AccountStatus `json:"accounts"`
// Guest 是访客会话的状态,不计入账号数
Guest *AccountStatus `json:"guest,omitempty"`
}
PoolStatus 汇总账号池状态
func (PoolStatus) Ready ¶ added in v0.1.3
func (s PoolStatus) Ready() bool
Ready 报告是否有就绪的账号或访客会话可以接收请求
type PoolUsageStatus ¶
type PoolUsageStatus struct {
Accounts []AccountUsageStatus `json:"accounts"`
}
PoolUsageStatus 汇总账号池官网用量