refactor: remove dead RemoveClientAffinity code, add affinity scheduling tests

- Remove RemoveClientAffinity from GatewayCache interface, implementation,
  Lua script, and all test mocks (never called in production code)
- Add 26 unit tests for affinity scheduling: filterByMinAffinityCount,
  populateAffinityCounts, Layer 1/2 sort chain integration

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
erioandClaude Opus 4.6 committed 2026-03-07 04:26:08 +08:00
1 parent d707c1f93f
commit b9dc2e8eb5
11 files changed
+607 -43

No files matched your search

@@ -22,14 +22,11 @@ var (
getAffinityLua string
//go:embed lua/update_affinity.lua
updateAffinityLua string
//go:embed lua/remove_affinity.lua
removeAffinityLua string
//go:embed lua/get_affinity_count.lua
getAffinityCountLua string
getAffinityScript = redis.NewScript(getAffinityLua)
updateAffinityScript = redis.NewScript(updateAffinityLua)
removeAffinityScript = redis.NewScript(removeAffinityLua)
getAffinityCountScript = redis.NewScript(getAffinityCountLua)
)
@@ -116,15 +113,6 @@ func (c *gatewayCache) UpdateClientAffinity(ctx context.Context, groupID int64,
).Err()
}
func (c *gatewayCache) RemoveClientAffinity(ctx context.Context, groupID int64, clientID string, accountID int64) error {
fwdKey := buildAffinityKey(groupID, clientID)
revKey := buildAffinityReverseKey(groupID, accountID)
return removeAffinityScript.Run(ctx, c.rdb, []string{fwdKey, revKey},
accountID, clientID,
).Err()
}
// GetAccountAffinityCountBatch 批量获取账号的亲和客户端数量(惰性清理过期成员)
func (c *gatewayCache) GetAccountAffinityCountBatch(ctx context.Context, groupID int64, accountIDs []int64, ttl time.Duration) (map[int64]int64, error) {
if len(accountIDs) == 0 {
@@ -1,8 +0,0 @@
-- 原子双删正向+反向索引
-- KEYS[1] = client_affinity:{groupID}:{clientID} (正向)
-- KEYS[2] = client_affinity_rev:{groupID}:{accountID} (反向)
-- ARGV[1] = accountID (正向索引中要删除的成员)
-- ARGV[2] = clientID (反向索引中要删除的成员)
redis.call('ZREM', KEYS[1], ARGV[1])
redis.call('ZREM', KEYS[2], ARGV[2])
return 1