[PR #6768] [MERGED] feat(notifications): add a failed-check threshold for node.down alerts #6447

Closed
opened 2026-11-02 04:08:50 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/MHSanaei/3x-ui/pull/6768
Author: @STRENCH0
Created: 10/8/2026
Status: ✅ Merged
Merged: 10/24/2026
Merged by: @MHSanaei

Base: main ← Head: feat/node-down-grace


📝 Commits (3)

  • 8e4ee39 feat(notifications): add a failed-check threshold for node.down alerts
  • 40797bf fix(notifications): stop re-alerting nodes that were already offline
  • 8a4db19 fix(notifications): persist node.down announcements across restarts

📊 Changes

34 files changed (+379 additions, -35 deletions)

View changed files

📝 docs/public/openapi.json (+12 -0)
📝 frontend/public/openapi.json (+12 -0)
📝 frontend/src/components/ui/notifications/EmailNotifications.tsx (+18 -1)
📝 frontend/src/components/ui/notifications/NotificationGroup.tsx (+17 -7)
📝 frontend/src/components/ui/notifications/TelegramNotifications.tsx (+18 -1)
📝 frontend/src/components/ui/notifications/types.ts (+1 -0)
📝 frontend/src/generated/examples.ts (+2 -0)
📝 frontend/src/generated/schemas.ts (+12 -0)
📝 frontend/src/generated/types.ts (+2 -0)
📝 frontend/src/generated/zod.ts (+2 -0)
📝 frontend/src/models/setting.ts (+5 -0)
📝 frontend/src/schemas/setting.ts (+1 -0)
📝 internal/database/db.go (+18 -1)
📝 internal/database/model/model.go (+2 -0)
➕ internal/database/node_down_notified_migration_test.go (+43 -0)
📝 internal/web/entity/entity.go (+1 -0)
➕ internal/web/job/node_down_threshold_test.go (+118 -0)
📝 internal/web/job/node_heartbeat_job.go (+62 -25)
📝 internal/web/job/node_transition_burst_test.go (+4 -0)
📝 internal/web/service/node.go (+6 -0)

...and 14 more files

📄 Description

Summary

Adds a nodeDownThreshold setting. node.down now fires only after that many failed heartbeat checks in a row, and node.up only after a node.down was actually sent. This works the same way outboundDownThreshold already debounces outbound alerts.

Why

One lost heartbeat on a lossy link flipped a node offline and sent node.down. The next probe, 5 s later, sent node.up. A flaky node flooded every channel (Telegram, email, Discord) with alert pairs, sometimes several pairs a minute for the same node.

The heartbeat now keeps a per-node in-memory streak, as XrayMetricsService does for outbounds:

  • A failed check increments the streak. node.down is published once the streak reaches the threshold, and only once per outage.
  • A successful check resets the streak. node.up is published only if a node.down was sent for that outage.
  • The node's DB status and the UI still change after every probe. Only the notifications wait for the threshold, so sync and offline-push logic are unaffected.
  • Burst folding (publishNodeTransitions) still applies to the debounced events.

Both threshold fields (outbound and node) also get a "failed checks in a row" hint. Before, the field was a bare number that did not say what it counts.

Type of change

  • Bug fix
  • New feature
  • Refactoring (no behavior change)
  • Documentation
  • Tests only
  • Build / CI / tooling
  • Other

Areas affected

  • Frontend (UI / panel pages)
  • Backend (API endpoints, login, settings)
  • Xray config generation
  • Subscription (share links / Clash / JSON)
  • Statistics / traffic counters
  • Database / migrations
  • Install / upgrade script
  • Docker image
  • Multi-node (sub-nodes)
  • Telegram bot

How was this tested?

  • New test TestHeartbeatHoldsNodeDownUntilThreshold (internal/web/job/node_down_threshold_test.go). It drives the real heartbeat job against an httptest node that can be switched up and down, with threshold = 3:

    • flapping below the threshold (fail, ok, fail, fail, ok, fail, ok): no notifications;
    • an outage reaching the threshold: node.down on the 3rd failed check, node.up on recovery;
    • node offline in the DB before a restart: stays quiet while still down, sends node.up on recovery.

    All three cases fail on the previous code. The flapping case reproduces the reported spam exactly.

  • The existing burst tests (node_transition_burst_test.go) pin the threshold to 1 and still pass.

  • go test ./internal/web/... passes. golangci-lint run ./internal/web/... reports 0 issues.

  • Frontend: npm run typecheck, npm run lint, npm run format:check, unit tests (1349, including i18n-dead-keys) and npm run build pass.

  • Manual check: ran the panel locally. Opened Settings → Telegram → Notifications, ticked Nodes → Down; the threshold field appears with the hint (same in Email). POST /panel/api/setting/all returns nodeDownThreshold: 1 on a fresh DB.

Screenshots / recordings

image

Breaking changes

None. No migration is needed: the setting defaults to 1, which keeps the current alerting. One minor behavior change: after a panel restart, node.up is no longer sent for a node that had no node.down in the current process (for example, a freshly added node or one that was offline in the DB at startup).

Checklist

  • I tested the change locally and confirmed the described behavior.
  • I added or updated tests for the new behavior (when applicable).
  • go build ./... and the test suite pass locally.
  • For frontend changes: npm run lint, npm run typecheck, and npm run build pass.
  • I updated the Wiki / README / API docs if user-facing behavior changed.
  • My commits follow the project's existing message style.
  • I have no unrelated changes mixed into this PR.

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/MHSanaei/3x-ui/pull/6768 **Author:** [@STRENCH0](https://github.com/STRENCH0) **Created:** 10/8/2026 **Status:** ✅ Merged **Merged:** 10/24/2026 **Merged by:** [@MHSanaei](https://github.com/MHSanaei) **Base:** `main` ← **Head:** `feat/node-down-grace` --- ### 📝 Commits (3) - [`8e4ee39`](https://github.com/MHSanaei/3x-ui/commit/8e4ee391aff30f698e476d290b879e9115a24de9) feat(notifications): add a failed-check threshold for node.down alerts - [`40797bf`](https://github.com/MHSanaei/3x-ui/commit/40797bfcb6740359e688e5f62721395599ee4d59) fix(notifications): stop re-alerting nodes that were already offline - [`8a4db19`](https://github.com/MHSanaei/3x-ui/commit/8a4db1945a29de3ebf25f4bc54a871ea06d1a5e0) fix(notifications): persist node.down announcements across restarts ### 📊 Changes **34 files changed** (+379 additions, -35 deletions) <details> <summary>View changed files</summary> 📝 `docs/public/openapi.json` (+12 -0) 📝 `frontend/public/openapi.json` (+12 -0) 📝 `frontend/src/components/ui/notifications/EmailNotifications.tsx` (+18 -1) 📝 `frontend/src/components/ui/notifications/NotificationGroup.tsx` (+17 -7) 📝 `frontend/src/components/ui/notifications/TelegramNotifications.tsx` (+18 -1) 📝 `frontend/src/components/ui/notifications/types.ts` (+1 -0) 📝 `frontend/src/generated/examples.ts` (+2 -0) 📝 `frontend/src/generated/schemas.ts` (+12 -0) 📝 `frontend/src/generated/types.ts` (+2 -0) 📝 `frontend/src/generated/zod.ts` (+2 -0) 📝 `frontend/src/models/setting.ts` (+5 -0) 📝 `frontend/src/schemas/setting.ts` (+1 -0) 📝 `internal/database/db.go` (+18 -1) 📝 `internal/database/model/model.go` (+2 -0) ➕ `internal/database/node_down_notified_migration_test.go` (+43 -0) 📝 `internal/web/entity/entity.go` (+1 -0) ➕ `internal/web/job/node_down_threshold_test.go` (+118 -0) 📝 `internal/web/job/node_heartbeat_job.go` (+62 -25) 📝 `internal/web/job/node_transition_burst_test.go` (+4 -0) 📝 `internal/web/service/node.go` (+6 -0) _...and 14 more files_ </details> ### 📄 Description ## Summary Adds a `nodeDownThreshold` setting. `node.down` now fires only after that many failed heartbeat checks in a row, and `node.up` only after a `node.down` was actually sent. This works the same way `outboundDownThreshold` already debounces outbound alerts. ## Why One lost heartbeat on a lossy link flipped a node offline and sent `node.down`. The next probe, 5 s later, sent `node.up`. A flaky node flooded every channel (Telegram, email, Discord) with alert pairs, sometimes several pairs a minute for the same node. The heartbeat now keeps a per-node in-memory streak, as `XrayMetricsService` does for outbounds: - A failed check increments the streak. `node.down` is published once the streak reaches the threshold, and only once per outage. - A successful check resets the streak. `node.up` is published only if a `node.down` was sent for that outage. - The node's DB status and the UI still change after every probe. Only the notifications wait for the threshold, so sync and offline-push logic are unaffected. - Burst folding (`publishNodeTransitions`) still applies to the debounced events. Both threshold fields (outbound and node) also get a "failed checks in a row" hint. Before, the field was a bare number that did not say what it counts. ## Type of change - [ ] Bug fix - [x] New feature - [ ] Refactoring (no behavior change) - [ ] Documentation - [ ] Tests only - [ ] Build / CI / tooling - [ ] Other ## Areas affected - [x] Frontend (UI / panel pages) - [x] Backend (API endpoints, login, settings) - [ ] Xray config generation - [ ] Subscription (share links / Clash / JSON) - [ ] Statistics / traffic counters - [x] Database / migrations - [ ] Install / upgrade script - [ ] Docker image - [x] Multi-node (sub-nodes) - [x] Telegram bot ## How was this tested? - New test `TestHeartbeatHoldsNodeDownUntilThreshold` (`internal/web/job/node_down_threshold_test.go`). It drives the real heartbeat job against an `httptest` node that can be switched up and down, with threshold = 3: - flapping below the threshold (fail, ok, fail, fail, ok, fail, ok): no notifications; - an outage reaching the threshold: `node.down` on the 3rd failed check, `node.up` on recovery; - node `offline` in the DB before a restart: stays quiet while still down, sends `node.up` on recovery. All three cases fail on the previous code. The flapping case reproduces the reported spam exactly. - The existing burst tests (`node_transition_burst_test.go`) pin the threshold to 1 and still pass. - `go test ./internal/web/...` passes. `golangci-lint run ./internal/web/...` reports 0 issues. - Frontend: `npm run typecheck`, `npm run lint`, `npm run format:check`, unit tests (1349, including `i18n-dead-keys`) and `npm run build` pass. - Manual check: ran the panel locally. Opened Settings → Telegram → Notifications, ticked Nodes → Down; the threshold field appears with the hint (same in Email). `POST /panel/api/setting/all` returns `nodeDownThreshold: 1` on a fresh DB. ## Screenshots / recordings <img width="572" height="158" alt="image" src="https://github.com/user-attachments/assets/54f118c3-e953-44b5-a8e2-d46ce97e4eaa" /> ## Breaking changes None. No migration is needed: the setting defaults to `1`, which keeps the current alerting. One minor behavior change: after a panel restart, `node.up` is no longer sent for a node that had no `node.down` in the current process (for example, a freshly added node or one that was `offline` in the DB at startup). ## Checklist - [x] I tested the change locally and confirmed the described behavior. - [x] I added or updated tests for the new behavior (when applicable). - [x] `go build ./...` and the test suite pass locally. - [x] For frontend changes: `npm run lint`, `npm run typecheck`, and `npm run build` pass. - [x] I updated the Wiki / README / API docs if user-facing behavior changed. - [x] My commits follow the project's existing message style. - [x] I have no unrelated changes mixed into this PR. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-11-02 04:08:50 +07:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
MHSanaei/3x-ui#6447
No description provided.