perf(heartbeat): 10s interval, collect metrics every 3rd beat (30s)
Made-with: Cursor
This commit is contained in:
@@ -14,6 +14,8 @@ const led = require('./led');
|
|||||||
const MAX_BACKOFF_MS = 60_000;
|
const MAX_BACKOFF_MS = 60_000;
|
||||||
const PONG_TIMEOUT_MS = 15_000;
|
const PONG_TIMEOUT_MS = 15_000;
|
||||||
const PING_INTERVAL_MS = 30_000;
|
const PING_INTERVAL_MS = 30_000;
|
||||||
|
const HEARTBEAT_INTERVAL_MS = 10_000; // 心跳间隔:10 秒,用于快速感知网络状态
|
||||||
|
const METRICS_EVERY_N = 3; // 每 N 次心跳采集一次指标(= 30 秒)
|
||||||
|
|
||||||
// systemd watchdog: 如果 WatchdogSec 存在,定期发 WATCHDOG=1
|
// systemd watchdog: 如果 WatchdogSec 存在,定期发 WATCHDOG=1
|
||||||
const SD_WATCHDOG_USEC = parseInt(process.env.WATCHDOG_USEC || '0', 10);
|
const SD_WATCHDOG_USEC = parseInt(process.env.WATCHDOG_USEC || '0', 10);
|
||||||
@@ -279,9 +281,8 @@ class ClawClient {
|
|||||||
|
|
||||||
_startHeartbeat() {
|
_startHeartbeat() {
|
||||||
this._clearHeartbeat();
|
this._clearHeartbeat();
|
||||||
const interval = (this._cfg.heartbeat_interval || 30) * 1000;
|
|
||||||
this._sendHeartbeat();
|
this._sendHeartbeat();
|
||||||
this._hbTimer = setInterval(() => this._sendHeartbeat(), interval);
|
this._hbTimer = setInterval(() => this._sendHeartbeat(), HEARTBEAT_INTERVAL_MS);
|
||||||
}
|
}
|
||||||
|
|
||||||
async _sendHeartbeat() {
|
async _sendHeartbeat() {
|
||||||
@@ -289,23 +290,25 @@ class ClawClient {
|
|||||||
try {
|
try {
|
||||||
this._hbCount++;
|
this._hbCount++;
|
||||||
|
|
||||||
// 每 10 次心跳(约 5 分钟)刷新一次 dashboard 信息,
|
// 每 30 次心跳(约 5 分钟)刷新一次 dashboard 信息
|
||||||
// 确保初次提取失败时能自动补偿,或在 token 变化后自动同步
|
if (this._hbCount % 30 === 0) {
|
||||||
if (this._hbCount % 10 === 0) {
|
|
||||||
const freshInfo = await getDashboardInfo().catch(() => null);
|
const freshInfo = await getDashboardInfo().catch(() => null);
|
||||||
if (freshInfo && Object.keys(freshInfo).length > 0) {
|
if (freshInfo && Object.keys(freshInfo).length > 0) {
|
||||||
this._dashInfo = freshInfo;
|
this._dashInfo = freshInfo;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const metrics = await collect();
|
// 每 METRICS_EVERY_N 次心跳(30 秒)采集一次指标,其余发轻量心跳
|
||||||
this._send({
|
const msg = {
|
||||||
type: 'heartbeat',
|
type: 'heartbeat',
|
||||||
claw_id: this._cfg.claw_id,
|
claw_id: this._cfg.claw_id,
|
||||||
token: this._cfg.token,
|
token: this._cfg.token,
|
||||||
metrics,
|
...this._dashInfo,
|
||||||
...this._dashInfo, // 携带 dashboard_token / dashboard_port,供 VPS 幂等更新
|
};
|
||||||
});
|
if (this._hbCount % METRICS_EVERY_N === 0) {
|
||||||
|
msg.metrics = await collect();
|
||||||
|
}
|
||||||
|
this._send(msg);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
log.error('clawd', '心跳发送失败:', e.message);
|
log.error('clawd', '心跳发送失败:', e.message);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user