Skip to content

A modern and elegant dashboard for network traffic visualization and analysis.

fix(collector): stop idle agents showing offline in the backend list (#58)

luoleia2c57a2f1d49
diff --git a/.env.example b/.env.example
index 405e7a3..f9ff37b 100644
--- a/.env.example
+++ b/.env.example
@@ -72,7 +72,7 @@ WS_EXTERNAL_PORT=3002
# API_PORT=3001
# COLLECTOR_WS_PORT=3002
-# Agent 自动健康检查的心跳超时(毫秒,最小 15000;默认 30 秒心跳 × 3,容忍上报延迟)
+# Agent 判定离线的心跳超时,后端列表/健康检查/手动测试共用(毫秒,最小 15000;默认 30 秒心跳 × 3,容忍上报延迟)
# 设置在采集器进程的环境中
# AGENT_HEARTBEAT_TIMEOUT_MS=90000
diff --git a/README.en.md b/README.en.md
index 4b71477..2281cc2 100644
--- a/README.en.md
+++ b/README.en.md
@@ -418,6 +418,7 @@ The script will automatically detect and suggest available ports.
| `NEXT_PUBLIC_WS_PORT` | `3002` | WS direct-connection fallback port (**build-time only — setting this at Docker runtime has no effect**; use `WS_EXTERNAL_PORT` instead) | Only for custom source builds |
| `API_URL` | `http://localhost:3001` | Next.js `/api` rewrite target (mainly source/custom builds) | API listen address changed |
| `COOKIE_SECRET` | auto-generated | Cookie signing secret; if not fixed, sessions can be invalidated after restart when data dir is not persisted | Strongly recommended in production |
+| `AGENT_HEARTBEAT_TIMEOUT_MS` | `90000` | How long (ms, minimum `15000`) an agent may go without a heartbeat/report before it is shown offline (backend list, health checks, manual test). Defaults to 3 × the agent's 30s heartbeat interval | Agents flap online/offline on slow links |
| `BACKEND_TLS_INSECURE` | empty | Set to `1` to skip TLS certificate verification for gateway APIs only (Surge `http-api-tls`, mihomo `external-controller-tls` with self-signed certs). Logs a warning at startup | Gateway uses HTTPS with a self-signed / IP-mismatched certificate |
| `GEOIP_LOOKUP_PROVIDER` | `online` | IP geolocation source (`online` / `local`) | Default to local MMDB lookup |
| `GEOIP_ONLINE_API_URL` | `https://api.ipinfo.es/ipinfo` | Online IP geolocation API endpoint (must be compatible with `ipinfo.my` response schema) | Set only when you deploy a compatible endpoint |
diff --git a/README.md b/README.md
index f1ab38c..9cc79f1 100644
--- a/README.md
+++ b/README.md
@@ -418,6 +418,7 @@ The script will automatically detect and suggest available ports.
| `NEXT_PUBLIC_WS_PORT` | `3002` | WS direct-connection fallback port (**build-time only — setting this at Docker runtime has no effect**; use `WS_EXTERNAL_PORT` instead) | Only for custom source builds |
| `API_URL` | `http://localhost:3001` | Next.js `/api` rewrite target (mainly source/custom builds) | API listen address changed |
| `COOKIE_SECRET` | auto-generated | Cookie signing secret; if not fixed, sessions can be invalidated after restart when data dir is not persisted | Strongly recommended in production |
+| `AGENT_HEARTBEAT_TIMEOUT_MS` | `90000` | How long (ms, minimum `15000`) an agent may go without a heartbeat/report before it is shown offline (backend list, health checks, manual test). Defaults to 3 × the agent's 30s heartbeat interval | Agents flap online/offline on slow links |
| `BACKEND_TLS_INSECURE` | empty | Set to `1` to skip TLS certificate verification for gateway APIs only (Surge `http-api-tls`, mihomo `external-controller-tls` with self-signed certs). Logs a warning at startup | Gateway uses HTTPS with a self-signed / IP-mismatched certificate |
| `GEOIP_LOOKUP_PROVIDER` | `online` | IP geolocation source (`online` / `local`) | Default to local MMDB lookup |
| `GEOIP_ONLINE_API_URL` | `https://api.ipinfo.es/ipinfo` | Online IP geolocation API endpoint (must be compatible with `ipinfo.my` response schema) | Set only when you deploy a compatible endpoint |
@@ -428,7 +429,6 @@ The script will automatically detect and suggest available ports.
| Variable | Default | Description |
| :-- | :-- | :-- |
-| `AGENT_HEARTBEAT_TIMEOUT_MS` | `90000` | Collector-side agent heartbeat timeout for automatic health checks in milliseconds (minimum `15000`); defaults to 3 × the agent's 30s heartbeat interval to tolerate delayed reports |
| `FLUSH_INTERVAL_MS` | `30000` | Buffer flush interval for collector writes |
| `FLUSH_MAX_BUFFER_SIZE` | `5000` | Max buffer entries before early flush |
| `REALTIME_MAX_MINUTES` | `180` | Realtime in-memory window size (minutes) |
diff --git a/README.zh.md b/README.zh.md
index 69c7bbd..47d01a8 100644
--- a/README.zh.md
+++ b/README.zh.md
@@ -409,6 +409,7 @@ curl -fsSL https://raw.githubusercontent.com/foru17/neko-master/main/setup.sh |
| `NEXT_PUBLIC_WS_PORT` | `3002` | WS 直连端口兜底值(**构建时注入,Docker 运行时设置无效**;Docker 场景请用 `WS_EXTERNAL_PORT`) | 仅源码自构建时需要自定义端口 |
| `API_URL` | `http://localhost:3001` | Next.js `/api` rewrite 目标(主要用于源码/自构建) | 你修改了 API 实际监听地址时 |
| `COOKIE_SECRET` | 自动生成 | Cookie 签名密钥;未固定时会自动生成(数据目录不持久化时重启后会话会失效) | 生产环境强烈建议固定配置 |
+| `AGENT_HEARTBEAT_TIMEOUT_MS` | `90000` | Agent 超过该时长(毫秒,最小 `15000`)没有心跳/上报即显示离线(后端列表、健康检查、手动测试)。默认为 Agent 30 秒心跳间隔的 3 倍 | 网络较慢、Agent 在线状态反复跳变时 |
| `BACKEND_TLS_INSECURE` | 空 | 设为 `1` 时仅对网关 API 跳过 TLS 证书校验(Surge `http-api-tls`、mihomo `external-controller-tls` 自签证书),启动时打印警告 | 网关走 HTTPS 且证书为自签 / 与 IP 不匹配时 |
| `GEOIP_LOOKUP_PROVIDER` | `online` | IP 地理查询来源(`online`/`local`) | 需要默认走本地 MMDB 查询时 |
| `GEOIP_ONLINE_API_URL` | `https://api.ipinfo.es/ipinfo` | 在线 IP 查询接口地址(需兼容 `ipinfo.my` 的响应结构) | 仅在你部署了兼容接口时设置 |
diff --git a/apps/collector/src/modules/backend/backend.service.test.ts b/apps/collector/src/modules/backend/backend.service.test.ts
index cefd6f2..a0044e9 100644
--- a/apps/collector/src/modules/backend/backend.service.test.ts
+++ b/apps/collector/src/modules/backend/backend.service.test.ts
@@ -47,4 +47,32 @@ describe('BackendService agent heartbeat health checks', () => {
expect(service.getHealthStatus(backendId)?.status).toBe(expected);
});
+
+ // Issue #58: an idle agent refreshes lastSeen only via its 30s heartbeat.
+ // The backend list badge and the manual test must not use a shorter window.
+ it('keeps an idle agent healthy in the backend list and manual test', async () => {
+ db.upsertAgentHeartbeat({
+ backendId,
+ agentId: 'test-agent',
+ lastSeen: new Date(Date.now() - 25_000).toISOString(),
+ });
+
+ const listed = service.getAllBackends().find((b) => b.id === backendId);
+ expect(listed?.health?.status).toBe('healthy');
+
+ const manual = await service.testExistingBackendConnection(backendId);
+ expect(manual.success).toBe(true);
+ });
+
+ it('still honours an explicit AGENT_MANUAL_TEST_TIMEOUT_MS', async () => {
+ vi.stubEnv('AGENT_MANUAL_TEST_TIMEOUT_MS', '8000');
+ db.upsertAgentHeartbeat({
+ backendId,
+ agentId: 'test-agent',
+ lastSeen: new Date(Date.now() - 25_000).toISOString(),
+ });
+
+ const manual = await service.testExistingBackendConnection(backendId);
+ expect(manual.success).toBe(false);
+ });
});
diff --git a/apps/collector/src/modules/backend/backend.service.ts b/apps/collector/src/modules/backend/backend.service.ts
index 3ccaf7e..ae04c13 100644
--- a/apps/collector/src/modules/backend/backend.service.ts
+++ b/apps/collector/src/modules/backend/backend.service.ts
@@ -211,11 +211,9 @@ export class BackendService {
*/
private attachHealthStatus(backend: BackendResponse): BackendResponse {
if (isAgentBackendUrl(backend.url)) {
- const dynamicHealth = this.buildAgentHealthStatus(
- backend.id,
- Date.now(),
- this.getAgentManualTestTimeoutMs(),
- );
+ // Idle agents only refresh lastSeen via the periodic heartbeat, so the
+ // list badge must use the heartbeat timeout (issue #58).
+ const dynamicHealth = this.buildAgentHealthStatus(backend.id, Date.now());
this.healthStatus.set(backend.id, dynamicHealth);
return { ...backend, health: dynamicHealth };
}
@@ -573,10 +571,13 @@ export class BackendService {
}
private getAgentManualTestTimeoutMs(): number {
- return Math.max(
- 3_000,
- Number.parseInt(process.env.AGENT_MANUAL_TEST_TIMEOUT_MS || '8000', 10) || 8_000,
- );
+ // An idle agent only sends a heartbeat every 30s, so a shorter window
+ // reports healthy agents as offline (issue #58). Default to the heartbeat
+ // timeout unless explicitly overridden.
+ const override = Number.parseInt(process.env.AGENT_MANUAL_TEST_TIMEOUT_MS || '', 10);
+ return Number.isFinite(override) && override > 0
+ ? Math.max(3_000, override)
+ : this.getAgentHeartbeatTimeoutMs();
}
private buildAgentHealthStatus(
diff --git a/docs/agent/install.en.md b/docs/agent/install.en.md
index 5633fe3..571e1f1 100644
--- a/docs/agent/install.en.md
+++ b/docs/agent/install.en.md
@@ -40,7 +40,7 @@ Optional env:
- `NEKO_BIN_LINK_MODE`: `auto|true|false` for symlink into global bin dir (default `auto`)
- `NEKO_LINK_DIR`: global bin dir for symlink (default `/usr/local/bin`)
-Set `AGENT_HEARTBEAT_TIMEOUT_MS` in the collector process environment to adjust the agent heartbeat timeout for automatic health checks (milliseconds, default `90000`, minimum `15000`). The default allows 3 × the agent's default 30-second heartbeat interval to tolerate delayed reports; this is a collector setting, not an agent install option.
+Set `AGENT_HEARTBEAT_TIMEOUT_MS` in the collector process environment to adjust how long an agent may stay silent before it is shown offline (backend list, automatic health checks and manual test) (milliseconds, default `90000`, minimum `15000`). The default allows 3 × the agent's default 30-second heartbeat interval to tolerate delayed reports; this is a collector setting, not an agent install option.
After install, manage agent with:
diff --git a/docs/agent/install.md b/docs/agent/install.md
index 9a31666..557406c 100644
--- a/docs/agent/install.md
+++ b/docs/agent/install.md
@@ -40,7 +40,7 @@ curl -fsSL https://raw.githubusercontent.com/foru17/neko-master/main/apps/agent/
- `NEKO_BIN_LINK_MODE`:全局 bin 目录软链模式(`auto|true|false`,默认 `auto`)
- `NEKO_LINK_DIR`:软链目标目录(默认 `/usr/local/bin`)
-采集器端可设置 `AGENT_HEARTBEAT_TIMEOUT_MS` 调整 Agent 自动健康检查的心跳超时(毫秒,默认 `90000`,最小 `15000`)。默认值为 Agent 默认 30 秒心跳间隔的 3 倍,用于容忍上报延迟;此变量应配置在采集器进程的环境中,而非 Agent 安装命令中。
+采集器端可设置 `AGENT_HEARTBEAT_TIMEOUT_MS` 调整 Agent 判定离线的心跳超时(后端列表、自动健康检查与手动测试共用)(毫秒,默认 `90000`,最小 `15000`)。默认值为 Agent 默认 30 秒心跳间隔的 3 倍,用于容忍上报延迟;此变量应配置在采集器进程的环境中,而非 Agent 安装命令中。
安装完成后,使用以下命令管理 Agent: