affinity

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Oct 11, 2026 License: AGPL-3.0 Imports: 5 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

Overview

Package affinity 按会话标识在候选渠道之间做确定性分配,让同一段会话的连续 请求落到同一个上游账号上,从而使上游的提示缓存有机会命中。

这里只做排序,不做选择:粘住的那条排在最前,其余候选按固定顺序跟在后面, 调用方的失败降级照旧生效。

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Chain

func Chain(entries [][]byte) []uint64

Chain 把条目序列折成一条累积哈希链。

链的第 i 项是「前 i + 1 个条目」的指纹。因此同一段会话在连续几轮里的链互为 前缀:上一轮的整条链就是这一轮链的前一段。靠这个关系,查「上一轮走了哪个渠道」 不需要知道上一轮有几个条目,只要从末尾往回查第一个命中的下标。

不用「整段内容算一个哈希」,是因为那样只能回答「这一轮是不是上一轮」,回答不了 「这一轮的开头哪一段是上一轮」。工具调用在一轮里能占六七条,去掉尾部固定条数 这种办法算不准。

func Order

func Order(key string, identities []string) []int

Order 返回 identities 的下标顺序,粘性最强的一条排在最前。

用 rendezvous 哈希(最高随机权重),不用取模。区别在候选集合变化时:取模会让 几乎所有键重新分配,rendezvous 只移动原本落在增删项上的那一部分。渠道上下线 时按取模会让全部会话的缓存集体失效,按 rendezvous 只影响其中一个渠道的份额。

func Pick

func Pick(key string, identities []string) string

Pick 返回 key 会粘住的标识,identities 为空时返回空串。

func PrefixBytes

func PrefixBytes(entries [][]byte) int

PrefixBytes 返回整个条目序列的字节数。

用来判断前缀够不够长。上游对可缓存的最小长度有门槛,短到进不了缓存时做亲和 只剩负载集中的坏处。

Types

type Table

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

Table 记住「请求前缀的指纹 → 渠道」的对应关系,供没有会话标识的客户端兜底。

键是内容指纹而不是会话名,因此两段内容完全相同的前缀会共用一条记忆。这不是 缺陷:内容相同就意味着上游那份缓存真的可以共享。

淘汰按插入顺序,不按最近使用。这里需要的是「上限之内保留各段会话最近若干轮的 指纹」,而活跃会话每一轮都写一条新记忆,上一轮那条只有一轮的岁数,挤不掉。 做成最近最少使用要额外维护一个堆,收益不足以抵消复杂度。

Table 可以并发使用。limit 不大于零时整个记忆关闭,Recall 一律不命中。

func NewTable

func NewTable(limit int, ttl time.Duration, now func() time.Time) *Table

NewTable 创建记忆表。ttl 为上游缓存的寿命,命中与写入都会续期。

func (*Table) Len

func (t *Table) Len() int

Len 返回当前的记忆条数。

func (*Table) Recall

func (t *Table) Recall(chain []uint64) (string, bool)

Recall 从链的末尾往回找第一条还活着的记忆,返回它记下的渠道。

从末尾往回是因为链越长说明前缀越贴近当前请求,命中的那一条就是最近一轮发过的 前缀。命中即续期:上游那份缓存被读一次就延长一次寿命。

func (*Table) Remember

func (t *Table) Remember(chain []uint64, channel string)

Remember 记下这一轮的完整前缀对应哪个渠道。

只写链的末项。前段由更早的轮次写过,Recall 会从末尾往回扫到它们。

Jump to

Keyboard shortcuts

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