balancer

package
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Oct 15, 2026 License: MIT Imports: 8 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

This section is empty.

Functions

This section is empty.

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

func (p *AccountPool) HasModel(modelID string) bool

HasModel 报告已就绪账号是否发现目标模型

func (*AccountPool) HasReadyModel

func (p *AccountPool) HasReadyModel(modelID string) bool

HasReadyModel 报告当前是否有可调度账号可以运行目标模型

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

func (p *AccountPool) NextForModel(sessionKey string, modelID string) (*gemini.Client, string)

NextForModel 为新请求选择真正支持目标模型的健康账号

func (*AccountPool) NextForModelExcluding

func (p *AccountPool) NextForModelExcluding(sessionKey string, modelID string, excludedAccountIDs map[string]struct{}) (*gemini.Client, string)

NextForModelExcluding 为无会话重试选择未排除的同能力账号

func (*AccountPool) ReadyAccounts added in v0.1.3

func (p *AccountPool) ReadyAccounts() []ReadyAccount

ReadyAccounts 返回已就绪账号,用于汇总模型目录

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

func (p *AccountPool) RetryAfter(modelID string) time.Time

RetryAfter 返回目标模型最早结束冷却的时间,没有冷却中的账号时为零值

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) 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"`
	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 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 汇总账号池官网用量

type ReadyAccount added in v0.1.3

type ReadyAccount struct {
	ID     string
	Client *gemini.Client
}

ReadyAccount 是一个已就绪账号的 ID 与客户端

Jump to

Keyboard shortcuts

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