feat: 添加 Session 亲和路由支持(NewSession / WithAffinity)
- 新增 NewSession(pool):返回固定到同一 worker 进程的 Pool 视图 - 新增 WithAffinity(ctx, key):相同 key 通过 FNV hash 稳定路由到同一 worker - pool.acquire 支持亲和路由,无亲和时保持轮询 - example 添加 Session / WithAffinity / 全局变量共享三组演示
This commit is contained in:
13
pool.go
13
pool.go
@@ -210,7 +210,18 @@ func (p *pool) bindHandler(name string, fn reflect.Value) {
|
||||
}
|
||||
|
||||
func (p *pool) acquire(ctx context.Context) (net.Conn, *worker, error) {
|
||||
idx := p.idx.Add(1) % uint64(len(p.workers))
|
||||
n := uint64(len(p.workers))
|
||||
if n == 0 {
|
||||
return nil, nil, fmt.Errorf("gobridge: pool has no workers")
|
||||
}
|
||||
|
||||
var idx uint64
|
||||
if i := workerIndexFor(ctx, int(n)); i >= 0 {
|
||||
idx = uint64(i) % n // 防御:ctx 可能来自不同 pool
|
||||
} else {
|
||||
idx = p.idx.Add(1) % n
|
||||
}
|
||||
|
||||
w := p.workers[idx]
|
||||
conn, err := w.acquire(ctx)
|
||||
return conn, w, err
|
||||
|
||||
Reference in New Issue
Block a user