Tunning and memory control
This commit is contained in:
+51
-8
@@ -7,22 +7,32 @@ import (
|
||||
)
|
||||
|
||||
type XrayNativeTuning struct {
|
||||
RuntimeGOMAXPROCS int `json:"runtime_gomaxprocs,omitempty"`
|
||||
MuxGlobalSessions int `json:"mux_global_sessions,omitempty"`
|
||||
TracePackets bool `json:"trace_packets,omitempty"`
|
||||
RuntimeGOMAXPROCS int `json:"runtime_gomaxprocs,omitempty"`
|
||||
MuxGlobalSessions int `json:"mux_global_sessions,omitempty"`
|
||||
MaxConcurrentConnections int `json:"max_concurrent_connections,omitempty"`
|
||||
MaxConcurrentXHTTPRequests int `json:"max_concurrent_xhttp_requests,omitempty"`
|
||||
XHTTPMaxSessions int `json:"xhttp_max_sessions,omitempty"`
|
||||
TracePackets bool `json:"trace_packets,omitempty"`
|
||||
}
|
||||
|
||||
const (
|
||||
defaultNativeRuntimeGOMAXPROCS = 0
|
||||
defaultNativeMuxGlobalSessions = 32768
|
||||
defaultNativeMuxGlobalSessions = 8192
|
||||
defaultNativeMaxConnections = 4096
|
||||
defaultNativeMaxXHTTPRequests = 8192
|
||||
|
||||
fixedNativeMuxMaxSessions = 128
|
||||
fixedNativeMuxMaxSessions = 64
|
||||
fixedNativeMuxUDPIdleMS = 120000
|
||||
fixedNativeMuxUDPReadBuffer = 256 * 1024
|
||||
fixedNativeMuxUDPWriteBuffer = 256 * 1024
|
||||
|
||||
defaultNativeXHTTPMaxSessions = 16384
|
||||
defaultNativeXHTTPBufferedPosts = 512
|
||||
defaultNativeXHTTPMaxSessions = 4096
|
||||
defaultNativeHTTP2MaxStreams = 256
|
||||
// Packet-up posts are also protected by byte budgets in xray_xhttp.go. Keep
|
||||
// the default reorder queue modest so thousands of unauthenticated sessions
|
||||
// cannot consume large amounts of memory merely by allocating empty channel
|
||||
// buffers. Operators may request more, up to the hard cap enforced there.
|
||||
defaultNativeXHTTPBufferedPosts = 64
|
||||
|
||||
// Do not impose an application-level lifetime on a connected XHTTP VPN
|
||||
// session. The official Xray server keeps a connected session for the
|
||||
@@ -35,6 +45,9 @@ const (
|
||||
var (
|
||||
nativeTuneRuntimeGOMAXPROCS atomic.Int64
|
||||
nativeTuneMuxGlobalSessions atomic.Int64
|
||||
nativeTuneMaxConnections atomic.Int64
|
||||
nativeTuneMaxXHTTPRequests atomic.Int64
|
||||
nativeTuneXHTTPMaxSessions atomic.Int64
|
||||
nativeTuneTracePackets atomic.Bool
|
||||
)
|
||||
|
||||
@@ -53,6 +66,15 @@ func normalizeNativeXrayTuning(t *XrayNativeTuning) XrayNativeTuning {
|
||||
if out.MuxGlobalSessions <= 0 {
|
||||
out.MuxGlobalSessions = defaultNativeMuxGlobalSessions
|
||||
}
|
||||
if out.MaxConcurrentConnections == 0 {
|
||||
out.MaxConcurrentConnections = defaultNativeMaxConnections
|
||||
}
|
||||
if out.MaxConcurrentXHTTPRequests == 0 {
|
||||
out.MaxConcurrentXHTTPRequests = defaultNativeMaxXHTTPRequests
|
||||
}
|
||||
if out.XHTTPMaxSessions == 0 {
|
||||
out.XHTTPMaxSessions = defaultNativeXHTTPMaxSessions
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
@@ -68,19 +90,40 @@ func applyNativeXrayTuning(t *XrayNativeTuning) XrayNativeTuning {
|
||||
runtime.GOMAXPROCS(gomax)
|
||||
nativeTuneRuntimeGOMAXPROCS.Store(int64(gomax))
|
||||
nativeTuneMuxGlobalSessions.Store(int64(out.MuxGlobalSessions))
|
||||
nativeTuneMaxConnections.Store(nativeLimitValue(out.MaxConcurrentConnections))
|
||||
nativeTuneMaxXHTTPRequests.Store(nativeLimitValue(out.MaxConcurrentXHTTPRequests))
|
||||
nativeTuneXHTTPMaxSessions.Store(nativeLimitValue(out.XHTTPMaxSessions))
|
||||
nativeTuneTracePackets.Store(out.TracePackets)
|
||||
return out
|
||||
}
|
||||
|
||||
// Native tuning limits use zero internally for unlimited. In configuration,
|
||||
// zero means "use the safe default" and any negative value disables the cap.
|
||||
func nativeLimitValue(v int) int64 {
|
||||
if v < 0 {
|
||||
return 0
|
||||
}
|
||||
return int64(v)
|
||||
}
|
||||
|
||||
func nativeRuntimeGOMAXPROCS() int { return int(nativeTuneRuntimeGOMAXPROCS.Load()) }
|
||||
func nativeMuxGlobalSessionLimit() int { return int(nativeTuneMuxGlobalSessions.Load()) }
|
||||
func nativeMaxConnectionLimit() int { return int(nativeTuneMaxConnections.Load()) }
|
||||
func nativeMaxXHTTPRequestLimit() int { return int(nativeTuneMaxXHTTPRequests.Load()) }
|
||||
func nativeXHTTPMaxSessionLimit() int { return int(nativeTuneXHTTPMaxSessions.Load()) }
|
||||
func nativeTracePacketsEnabled() bool { return nativeTuneTracePackets.Load() }
|
||||
|
||||
func nativeMuxMaxSessionLimit() int { return fixedNativeMuxMaxSessions }
|
||||
func nativeMuxUDPReadBufferSize() int { return fixedNativeMuxUDPReadBuffer }
|
||||
func nativeMuxUDPWriteBufferSize() int { return fixedNativeMuxUDPWriteBuffer }
|
||||
func nativeXHTTPMaxSessionLimit() int { return defaultNativeXHTTPMaxSessions }
|
||||
func nativeXHTTPBufferedPostLimit() int { return defaultNativeXHTTPBufferedPosts }
|
||||
func nativeHTTP2MaxConcurrentStreams() uint32 {
|
||||
limit := nativeMaxXHTTPRequestLimit()
|
||||
if limit <= 0 || limit > defaultNativeHTTP2MaxStreams {
|
||||
return defaultNativeHTTP2MaxStreams
|
||||
}
|
||||
return uint32(limit)
|
||||
}
|
||||
func nativeMuxUDPIdleTimeout() time.Duration {
|
||||
return fixedNativeMuxUDPIdleMS * time.Millisecond
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user