From 793994a3fef6ba4feee7483d7d57e6f07281e83d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E7=A5=A5=E8=B6=85?= Date: Sun, 2 Apr 2023 21:30:03 +0800 Subject: [PATCH] =?UTF-8?q?=E5=9C=A8=E8=8A=82=E7=82=B9=E5=90=AF=E5=8A=A8?= =?UTF-8?q?=E6=97=B6=E8=87=AA=E5=8A=A8=E8=B0=83=E6=95=B4=E7=B3=BB=E7=BB=9F?= =?UTF-8?q?=E5=86=85=E6=A0=B8=E5=8F=82=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/nodes/node.go | 54 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/internal/nodes/node.go b/internal/nodes/node.go index 326873c..0483e5c 100644 --- a/internal/nodes/node.go +++ b/internal/nodes/node.go @@ -44,6 +44,7 @@ import ( "runtime" "runtime/debug" "sort" + "strings" "sync" "syscall" "time" @@ -137,6 +138,9 @@ func (this *Node) Start() { remotelogs.Error("NODE", "initialize ip library failed: "+err.Error()) } + // 调整系统参数 + this.checkSystem() + // 检查硬盘类型 this.checkDisk() @@ -1243,6 +1247,56 @@ func (this *Node) reloadServer() { } } +// 检查系统 +func (this *Node) checkSystem() { + if runtime.GOOS != "linux" || os.Getgid() != 0 { + return + } + + type variable struct { + name string + minValue int + maxValue int + } + + const dir = "/proc/sys" + + for _, v := range []variable{ + {name: "net.core.somaxconn", minValue: 2048}, + {name: "net.ipv4.tcp_max_syn_backlog", minValue: 2048}, + {name: "net.core.netdev_max_backlog", minValue: 4096}, + {name: "net.ipv4.tcp_fin_timeout", maxValue: 10}, + {name: "net.ipv4.tcp_max_tw_buckets", minValue: 65535}, + {name: "net.core.rmem_default", minValue: 4 << 20}, + {name: "net.core.wmem_default", minValue: 4 << 20}, + {name: "net.core.rmem_max", minValue: 32 << 20}, + {name: "net.core.wmem_max", minValue: 32 << 20}, + } { + var path = dir + "/" + strings.Replace(v.name, ".", "/", -1) + data, err := os.ReadFile(path) + if err != nil { + continue + } + data = bytes.TrimSpace(data) + if len(data) == 0 { + continue + } + + var oldValue = types.Int(string(data)) + if v.minValue > 0 && oldValue < v.minValue { + err = os.WriteFile(path, []byte(types.String(v.minValue)), 0666) + if err == nil { + remotelogs.Println("NODE", "change kernel parameter '"+v.name+"' from '"+types.String(oldValue)+"' to '"+types.String(v.minValue)+"'") + } + } else if v.maxValue > 0 && oldValue > v.maxValue { + err = os.WriteFile(path, []byte(types.String(v.maxValue)), 0666) + if err == nil { + remotelogs.Println("NODE", "change kernel parameter '"+v.name+"' from '"+types.String(oldValue)+"' to '"+types.String(v.maxValue)+"'") + } + } + } +} + // 检查硬盘 func (this *Node) checkDisk() { if runtime.GOOS != "linux" {