[PR #4299] [CLOSED] fix(multi-node): scope tag and email unique constraints per node #5644

Closed
opened 2026-10-31 20:48:22 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/MHSanaei/3x-ui/pull/4299
Author: @abdalrahmanx9
Created: 5/12/2026
Status: ❌ Closed

Base: main ← Head: fix/node-tag-unique-scope


📝 Commits (9)

  • 686ca1f fix(multi-node): scope tag and email unique constraints per node
  • 821e50d fix(inbound): prevent shared client_traffic row deletion when sibling inbound is deleted
  • 41faaa2 Merge branch 'main' into fix/node-tag-unique-scope
  • dd18130 Merge branch 'main' into fix/node-tag-unique-scope
  • 341eb5b chore(xray): minor cleanups to graceful-shutdown follow-ups for #4259
  • 4ec84cf Merge branch 'main' into fix/node-tag-unique-scope
  • b8adda0 Merge branch 'main' into fix/node-tag-unique-scope
  • 05b24bd Update db.go
  • 3463aee Merge branch 'main' into fix/node-tag-unique-scope

📊 Changes

7 files changed (+110 additions, -25 deletions)

View changed files

📝 database/db.go (+52 -0)
📝 database/model/model.go (+7 -2)
📝 web/service/inbound.go (+37 -10)
📝 web/service/port_conflict.go (+10 -5)
📝 xray/client_traffic.go (+2 -2)
📝 xray/process.go (+0 -4)
📝 xray/process_test.go (+2 -2)

📄 Description

Problem

When running a central panel with multiple remote nodes, the traffic sync silently drops inbounds from some nodes. The server log shows:

WARNING - setRemoteTraffic: create central inbound for tag inbound-48055 failed: UNIQUE constraint failed: inbounds.tag
WARNING - node traffic sync: merge for $NODE_NAME failed: UNIQUE constraint failed: client_traffics.email

Two global UNIQUE constraints are responsible:

  1. inbounds.tag (gorm:"unique") -- if Node A has an inbound on port 48055 (tag = inbound-48055), Node B cannot sync its inbound on the same port. The tag must be globally unique, even though each node runs a separate xray instance.

  2. client_traffics.email (gorm:"unique") -- if Node A has a client with email foo@bar, and Node B also has a client with that email, the client stats CREATE fails and rolls back the entire transaction -- including all inbounds that were successfully created in the same cycle. The node stays at 0 inbounds permanently, retrying every 10s and failing each time.

Root cause

Tags and client emails only need to be unique within the same xray instance. A tag like inbound-48055 on Node A is a completely different xray socket from inbound-48055 on Node B -- they are on different machines. The same applies to client emails.

Changes

4 files, +67/-9

  • database/model/model.go: Changed Tag from gorm:"unique" (global) to uniqueIndex:idx_node_tag composited with NodeID.
  • xray/client_traffic.go: Changed Email from gorm:"unique" (global) to uniqueIndex:idx_ct_inbound_email composited with InboundId.
  • web/service/port_conflict.go: tagExists() scopes by node_id so the UI does not see remote tags as conflicts.
  • database/db.go: Added migrateLegacyUniqueConstraints() that removes old UNIQUE constraints from existing databases on startup and creates the new composite indexes.

🔄 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/4299 **Author:** [@abdalrahmanx9](https://github.com/abdalrahmanx9) **Created:** 5/12/2026 **Status:** ❌ Closed **Base:** `main` ← **Head:** `fix/node-tag-unique-scope` --- ### 📝 Commits (9) - [`686ca1f`](https://github.com/MHSanaei/3x-ui/commit/686ca1f6378a84b2a435084bb12c58edb9b3fe72) fix(multi-node): scope tag and email unique constraints per node - [`821e50d`](https://github.com/MHSanaei/3x-ui/commit/821e50da51a26de479a7d62c562f243de010e6a8) fix(inbound): prevent shared client_traffic row deletion when sibling inbound is deleted - [`41faaa2`](https://github.com/MHSanaei/3x-ui/commit/41faaa2b25cbd4d6547e78af3feeb83ec69ff5f6) Merge branch 'main' into fix/node-tag-unique-scope - [`dd18130`](https://github.com/MHSanaei/3x-ui/commit/dd18130ece579eae2beecd8683f10c35bcd2c3bc) Merge branch 'main' into fix/node-tag-unique-scope - [`341eb5b`](https://github.com/MHSanaei/3x-ui/commit/341eb5b63b8d4bc013671c05798b191a3b619e06) chore(xray): minor cleanups to graceful-shutdown follow-ups for #4259 - [`4ec84cf`](https://github.com/MHSanaei/3x-ui/commit/4ec84cf56cae96d7f2d5967f133ba56498ad1431) Merge branch 'main' into fix/node-tag-unique-scope - [`b8adda0`](https://github.com/MHSanaei/3x-ui/commit/b8adda0bec34154e22bec92eed2e312a16f588b3) Merge branch 'main' into fix/node-tag-unique-scope - [`05b24bd`](https://github.com/MHSanaei/3x-ui/commit/05b24bd9b061432df607610be7f2499c774c8358) Update db.go - [`3463aee`](https://github.com/MHSanaei/3x-ui/commit/3463aeef625c7b813ebe05337d610952bf105ca0) Merge branch 'main' into fix/node-tag-unique-scope ### 📊 Changes **7 files changed** (+110 additions, -25 deletions) <details> <summary>View changed files</summary> 📝 `database/db.go` (+52 -0) 📝 `database/model/model.go` (+7 -2) 📝 `web/service/inbound.go` (+37 -10) 📝 `web/service/port_conflict.go` (+10 -5) 📝 `xray/client_traffic.go` (+2 -2) 📝 `xray/process.go` (+0 -4) 📝 `xray/process_test.go` (+2 -2) </details> ### 📄 Description ## Problem When running a central panel with multiple remote nodes, the traffic sync silently drops inbounds from some nodes. The server log shows: ``` WARNING - setRemoteTraffic: create central inbound for tag inbound-48055 failed: UNIQUE constraint failed: inbounds.tag WARNING - node traffic sync: merge for $NODE_NAME failed: UNIQUE constraint failed: client_traffics.email ``` **Two global UNIQUE constraints** are responsible: 1. **`inbounds.tag`** (`gorm:"unique"`) -- if Node A has an inbound on port 48055 (tag = `inbound-48055`), Node B cannot sync its inbound on the same port. The tag must be globally unique, even though each node runs a separate xray instance. 2. **`client_traffics.email`** (`gorm:"unique"`) -- if Node A has a client with email `foo@bar`, and Node B also has a client with that email, the client stats CREATE fails and **rolls back the entire transaction** -- including all inbounds that were successfully created in the same cycle. The node stays at 0 inbounds permanently, retrying every 10s and failing each time. ## Root cause Tags and client emails only need to be unique **within the same xray instance**. A tag like `inbound-48055` on Node A is a completely different xray socket from `inbound-48055` on Node B -- they are on different machines. The same applies to client emails. ## Changes 4 files, +67/-9 - **`database/model/model.go`**: Changed `Tag` from `gorm:"unique"` (global) to `uniqueIndex:idx_node_tag` composited with `NodeID`. - **`xray/client_traffic.go`**: Changed `Email` from `gorm:"unique"` (global) to `uniqueIndex:idx_ct_inbound_email` composited with `InboundId`. - **`web/service/port_conflict.go`**: `tagExists()` scopes by `node_id` so the UI does not see remote tags as conflicts. - **`database/db.go`**: Added `migrateLegacyUniqueConstraints()` that removes old UNIQUE constraints from existing databases on startup and creates the new composite indexes. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-10-31 20:48:22 +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#5644
No description provided.