feat(framework): net 候选服务器轮询策略

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-06-28 14:23:41 +08:00
co-authored by Claude Opus 4.8
parent 08d41cdec1
commit 79d0071645
2 changed files with 53 additions and 0 deletions
@@ -0,0 +1,21 @@
/** 候选服务器轮询。docs/protocol/01 §7.3。 */
export class ReconnectPolicy {
private servers: string[];
private index = 0;
private failCount = 0;
constructor(servers: string | string[], private rotateEvery: number) {
this.servers = Array.isArray(servers) ? servers.slice() : [servers];
}
current(): string { return this.servers[this.index]; }
/** 记一次连接失败,按 rotateEvery 轮换候选,返回新的 current。 */
onFailure(): string {
this.failCount++;
if (this.servers.length > 1 && this.failCount % this.rotateEvery === 0) {
this.index = (this.index + 1) % this.servers.length;
}
return this.current();
}
reset(): void { this.failCount = 0; }
/** 服务器切换指令:改用指定地址。docs/protocol/01 §7.4。 */
setCurrent(server: string): void { this.servers = [server]; this.index = 0; this.failCount = 0; }
}