affinity

package
v0.0.21 Latest Latest
Warning

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

Go to latest
Published: Sep 12, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package affinity 实现 agent 本地的渠道缓存粘性:把 (用户, API token, 真实模型) 上次发生 缓存活动的上游 channel 记下来,供 plan 阶段优先选用。叶子包,只依赖 state(取 ChannelSource)+ settings + utils,不反向 import plan/publish/exec。

Index

Constants

View Source
const (
	StatusHit      = "hit"
	StatusFallback = "fallback"
	StatusNone     = "none"
)

AffinityStatus values reported in usage logs.

Variables

This section is empty.

Functions

This section is empty.

Types

type ConfigReader

type ConfigReader interface {
	Settings() settings.AgentSettings
}

ConfigReader 读当前同步配置快照。由 app.AgentCache(cache.Store)结构化满足, affinity 借此读 AffinityEnabled / AffinityTTLSec 而不 import app(避免成环)。

type Decision

type Decision struct {
	Record bool // 是否记录粘性
	Apply  bool // 是否应用粘性(重排置顶)
}

Decision 拆 Record/Apply 两个布尔,为"只记录不应用 / 只应用不记录"等反向配置留口。

type Engine

type Engine struct {
	// contains filtered or unexported fields
}

Engine 是注入到 plan/publish/exec 的门面,组合 Store + Policy + ConfigReader。

func New

func New(cfg ConfigReader) *Engine

New 用全局策略装配 Engine。cfg 提供开关与 TTL。

func (*Engine) Decide

func (e *Engine) Decide(s Subject) Decision

Decide 转交策略。

func (*Engine) Forget

func (e *Engine) Forget(k Key)

Forget 剔除(粘性 channel 硬失败时调用)。

func (*Engine) Lookup

func (e *Engine) Lookup(k Key) (Entry, bool)

Lookup 查粘性记录。

func (*Engine) Remember

func (e *Engine) Remember(k Key, src state.ChannelSource, sourceID uint, ttlSecOverride *int)

Remember 记录/续期;解析后 TTL<=0 视为关闭,不写。ttlSecOverride 非 nil 时覆盖全局 TTL。

type Entry

type Entry struct {
	Source    state.ChannelSource
	SourceID  uint
	ExpiresAt time.Time
}

Entry 粘性记录值。带 Source 是因为 private/admin 的 ID 是两套空间, 重排时用 (Source, SourceID) 精确匹配候选,避免串号。

type Key

type Key struct {
	UserID    uint
	TokenID   uint
	RealModel string
	CacheKey  string
}

Key 是粘性映射键;显式 CacheKey 可按 identity partition 选择是否叠加用户、Token 和模型维度。

func BuildKey added in v0.0.18

func BuildKey(identity state.AffinityIdentity, userID, tokenID uint, realModel string) Key

BuildKey 把 finder 输出的身份隔离规则投影为实际 Store key。没有显式身份时 保持原有 user + token + model 粘性键。

type Policy

type Policy interface {
	Decide(s Subject) Decision
}

Policy 决定对某 Subject 是否记录/应用粘性。

type Store

type Store interface {
	Lookup(k Key) (Entry, bool) // 纯读:命中且未过期返回;过期当 miss 并惰性删除
	Remember(k Key, e Entry)    // 写/续期
	Forget(k Key)               // 删除
}

Store 是粘性表抽象。

type Subject

type Subject struct {
	UserID         uint
	RealModel      string
	ChannelEnabled *bool
	ChannelTTLSec  *int
}

Subject 是 Policy 决策的身份载体。ChannelEnabled/ChannelTTLSec 是命中渠道的每渠道覆盖, 零值(nil)=继承全局。解析在 Decide/ttl 内统一完成,调用方只负责把渠道覆盖原样传入。

Jump to

Keyboard shortcuts

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