From a9d22498c2784fae2450429ea2d61aec0bc4bb26 Mon Sep 17 00:00:00 2001 From: Hamed Date: Sun, 29 Nov 2026 18:25:19 +0330 Subject: [PATCH] feat(traffic): separate depletion restarts from client disables (#7267) One switch decided two things: whether Xray restarts when a client is disabled or deleted by hand, and whether it restarts when a client runs out of quota or expires. A panel that takes many API deletes had to choose between a restart on each of them and leaving depleted clients' open connections alive. Add a switch for the quota and expiry case. It inherits the old switch until it is saved, and older settings clients may omit it. A depletion asks for one restart per affected running core per committed batch: the local core when a local inbound served the client, and each affected node through the node API. A master whose nodes alone served the client is not restarted. With the new switch off, a depleted client is still removed from the running core and from its config snapshot, so a later reconcile does not restart for it either. Also add a node traffic fetch timeout setting (1 to 30 seconds, default 4). Run on a panel with real cores: with the old switch off and the new one on, a stream through a client that crossed its quota was cut within one poll and the lifecycle log names client_depletion; with them swapped, the stream and the core survived. A depletion a node detects itself follows that node's own switches. Refs #7145 --- docs/content/docs/en/config/clients.mdx | 8 +- docs/content/docs/en/config/panel.mdx | 13 ++ docs/public/openapi.json | 18 ++ frontend/public/openapi.json | 18 ++ frontend/src/generated/examples.ts | 4 + frontend/src/generated/schemas.ts | 18 ++ frontend/src/generated/types.ts | 4 + frontend/src/generated/zod.ts | 4 + frontend/src/models/setting.ts | 2 + frontend/src/pages/settings/GeneralTab.tsx | 20 +++ frontend/src/schemas/setting.ts | 2 + internal/web/entity/entity.go | 40 +++-- internal/web/job/node_traffic_sync_job.go | 22 +-- internal/web/job/xray_traffic_job.go | 13 +- internal/web/job/xray_traffic_retry_test.go | 4 +- .../web/service/client_depletion_settings.go | 31 ++++ internal/web/service/inbound_disable.go | 4 +- internal/web/service/inbound_node.go | 13 +- internal/web/service/inbound_traffic.go | 5 +- internal/web/service/inbound_traffic_apply.go | 27 ++- .../web/service/server_xray_update_test.go | 24 +++ internal/web/service/setting.go | 42 ++++- .../service/setting_factory_defaults_test.go | 5 +- .../traffic_depletion_node_push_test.go | 5 +- internal/web/service/xray.go | 26 ++- .../web/service/xray_depletion_restart.go | 48 ++++++ .../service/xray_depletion_restart_test.go | 161 ++++++++++++++++++ .../service/xray_lifecycle_telemetry_test.go | 9 +- internal/web/translation/ar-EG.json | 2 + internal/web/translation/en-US.json | 4 +- internal/web/translation/es-ES.json | 2 + internal/web/translation/fa-IR.json | 4 +- internal/web/translation/id-ID.json | 2 + internal/web/translation/ja-JP.json | 2 + internal/web/translation/pt-BR.json | 2 + internal/web/translation/ru-RU.json | 2 + internal/web/translation/sk-SK.json | 2 + internal/web/translation/tr-TR.json | 2 + internal/web/translation/uk-UA.json | 2 + internal/web/translation/vi-VN.json | 2 + internal/web/translation/zh-CN.json | 2 + internal/web/translation/zh-TW.json | 2 + internal/xray/process.go | 43 +++++ 43 files changed, 585 insertions(+), 80 deletions(-) create mode 100644 internal/web/service/client_depletion_settings.go create mode 100644 internal/web/service/xray_depletion_restart.go create mode 100644 internal/web/service/xray_depletion_restart_test.go diff --git a/docs/content/docs/en/config/clients.mdx b/docs/content/docs/en/config/clients.mdx index 79c8a3300..9fdff5b7f 100644 --- a/docs/content/docs/en/config/clients.mdx +++ b/docs/content/docs/en/config/clients.mdx @@ -27,9 +27,11 @@ inbounds** at once, with per-client traffic accounting. | **Comment** | all | Free-text note. | - Reaching the **traffic** or **expiry** limit disables the client, and a client - disabled or deleted by hand counts too; the panel restarts Xray then - (`restartXrayOnClientDisable`, on by default). + Reaching the **traffic** or **expiry** limit disables the client. + `restartXrayOnClientDepleted` controls whether Xray restarts to close the + client's existing sessions. Manual disable and delete use + `restartXrayOnClientDisable`. The new setting inherits the old value until + explicitly configured. ## Block a subscription device diff --git a/docs/content/docs/en/config/panel.mdx b/docs/content/docs/en/config/panel.mdx index d10faef2d..680fdba17 100644 --- a/docs/content/docs/en/config/panel.mdx +++ b/docs/content/docs/en/config/panel.mdx @@ -172,3 +172,16 @@ a 2xx status within 3 seconds, the panel drops that batch, logs one warning with the reason, and logs again when deliveries resume. Deltas waiting for the next send are held in memory: disabling the webhook or a panel process exit discards them. + +### Xray restart policy + +**Restart Xray on client quota or expiry** is independent of the existing +disable/delete switch. It can close already accepted sessions for clients +disabled automatically on the panel or on a managed node. Restarts are coalesced +per depletion batch and per affected node. Remote restart requests use a bounded +best-effort call. If the new setting is unset, it inherits the old switch until +saved explicitly. + +**Node traffic fetch timeout** defaults to 4 seconds and accepts values from +1 to 30 seconds. It applies to the snapshot fetch. Local traffic polling keeps +its existing cadence. diff --git a/docs/public/openapi.json b/docs/public/openapi.json index 93229c43e..bb735ee53 100644 --- a/docs/public/openapi.json +++ b/docs/public/openapi.json @@ -177,6 +177,10 @@ "minimum": 1, "type": "integer" }, + "nodeTrafficFetchTimeout": { + "example": 4, + "type": "integer" + }, "outboundDownThreshold": { "maximum": 100, "minimum": 1, @@ -211,6 +215,10 @@ "remarkTemplate": { "type": "string" }, + "restartXrayOnClientDepleted": { + "nullable": true, + "type": "boolean" + }, "restartXrayOnClientDisable": { "type": "boolean" }, @@ -733,6 +741,7 @@ "networkTrafficInterface", "networkTrafficMode", "nodeDownThreshold", + "nodeTrafficFetchTimeout", "outboundDownThreshold", "pageSize", "panelOutbound", @@ -1075,6 +1084,10 @@ "minimum": 1, "type": "integer" }, + "nodeTrafficFetchTimeout": { + "example": 4, + "type": "integer" + }, "outboundDownThreshold": { "maximum": 100, "minimum": 1, @@ -1109,6 +1122,10 @@ "remarkTemplate": { "type": "string" }, + "restartXrayOnClientDepleted": { + "nullable": true, + "type": "boolean" + }, "restartXrayOnClientDisable": { "type": "boolean" }, @@ -1641,6 +1658,7 @@ "networkTrafficInterface", "networkTrafficMode", "nodeDownThreshold", + "nodeTrafficFetchTimeout", "outboundDownThreshold", "pageSize", "panelOutbound", diff --git a/frontend/public/openapi.json b/frontend/public/openapi.json index 93229c43e..bb735ee53 100644 --- a/frontend/public/openapi.json +++ b/frontend/public/openapi.json @@ -177,6 +177,10 @@ "minimum": 1, "type": "integer" }, + "nodeTrafficFetchTimeout": { + "example": 4, + "type": "integer" + }, "outboundDownThreshold": { "maximum": 100, "minimum": 1, @@ -211,6 +215,10 @@ "remarkTemplate": { "type": "string" }, + "restartXrayOnClientDepleted": { + "nullable": true, + "type": "boolean" + }, "restartXrayOnClientDisable": { "type": "boolean" }, @@ -733,6 +741,7 @@ "networkTrafficInterface", "networkTrafficMode", "nodeDownThreshold", + "nodeTrafficFetchTimeout", "outboundDownThreshold", "pageSize", "panelOutbound", @@ -1075,6 +1084,10 @@ "minimum": 1, "type": "integer" }, + "nodeTrafficFetchTimeout": { + "example": 4, + "type": "integer" + }, "outboundDownThreshold": { "maximum": 100, "minimum": 1, @@ -1109,6 +1122,10 @@ "remarkTemplate": { "type": "string" }, + "restartXrayOnClientDepleted": { + "nullable": true, + "type": "boolean" + }, "restartXrayOnClientDisable": { "type": "boolean" }, @@ -1641,6 +1658,7 @@ "networkTrafficInterface", "networkTrafficMode", "nodeDownThreshold", + "nodeTrafficFetchTimeout", "outboundDownThreshold", "pageSize", "panelOutbound", diff --git a/frontend/src/generated/examples.ts b/frontend/src/generated/examples.ts index f5177877c..c25602e7d 100644 --- a/frontend/src/generated/examples.ts +++ b/frontend/src/generated/examples.ts @@ -44,6 +44,7 @@ export const EXAMPLES: Record = { "networkTrafficInterface": "eth0", "networkTrafficMode": "legacy", "nodeDownThreshold": 1, + "nodeTrafficFetchTimeout": 4, "outboundDownThreshold": 1, "pageSize": 0, "panelOutbound": "", @@ -53,6 +54,7 @@ export const EXAMPLES: Record = { "publicIpv6Override": "", "realityScanCandidates": "", "remarkTemplate": "", + "restartXrayOnClientDepleted": null, "restartXrayOnClientDisable": false, "sessionMaxAge": 1, "smtpCpu": 0, @@ -257,6 +259,7 @@ export const EXAMPLES: Record = { "networkTrafficInterface": "eth0", "networkTrafficMode": "legacy", "nodeDownThreshold": 1, + "nodeTrafficFetchTimeout": 4, "outboundDownThreshold": 1, "pageSize": 0, "panelOutbound": "", @@ -266,6 +269,7 @@ export const EXAMPLES: Record = { "publicIpv6Override": "", "realityScanCandidates": "", "remarkTemplate": "", + "restartXrayOnClientDepleted": null, "restartXrayOnClientDisable": false, "sessionMaxAge": 1, "smtpCpu": 0, diff --git a/frontend/src/generated/schemas.ts b/frontend/src/generated/schemas.ts index 5739c4ed3..f243fe1cb 100644 --- a/frontend/src/generated/schemas.ts +++ b/frontend/src/generated/schemas.ts @@ -151,6 +151,10 @@ export const SCHEMAS: Record = { "minimum": 1, "type": "integer" }, + "nodeTrafficFetchTimeout": { + "example": 4, + "type": "integer" + }, "outboundDownThreshold": { "maximum": 100, "minimum": 1, @@ -185,6 +189,10 @@ export const SCHEMAS: Record = { "remarkTemplate": { "type": "string" }, + "restartXrayOnClientDepleted": { + "nullable": true, + "type": "boolean" + }, "restartXrayOnClientDisable": { "type": "boolean" }, @@ -707,6 +715,7 @@ export const SCHEMAS: Record = { "networkTrafficInterface", "networkTrafficMode", "nodeDownThreshold", + "nodeTrafficFetchTimeout", "outboundDownThreshold", "pageSize", "panelOutbound", @@ -1049,6 +1058,10 @@ export const SCHEMAS: Record = { "minimum": 1, "type": "integer" }, + "nodeTrafficFetchTimeout": { + "example": 4, + "type": "integer" + }, "outboundDownThreshold": { "maximum": 100, "minimum": 1, @@ -1083,6 +1096,10 @@ export const SCHEMAS: Record = { "remarkTemplate": { "type": "string" }, + "restartXrayOnClientDepleted": { + "nullable": true, + "type": "boolean" + }, "restartXrayOnClientDisable": { "type": "boolean" }, @@ -1615,6 +1632,7 @@ export const SCHEMAS: Record = { "networkTrafficInterface", "networkTrafficMode", "nodeDownThreshold", + "nodeTrafficFetchTimeout", "outboundDownThreshold", "pageSize", "panelOutbound", diff --git a/frontend/src/generated/types.ts b/frontend/src/generated/types.ts index 8784dfbdd..04e1c4ff6 100644 --- a/frontend/src/generated/types.ts +++ b/frontend/src/generated/types.ts @@ -49,6 +49,7 @@ export interface AllSetting { networkTrafficInterface: string; networkTrafficMode: string; nodeDownThreshold: number; + nodeTrafficFetchTimeout: number; outboundDownThreshold: number; pageSize: number; panelOutbound: string; @@ -58,6 +59,7 @@ export interface AllSetting { publicIpv6Override: string; realityScanCandidates: string; remarkTemplate: string; + restartXrayOnClientDepleted?: boolean | null; restartXrayOnClientDisable: boolean; sessionMaxAge: number; smtpCpu: number; @@ -263,6 +265,7 @@ export interface AllSettingView { networkTrafficInterface: string; networkTrafficMode: string; nodeDownThreshold: number; + nodeTrafficFetchTimeout: number; outboundDownThreshold: number; pageSize: number; panelOutbound: string; @@ -272,6 +275,7 @@ export interface AllSettingView { publicIpv6Override: string; realityScanCandidates: string; remarkTemplate: string; + restartXrayOnClientDepleted?: boolean | null; restartXrayOnClientDisable: boolean; sessionMaxAge: number; smtpCpu: number; diff --git a/frontend/src/generated/zod.ts b/frontend/src/generated/zod.ts index e61e98f44..9f88ee452 100644 --- a/frontend/src/generated/zod.ts +++ b/frontend/src/generated/zod.ts @@ -59,6 +59,7 @@ export const AllSettingSchema = z.object({ networkTrafficInterface: z.string(), networkTrafficMode: z.enum(['legacy', 'auto', 'manual']), nodeDownThreshold: z.number().int().min(1).max(100), + nodeTrafficFetchTimeout: z.number().int(), outboundDownThreshold: z.number().int().min(1).max(100), pageSize: z.number().int().min(0).max(1000), panelOutbound: z.string(), @@ -68,6 +69,7 @@ export const AllSettingSchema = z.object({ publicIpv6Override: z.string(), realityScanCandidates: z.string(), remarkTemplate: z.string(), + restartXrayOnClientDepleted: z.boolean().nullable().optional(), restartXrayOnClientDisable: z.boolean(), sessionMaxAge: z.number().int().min(1).max(525600), smtpCpu: z.number().int().min(0).max(100), @@ -274,6 +276,7 @@ export const AllSettingViewSchema = z.object({ networkTrafficInterface: z.string(), networkTrafficMode: z.enum(['legacy', 'auto', 'manual']), nodeDownThreshold: z.number().int().min(1).max(100), + nodeTrafficFetchTimeout: z.number().int(), outboundDownThreshold: z.number().int().min(1).max(100), pageSize: z.number().int().min(0).max(1000), panelOutbound: z.string(), @@ -283,6 +286,7 @@ export const AllSettingViewSchema = z.object({ publicIpv6Override: z.string(), realityScanCandidates: z.string(), remarkTemplate: z.string(), + restartXrayOnClientDepleted: z.boolean().nullable().optional(), restartXrayOnClientDisable: z.boolean(), sessionMaxAge: z.number().int().min(1).max(525600), smtpCpu: z.number().int().min(0).max(100), diff --git a/frontend/src/models/setting.ts b/frontend/src/models/setting.ts index ec7e3b5c6..933a228a4 100644 --- a/frontend/src/models/setting.ts +++ b/frontend/src/models/setting.ts @@ -78,6 +78,8 @@ export class AllSetting { externalTrafficInformInterval = 5; externalTrafficInformURI = ''; restartXrayOnClientDisable = true; + restartXrayOnClientDepleted = true; + nodeTrafficFetchTimeout = 4; mcpEnable = false; subCertFile = ''; subKeyFile = ''; diff --git a/frontend/src/pages/settings/GeneralTab.tsx b/frontend/src/pages/settings/GeneralTab.tsx index 571aae5d2..f65525a7a 100644 --- a/frontend/src/pages/settings/GeneralTab.tsx +++ b/frontend/src/pages/settings/GeneralTab.tsx @@ -402,6 +402,26 @@ export default function GeneralTab({ allSetting, updateSetting }: GeneralTabProp onChange={(v) => updateSetting({ restartXrayOnClientDisable: v })} /> + + updateSetting({ restartXrayOnClientDepleted: v })} + /> + + + updateSetting({ nodeTrafficFetchTimeout: v }))} + /> +