mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-12-01 06:54:47 +03:00
1ec55aed30
Every five seconds the master fetched each node's complete inbound list, settings JSON and enrichment included, parsed it and reconciled every client, whether anything had changed or not. A node that advertises traffic-snapshot-v1 now answers a revisioned endpoint: counters and lifecycle state on every poll, inbound configuration and attachments only when its revision changed. The master skips structural parsing and client reconciliation while the node's revision, its own rows for that node, the lifecycle state and the full remote-to-central inbound mapping are all unchanged; anything else falls back to the full merge. Old and new panels keep talking through the previous endpoints. Revisions come from a hidden per-row token on inbounds and clients, maintained by database triggers on SQLite and PostgreSQL so that direct and background writes count too. Counter and timestamp updates leave it alone. The triggers are dropped before migrations and recreated after them, and the previous build runs unchanged on a database that carries them. Measured here at 20,000 clients on one node, per idle poll: master SQL statements 551 -> 44, node statements 55 -> 4, transfer 23.0 MB -> 4.8 MB. When every client is shared between nodes the ownership checks keep the full master merge, so only the node and the transfer get cheaper. The node still reads every membership and counter on each poll. Closes #7007
144 lines
4.9 KiB
Go
144 lines
4.9 KiB
Go
package runtime
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"fmt"
|
|
|
|
"github.com/mhsanaei/3x-ui/v3/internal/database/model"
|
|
"github.com/mhsanaei/3x-ui/v3/internal/util/json_util"
|
|
)
|
|
|
|
// Counters and memberships allow million-client deployments; catalogs retain the legacy cap.
|
|
const maxNodeSnapshotClients = 1 << 20
|
|
|
|
// Count raw entries before allocating typed rows, including for an oversized list of empty objects.
|
|
func decodeSnapshotList[T any](raw json.RawMessage, limit int) ([]T, error) {
|
|
count, err := json_util.CountCappedList(raw, limit)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if len(raw) == 0 || bytes.Equal(bytes.TrimSpace(raw), []byte("null")) {
|
|
return nil, nil
|
|
}
|
|
result := make([]T, 0, count)
|
|
if err := json.Unmarshal(raw, &result); err != nil {
|
|
return nil, err
|
|
}
|
|
return result, nil
|
|
}
|
|
|
|
func decodeSnapshotInbounds(raw json.RawMessage) ([]*model.Inbound, error) {
|
|
rows, err := json_util.DecodeCappedList[json.RawMessage](raw, maxNodeInbounds)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if len(raw) == 0 || bytes.Equal(bytes.TrimSpace(raw), []byte("null")) {
|
|
return nil, nil
|
|
}
|
|
result := make([]*model.Inbound, len(rows))
|
|
for i, row := range rows {
|
|
if bytes.Equal(bytes.TrimSpace(row), []byte("null")) {
|
|
continue
|
|
}
|
|
type inboundAlias model.Inbound
|
|
inbound := &model.Inbound{}
|
|
fields := struct {
|
|
*inboundAlias
|
|
Settings json.RawMessage `json:"settings"`
|
|
StreamSettings json.RawMessage `json:"streamSettings"`
|
|
Sniffing json.RawMessage `json:"sniffing"`
|
|
ClientStats json.RawMessage `json:"clientStats"`
|
|
}{inboundAlias: (*inboundAlias)(inbound)}
|
|
if err := json.Unmarshal(row, &fields); err != nil {
|
|
return nil, err
|
|
}
|
|
// These legacy stats are replaced by the separately bounded compact counters.
|
|
if _, err := json_util.CountCappedList(fields.ClientStats, maxNodeSnapshotClients); err != nil {
|
|
return nil, err
|
|
}
|
|
inbound.Settings = snapshotConfigString(fields.Settings)
|
|
inbound.StreamSettings = snapshotConfigString(fields.StreamSettings)
|
|
inbound.Sniffing = snapshotConfigString(fields.Sniffing)
|
|
result[i] = inbound
|
|
}
|
|
|
|
return result, nil
|
|
}
|
|
|
|
func snapshotConfigString(raw json.RawMessage) string {
|
|
raw = bytes.TrimSpace(raw)
|
|
if len(raw) == 0 || bytes.Equal(raw, []byte("null")) {
|
|
return ""
|
|
}
|
|
if raw[0] == '"' {
|
|
var value string
|
|
if err := json.Unmarshal(raw, &value); err == nil {
|
|
return value
|
|
}
|
|
}
|
|
return string(raw)
|
|
}
|
|
|
|
func decodeNodeTrafficSnapshot(raw json.RawMessage) (NodeTrafficSnapshotV1, error) {
|
|
var fields struct {
|
|
Version int `json:"version"`
|
|
Revision string `json:"revision"`
|
|
Structure json.RawMessage `json:"structure"`
|
|
InboundTraffic json.RawMessage `json:"inboundTraffic"`
|
|
Clients json.RawMessage `json:"clients"`
|
|
OnlineEmails json.RawMessage `json:"onlineEmails"`
|
|
OnlineTree json.RawMessage `json:"onlineTree"`
|
|
ActiveInboundTree json.RawMessage `json:"activeInboundTree"`
|
|
LastOnlineMap json.RawMessage `json:"lastOnlineMap"`
|
|
}
|
|
var wire NodeTrafficSnapshotV1
|
|
if err := json.Unmarshal(raw, &fields); err != nil {
|
|
return wire, err
|
|
}
|
|
wire.Version, wire.Revision = fields.Version, fields.Revision
|
|
if len(fields.Structure) > 0 && !bytes.Equal(bytes.TrimSpace(fields.Structure), []byte("null")) {
|
|
var structure struct {
|
|
Inbounds json.RawMessage `json:"inbounds"`
|
|
Attachments json.RawMessage `json:"attachments"`
|
|
}
|
|
if err := json.Unmarshal(fields.Structure, &structure); err != nil {
|
|
return wire, err
|
|
}
|
|
wire.Structure = &NodeSnapshotStructure{}
|
|
var err error
|
|
if wire.Structure.Inbounds, err = decodeSnapshotInbounds(structure.Inbounds); err != nil {
|
|
return wire, fmt.Errorf("structure.inbounds: %w", err)
|
|
}
|
|
if wire.Structure.Attachments, err = decodeSnapshotList[NodeSnapshotAttachment](structure.Attachments, maxNodeSnapshotClients); err != nil {
|
|
return wire, fmt.Errorf("structure.attachments: %w", err)
|
|
}
|
|
}
|
|
var err error
|
|
if wire.Clients, err = decodeSnapshotList[NodeClientTraffic](fields.Clients, maxNodeSnapshotClients); err != nil {
|
|
return wire, fmt.Errorf("clients: %w", err)
|
|
}
|
|
if wire.InboundTraffic, err = decodeSnapshotList[NodeInboundTraffic](fields.InboundTraffic, maxNodeInbounds); err != nil {
|
|
return wire, fmt.Errorf("inboundTraffic: %w", err)
|
|
}
|
|
if wire.OnlineEmails, err = decodeSnapshotList[string](fields.OnlineEmails, maxNodeMapValues); err != nil {
|
|
return wire, fmt.Errorf("onlineEmails: %w", err)
|
|
}
|
|
for _, entry := range []struct {
|
|
name string
|
|
raw json.RawMessage
|
|
target any
|
|
}{
|
|
{"onlineTree", fields.OnlineTree, &wire.OnlineTree},
|
|
{"activeInboundTree", fields.ActiveInboundTree, &wire.ActiveInboundTree},
|
|
{"lastOnlineMap", fields.LastOnlineMap, &wire.LastOnlineMap},
|
|
} {
|
|
if len(entry.raw) > 0 {
|
|
if err := decodeCappedValues(entry.raw, entry.target, maxNodeMapValues); err != nil {
|
|
return wire, fmt.Errorf("%s: %w", entry.name, err)
|
|
}
|
|
}
|
|
}
|
|
return wire, nil
|
|
}
|