diff options
Diffstat (limited to 'web/job')
| -rw-r--r-- | web/job/xray_traffic_job.go | 32 |
1 files changed, 19 insertions, 13 deletions
diff --git a/web/job/xray_traffic_job.go b/web/job/xray_traffic_job.go index 8d2bfbd6..f443aa77 100644 --- a/web/job/xray_traffic_job.go +++ b/web/job/xray_traffic_job.go @@ -50,7 +50,13 @@ func (j *XrayTrafficJob) Run() { j.xrayService.SetToNeedRestart() } - // Get online clients and last online map for real-time status updates + // If no frontend client is connected, skip all WebSocket broadcasting routines, + // including expensive DB queries for online clients and JSON marshaling. + if !websocket.HasClients() { + return + } + + // Update online clients list and map onlineClients := j.inboundService.GetOnlineClients() lastOnlineMap, err := j.inboundService.GetClientsLastOnline() if err != nil { @@ -58,8 +64,17 @@ func (j *XrayTrafficJob) Run() { lastOnlineMap = make(map[string]int64) } + // Broadcast traffic update (deltas and online stats) via WebSocket + trafficUpdate := map[string]any{ + "traffics": traffics, + "clientTraffics": clientTraffics, + "onlineClients": onlineClients, + "lastOnlineMap": lastOnlineMap, + } + websocket.BroadcastTraffic(trafficUpdate) + // Fetch updated inbounds from database with accumulated traffic values - // This ensures frontend receives the actual total traffic, not just delta values + // This ensures frontend receives the actual total traffic for real-time UI refresh. updatedInbounds, err := j.inboundService.GetAllInbounds() if err != nil { logger.Warning("get all inbounds for websocket failed:", err) @@ -70,16 +85,8 @@ func (j *XrayTrafficJob) Run() { logger.Warning("get all outbounds for websocket failed:", err) } - // Broadcast traffic update via WebSocket with accumulated values from database - trafficUpdate := map[string]any{ - "traffics": traffics, - "clientTraffics": clientTraffics, - "onlineClients": onlineClients, - "lastOnlineMap": lastOnlineMap, - } - websocket.BroadcastTraffic(trafficUpdate) - - // Broadcast full inbounds update for real-time UI refresh + // The WebSocket hub will automatically check the payload size. + // If it exceeds 100MB, it sends a lightweight 'invalidate' signal instead. if updatedInbounds != nil { websocket.BroadcastInbounds(updatedInbounds) } @@ -87,7 +94,6 @@ func (j *XrayTrafficJob) Run() { if updatedOutbounds != nil { websocket.BroadcastOutbounds(updatedOutbounds) } - } func (j *XrayTrafficJob) informTrafficToExternalAPI(inboundTraffics []*xray.Traffic, clientTraffics []*xray.ClientTraffic) { |
