balancer

package
v0.1.8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 9, 2026 License: MIT Imports: 13 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

View Source
const (
	StateInitializing = "initializing"
	StateReady        = "ready"
	StateCooldown     = "cooldown"
	StateAuthRequired = "auth_required"
	StateUnavailable  = "unavailable"
	StateDisabled     = "disabled"
)

账号状态

View Source
const (
	// RoundRobin 在账号间轮流分配
	RoundRobin = "round-robin"
	// FillFirst 优先使用排在前面且有空闲请求槽的账号
	FillFirst = "fill-first"
)

调度策略

View Source
const SourceGuest = "guest"

SourceGuest 是访客会话的来源

Variables

View Source
var ErrAccountNotFound = errors.New("账号不存在")

ErrAccountNotFound 表示账号池中没有该账号

Functions

func ContextWithAPIKey added in v0.1.7

func ContextWithAPIKey(ctx context.Context, key string) context.Context

ContextWithAPIKey 返回带有请求所用 API key 的上下文,账号池只在该 key 绑定的账号中选号

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) AccountReason added in v0.1.6

func (p *AccountPool) AccountReason(ctx context.Context, accountID string, modelID string) string

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) 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) 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

func (p *AccountPool) RetryAfter(ctx context.Context, modelID string) time.Time

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) 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) SetProxy added in v0.1.3

func (p *AccountPool) SetProxy(accountID string, proxy string)

SetProxy 更新账号显示的代理

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) Size

func (p *AccountPool) Size() int

Size 返回当前可接收请求的账号与访客会话数量

func (*AccountPool) Status

func (p *AccountPool) Status() PoolStatus

Status 返回账号池状态快照

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"`
	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
}

CatalogAccount 是一个已就绪或已读取模型目录的账号,Ready 表示已就绪

type Cooldown added in v0.1.6

type Cooldown struct {
	Account string
	Model   string
	Until   time.Time
	Reason  string
}

Cooldown 是一个账号或其某个模型的冷却,Model 为空表示整个账号

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"`
	Unavailable  int             `json:"unavailable"`
	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 汇总账号池官网用量

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL