惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

IT之家
IT之家
The GitHub Blog
The GitHub Blog
F
Fortinet All Blogs
Last Week in AI
Last Week in AI
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
L
LangChain Blog
爱范儿
爱范儿
博客园_首页
Stack Overflow Blog
Stack Overflow Blog
MongoDB | Blog
MongoDB | Blog
博客园 - 三生石上(FineUI控件)
大猫的无限游戏
大猫的无限游戏
宝玉的分享
宝玉的分享
GbyAI
GbyAI
H
Help Net Security
A
About on SuperTechFans
Recent Announcements
Recent Announcements
Hugging Face - Blog
Hugging Face - Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
雷峰网
雷峰网
D
Docker
博客园 - Franky
有赞技术团队
有赞技术团队
G
Google Developers Blog

Recent Commits to openclaw:main

test: merge chat side-result checks · openclaw/openclaw@ddd2c2a test: merge cron history checks · openclaw/openclaw@f7eb746 test: merge responsive navigation shell checks · openclaw/openclaw@c2e4b47 docs(changelog): add codex oauth fixes · openclaw/openclaw@628e6cd test: merge navigation routing cases · openclaw/openclaw@5d8cecb Tests: mock channel registry bundled fallback · openclaw/openclaw@2b08233 Secrets: avoid broad web search discovery for single plugin config · openclaw/openclaw@a464f59 test: merge config view browser checks · openclaw/openclaw@20cf511 fix(status): align oauth health with runtime · openclaw/openclaw@eed7116 feat: add macOS screen snapshots for monitor preview (#67954) thanks … · openclaw/openclaw@f377db1 fix: report shared auth scopes in hello-ok (#67810) thanks @BunsDev · openclaw/openclaw@0b6c39b Auto-reply: avoid eager bundled route fallback · openclaw/openclaw@3ea1bf4 Tests: narrow session binding contract setup · openclaw/openclaw@54e4e16 fix(macOS): enable undo/redo in webchat composer text input (#34962) · openclaw/openclaw@00951dc Tests: speed up channel setup promotion · openclaw/openclaw@82b529a Docs: refresh agent instructions · openclaw/openclaw@5775fe2 fix(auth): serialize OAuth refresh across agents to fix #26322 (#67876) · openclaw/openclaw@8e79080 test: allow ollama public surface boundary test · openclaw/openclaw@7d4f1a6 Docs: add test performance guardrails · openclaw/openclaw@89706d3 Tests: restore context-engine usage proof · openclaw/openclaw@e4c4f95 Tests: slim context engine runtime coverage · openclaw/openclaw@74c198f ci: retry failed custom checkouts · openclaw/openclaw@0ee5baf test: trim duplicate provider auth onboarding cases · openclaw/openclaw@1ffc02e matrix: fix sessions_spawn --thread subagent session spawning (#67643) · openclaw/openclaw@1ce2596 test: reduce auth choice fixture churn · openclaw/openclaw@857b9cd test: mock health status config boundaries · openclaw/openclaw@9d5ab4a test: mock onboard config io boundary · openclaw/openclaw@299694d test: mock legacy state plugin boundaries · openclaw/openclaw@2713089 test: mock channel install boundaries · openclaw/openclaw@b945248 test: mock doctor preview channel boundaries · openclaw/openclaw@b1a3ad4
fix(gateway): expose event loop health in readiness · ope...
vincentkoc · 2026-04-28 · via Recent Commits to openclaw:main

@@ -0,0 +1,108 @@

1+

import { monitorEventLoopDelay, performance } from "node:perf_hooks";

2+3+

const EVENT_LOOP_MONITOR_RESOLUTION_MS = 20;

4+

const EVENT_LOOP_DELAY_WARN_MS = 1_000;

5+

const EVENT_LOOP_UTILIZATION_WARN = 0.95;

6+

const CPU_CORE_RATIO_WARN = 0.9;

7+8+

type EventLoopDelayMonitor = ReturnType<typeof monitorEventLoopDelay>;

9+

type EventLoopUtilization = ReturnType<typeof performance.eventLoopUtilization>;

10+

type CpuUsage = ReturnType<typeof process.cpuUsage>;

11+12+

export type GatewayEventLoopHealthReason = "event_loop_delay" | "event_loop_utilization" | "cpu";

13+14+

export type GatewayEventLoopHealth = {

15+

degraded: boolean;

16+

reasons: GatewayEventLoopHealthReason[];

17+

intervalMs: number;

18+

delayP99Ms: number;

19+

delayMaxMs: number;

20+

utilization: number;

21+

cpuCoreRatio: number;

22+

};

23+24+

export type GatewayEventLoopHealthMonitor = {

25+

snapshot: () => GatewayEventLoopHealth | undefined;

26+

stop: () => void;

27+

};

28+29+

function roundMetric(value: number, digits = 3): number {

30+

if (!Number.isFinite(value)) {

31+

return 0;

32+

}

33+

const factor = 10 ** digits;

34+

return Math.round(value * factor) / factor;

35+

}

36+37+

function nanosecondsToMilliseconds(value: number): number {

38+

return roundMetric(value / 1_000_000, 1);

39+

}

40+41+

export function createGatewayEventLoopHealthMonitor(): GatewayEventLoopHealthMonitor {

42+

let monitor: EventLoopDelayMonitor | null = null;

43+

let lastWallAt = Date.now();

44+

let lastCpuUsage: CpuUsage | null = process.cpuUsage();

45+

let lastEventLoopUtilization: EventLoopUtilization | null = performance.eventLoopUtilization();

46+47+

try {

48+

monitor = monitorEventLoopDelay({ resolution: EVENT_LOOP_MONITOR_RESOLUTION_MS });

49+

monitor.enable();

50+

monitor.reset();

51+

} catch {

52+

monitor = null;

53+

}

54+55+

return {

56+

snapshot: () => {

57+

if (!monitor || !lastCpuUsage || !lastEventLoopUtilization || lastWallAt <= 0) {

58+

return undefined;

59+

}

60+61+

const now = Date.now();

62+

const intervalMs = Math.max(1, now - lastWallAt);

63+

const cpuUsage = process.cpuUsage(lastCpuUsage);

64+

const currentEventLoopUtilization = performance.eventLoopUtilization();

65+

const utilization = roundMetric(

66+

performance.eventLoopUtilization(currentEventLoopUtilization, lastEventLoopUtilization)

67+

.utilization,

68+

);

69+

const delayP99Ms = nanosecondsToMilliseconds(monitor.percentile(99));

70+

const delayMaxMs = nanosecondsToMilliseconds(monitor.max);

71+

const cpuTotalMs = roundMetric((cpuUsage.user + cpuUsage.system) / 1_000, 1);

72+

const cpuCoreRatio = roundMetric(cpuTotalMs / intervalMs);

73+

const reasons: GatewayEventLoopHealthReason[] = [];

74+75+

if (delayP99Ms >= EVENT_LOOP_DELAY_WARN_MS || delayMaxMs >= EVENT_LOOP_DELAY_WARN_MS) {

76+

reasons.push("event_loop_delay");

77+

}

78+

if (utilization >= EVENT_LOOP_UTILIZATION_WARN) {

79+

reasons.push("event_loop_utilization");

80+

}

81+

if (cpuCoreRatio >= CPU_CORE_RATIO_WARN) {

82+

reasons.push("cpu");

83+

}

84+85+

monitor.reset();

86+

lastWallAt = now;

87+

lastCpuUsage = process.cpuUsage();

88+

lastEventLoopUtilization = currentEventLoopUtilization;

89+90+

return {

91+

degraded: reasons.length > 0,

92+

reasons,

93+

intervalMs,

94+

delayP99Ms,

95+

delayMaxMs,

96+

utilization,

97+

cpuCoreRatio,

98+

};

99+

},

100+

stop: () => {

101+

monitor?.disable();

102+

monitor = null;

103+

lastWallAt = 0;

104+

lastCpuUsage = null;

105+

lastEventLoopUtilization = null;

106+

},

107+

};

108+

}