feat(core): aggregate receive health by account
Track receive health for every process attached to an account while treating unarmed monitoring as compatible rather than degraded. Report the account as healthy while any attached process remains healthy, and mark it degraded only after every attached process has been confirmed stale. Keep health ownership synchronized with process attach, migration, and detach transitions. Fail fast when process ownership and health state diverge instead of silently presenting a healthy status. Add multi-process regression coverage for partial degradation, full degradation, and recovery. Refs #233.
This commit is contained in:
4 files changed
+63
No files matched your search
@@ -2,4 +2,7 @@ import type { BridgeContext } from './bridge-context';
|
||||
|
||||
export interface BridgeInterface extends BridgeContext {
|
||||
readonly activePid: number | null;
|
||||
/** False only when every attached QQ process is confirmed stale.
|
||||
* An unarmed watchdog remains true for compatibility. */
|
||||
readonly receiveHealthy: boolean;
|
||||
}
|
||||
@@ -19,6 +19,7 @@ const log = createLogger('Bridge');
|
||||
export class Bridge implements BridgeInterface {
|
||||
readonly identity: IdentityService;
|
||||
private pids_ = new Set<number>();
|
||||
private readonly receiveHealthByPid_ = new Map<number, boolean>();
|
||||
/** Packet senders keyed by their owning QQ process. Keeping the sender and
|
||||
* PID together is what makes disconnect fallback deterministic: a sender
|
||||
* can never outlive the PID that supplied it. */
|
||||
@@ -97,6 +98,7 @@ export class Bridge implements BridgeInterface {
|
||||
|
||||
dispose(): void {
|
||||
this.pids_.clear();
|
||||
this.receiveHealthByPid_.clear();
|
||||
this.packetClientsByPid_.clear();
|
||||
this.packetClient_ = null;
|
||||
this.packetClientPid_ = null;
|
||||
@@ -134,6 +136,10 @@ export class Bridge implements BridgeInterface {
|
||||
attachPid(pid: number): void {
|
||||
this.pids_.delete(pid);
|
||||
this.pids_.add(pid);
|
||||
// A fresh/rebound login starts UNKNOWN-but-compatible. The receive
|
||||
// watchdog only flips this false after observing QQ's own heartbeat and
|
||||
// then missing the full stale + confirmation window.
|
||||
this.receiveHealthByPid_.set(pid, true);
|
||||
}
|
||||
|
||||
/** Atomically attach a process and make its sender the active sender.
|
||||
@@ -153,6 +159,7 @@ export class Bridge implements BridgeInterface {
|
||||
|
||||
detachPid(pid: number): void {
|
||||
this.pids_.delete(pid);
|
||||
this.receiveHealthByPid_.delete(pid);
|
||||
this.packetClientsByPid_.delete(pid);
|
||||
|
||||
if (this.packetClientPid_ !== pid) {
|
||||
@@ -177,6 +184,24 @@ export class Bridge implements BridgeInterface {
|
||||
const pids = [...this.pids_];
|
||||
return pids.length > 0 ? pids[pids.length - 1]! : null;
|
||||
}
|
||||
get receiveHealthy(): boolean {
|
||||
for (const pid of this.pids_) {
|
||||
const healthy = this.receiveHealthByPid_.get(pid);
|
||||
if (healthy === undefined) {
|
||||
const message = `Bridge receive-health invariant violated: PID=${pid} has no health state`;
|
||||
log.error(message);
|
||||
throw new Error(message);
|
||||
}
|
||||
if (healthy) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
setPidReceiveHealthy(pid: number, healthy: boolean): void {
|
||||
if (!this.pids_.has(pid)) {
|
||||
throw new Error(`Bridge receive-health invariant violated: PID=${pid} is not attached`);
|
||||
}
|
||||
this.receiveHealthByPid_.set(pid, healthy);
|
||||
}
|
||||
onPacket(pkt: PacketInfo): void {
|
||||
this.pipeline.process(pkt);
|
||||
}
|
||||
|
||||
@@ -60,6 +60,23 @@ export class BridgeManager {
|
||||
this.detachPidFromSession(pid, uin);
|
||||
}
|
||||
|
||||
onPidReceiveHealthChanged(pid: number, healthy: boolean): void {
|
||||
const uin = this.pidToUin_.get(pid);
|
||||
if (!uin) {
|
||||
const message = `BridgeManager invariant violated: receive health for unmapped PID=${pid}`;
|
||||
log.error(message);
|
||||
throw new Error(message);
|
||||
}
|
||||
|
||||
const session = this.sessions_.get(uin);
|
||||
if (!session || !session.bridge.hasPid(pid)) {
|
||||
const message = `BridgeManager invariant violated: receive health PID=${pid} is not owned by UIN=${uin}`;
|
||||
log.error(message);
|
||||
throw new Error(message);
|
||||
}
|
||||
session.bridge.setPidReceiveHealthy(pid, healthy);
|
||||
}
|
||||
|
||||
onHookLogin(pid: number, uin: string, packetClient: PacketSender): void {
|
||||
if (!isRealUin(uin)) return;
|
||||
|
||||
|
||||
@@ -104,6 +104,24 @@ describe('BridgeManager PID ownership', () => {
|
||||
});
|
||||
});
|
||||
|
||||
it('reports an account healthy while any attached PID still receives packets', () => {
|
||||
const manager = new BridgeManager();
|
||||
manager.onHookLogin(101, '10001', makeSender().client);
|
||||
manager.onHookLogin(202, '10001', makeSender().client);
|
||||
const bridge = manager.getSession('10001')!.bridge;
|
||||
|
||||
expect(bridge.receiveHealthy).toBe(true);
|
||||
|
||||
manager.onPidReceiveHealthChanged(101, false);
|
||||
expect(bridge.receiveHealthy).toBe(true);
|
||||
|
||||
manager.onPidReceiveHealthChanged(202, false);
|
||||
expect(bridge.receiveHealthy).toBe(false);
|
||||
|
||||
manager.onPidReceiveHealthChanged(101, true);
|
||||
expect(bridge.receiveHealthy).toBe(true);
|
||||
});
|
||||
|
||||
it('prefers the most recently rebound live PID, then falls back by recency', async () => {
|
||||
const manager = new BridgeManager();
|
||||
const first = makeSender();
|
||||
|
||||
Reference in new issue
Block a user