Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS
Mirrored from https://github.com/Slazee/caddy.git
Synced Nov 12, 03:15 UTC
cmd: automatically set GOMEMLIMIT (#6809)
* feat: automatically set GOMEMLIMIT * add system support * comments * add logs
Kévin Dunglas committed
Jan 27, 2025 at 17:32 UTC
066d770409917b409d0bdc14cb5ba33d3e4cb33e
3 files changed
+28
-1
cmd/main.go
+22
-1
@@ -24,6 +24,7 @@ import (
24
"io"
25
"io/fs"
26
"log"
27
+ "log/slog"
28
"net"
29
"os"
30
"path/filepath"
@@ -33,10 +34,12 @@ import (
34
"strings"
35
"time"
36
37
+ "github.com/KimMachineGun/automemlimit/memlimit"
38
"github.com/caddyserver/certmagic"
39
"github.com/spf13/pflag"
40
"go.uber.org/automaxprocs/maxprocs"
41
"go.uber.org/zap"
42
+ "go.uber.org/zap/exp/zapslog"
43
44
"github.com/caddyserver/caddy/v2"
45
"github.com/caddyserver/caddy/v2/caddyconfig"
@@ -66,12 +69,30 @@ func Main() {
69
os.Exit(caddy.ExitCodeFailedStartup)
70
}
71
69
- undo, err := maxprocs.Set()
72
+ logger := caddy.Log()
73
+
74
+ // Configure the maximum number of CPUs to use to match the Linux container quota (if any)
75
+ // See https://pkg.go.dev/runtime#GOMAXPROCS
76
+ undo, err := maxprocs.Set(maxprocs.Logger(logger.Sugar().Infof))
77
defer undo()
78
if err != nil {
79
caddy.Log().Warn("failed to set GOMAXPROCS", zap.Error(err))
80
}
81
82
+ // Configure the maximum memory to use to match the Linux container quota (if any) or system memory
83
+ // See https://pkg.go.dev/runtime/debug#SetMemoryLimit
84
+ _, _ = memlimit.SetGoMemLimitWithOpts(
85
+ memlimit.WithLogger(
86
+ slog.New(zapslog.NewHandler(logger.Core())),
87
+ ),
88
+ memlimit.WithProvider(
89
+ memlimit.ApplyFallback(
90
+ memlimit.FromCgroup,
91
+ memlimit.FromSystem,
92
+ ),
93
+ ),
94
+ )
95
+
96
if err := defaultFactory.Build().Execute(); err != nil {
97
var exitError *exitError
98
if errors.As(err, &exitError) {
go.mod
+2
@@ -49,6 +49,7 @@ require (
49
50
require (
51
dario.cat/mergo v1.0.1 // indirect
52
+ github.com/KimMachineGun/automemlimit v0.7.0 // indirect
53
github.com/Microsoft/go-winio v0.6.0 // indirect
54
github.com/antlr4-go/antlr/v4 v4.13.0 // indirect
55
github.com/davecgh/go-spew v1.1.1 // indirect
@@ -63,6 +64,7 @@ require (
64
github.com/google/pprof v0.0.0-20231212022811-ec68065c825e // indirect
65
github.com/grpc-ecosystem/grpc-gateway/v2 v2.22.0 // indirect
66
github.com/onsi/ginkgo/v2 v2.13.2 // indirect
67
+ github.com/pbnjay/memory v0.0.0-20210728143218-7b4eea64cf58 // indirect
68
github.com/pmezard/go-difflib v1.0.0 // indirect
69
github.com/quic-go/qpack v0.5.1 // indirect
70
github.com/smallstep/go-attestation v0.4.4-0.20240109183208-413678f90935 // indirect
go.sum
+4
@@ -31,6 +31,8 @@ github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03
31
github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
32
github.com/BurntSushi/toml v1.4.0 h1:kuoIxZQy2WRRk1pttg9asf+WVv6tWQuBNVmK8+nqPr0=
33
github.com/BurntSushi/toml v1.4.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho=
34
+github.com/KimMachineGun/automemlimit v0.7.0 h1:7G06p/dMSf7G8E6oq+f2uOPuVncFyIlDI/pBWK49u88=
35
+github.com/KimMachineGun/automemlimit v0.7.0/go.mod h1:QZxpHaGOQoYvFhv/r4u3U0JTC2ZcOwbSr11UZF46UBM=
36
github.com/Masterminds/goutils v1.1.1 h1:5nUrii3FMTL5diU80unEVvNevw1nH4+ZV4DSLVJLSYI=
37
github.com/Masterminds/goutils v1.1.1/go.mod h1:8cTjp+g8YejhMuvIA5y2vz3BpJxksy863GQaJW2MFNU=
38
github.com/Masterminds/semver/v3 v3.1.1/go.mod h1:VPu/7SZ7ePZ3QOrcuXROw5FAcLl4a0cBrbBpGY/8hQs=
@@ -366,6 +368,8 @@ github.com/onsi/ginkgo/v2 v2.13.2/go.mod h1:XStQ8QcGwLyF4HdfcZB8SFOS/MWCgDuXMSBe
368
github.com/onsi/gomega v1.29.0 h1:KIA/t2t5UBzoirT4H9tsML45GEbo3ouUnBHsCfD2tVg=
369
github.com/onsi/gomega v1.29.0/go.mod h1:9sxs+SwGrKI0+PWe4Fxa9tFQQBG5xSsSbMXOI8PPpoQ=
370
github.com/openzipkin/zipkin-go v0.1.1/go.mod h1:NtoC/o8u3JlF1lSlyPNswIbeQH9bJTmOf0Erfk+hxe8=
371
+github.com/pbnjay/memory v0.0.0-20210728143218-7b4eea64cf58 h1:onHthvaw9LFnH4t2DcNVpwGmV9E1BkGknEliJkfwQj0=
372
+github.com/pbnjay/memory v0.0.0-20210728143218-7b4eea64cf58/go.mod h1:DXv8WO4yhMYhSNPKjeNKa5WY9YCIEBRbNzFFPJbWO6Y=
373
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
374
github.com/peterbourgon/diskv/v3 v3.0.1 h1:x06SQA46+PKIUftmEujdwSEpIx8kR+M9eLYsUxeYveU=
375
github.com/peterbourgon/diskv/v3 v3.0.1/go.mod h1:kJ5Ny7vLdARGU3WUuy6uzO6T0nb/2gWcT1JiBvRmb5o=