@marcel91200 / caddy Mirror · read-only / commits / a686f7c3
Fork 0

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: Only set memory/CPU limits on run (fix #6879)

Matthew Holt committed Mar 6, 2025 at 15:11 UTC a686f7c346fe011ad153a3bd4ac3e31e6758bcce
2 files changed +35 -32
cmd/commandfuncs.go
+11 -8
@@ -171,6 +171,9 @@ func cmdStart(fl Flags) (int, error) {
171 func cmdRun(fl Flags) (int, error) {
172 caddy.TrapSignals()
173
174 + logger := caddy.Log()
175 + setResourceLimits(logger)
176 +
177 configFlag := fl.String("config")
178 configAdapterFlag := fl.String("adapter")
179 resumeFlag := fl.Bool("resume")
@@ -196,18 +199,18 @@ func cmdRun(fl Flags) (int, error) {
199 config, err = os.ReadFile(caddy.ConfigAutosavePath)
200 if errors.Is(err, fs.ErrNotExist) {
201 // not a bad error; just can't resume if autosave file doesn't exist
199 - caddy.Log().Info("no autosave file exists", zap.String("autosave_file", caddy.ConfigAutosavePath))
202 + logger.Info("no autosave file exists", zap.String("autosave_file", caddy.ConfigAutosavePath))
203 resumeFlag = false
204 } else if err != nil {
205 return caddy.ExitCodeFailedStartup, err
206 } else {
207 if configFlag == "" {
205 - caddy.Log().Info("resuming from last configuration",
208 + logger.Info("resuming from last configuration",
209 zap.String("autosave_file", caddy.ConfigAutosavePath))
210 } else {
211 // if they also specified a config file, user should be aware that we're not
212 // using it (doing so could lead to data/config loss by overwriting!)
210 - caddy.Log().Warn("--config and --resume flags were used together; ignoring --config and resuming from last configuration",
213 + logger.Warn("--config and --resume flags were used together; ignoring --config and resuming from last configuration",
214 zap.String("autosave_file", caddy.ConfigAutosavePath))
215 }
216 }
@@ -225,7 +228,7 @@ func cmdRun(fl Flags) (int, error) {
228 if pidfileFlag != "" {
229 err := caddy.PIDFile(pidfileFlag)
230 if err != nil {
228 - caddy.Log().Error("unable to write PID file",
231 + logger.Error("unable to write PID file",
232 zap.String("pidfile", pidfileFlag),
233 zap.Error(err))
234 }
@@ -236,7 +239,7 @@ func cmdRun(fl Flags) (int, error) {
239 if err != nil {
240 return caddy.ExitCodeFailedStartup, fmt.Errorf("loading initial config: %v", err)
241 }
239 - caddy.Log().Info("serving initial configuration")
242 + logger.Info("serving initial configuration")
243
244 // if we are to report to another process the successful start
245 // of the server, do so now by echoing back contents of stdin
@@ -272,15 +275,15 @@ func cmdRun(fl Flags) (int, error) {
275 switch runtime.GOOS {
276 case "windows":
277 if os.Getenv("HOME") == "" && os.Getenv("USERPROFILE") == "" && !hasXDG {
275 - caddy.Log().Warn("neither HOME nor USERPROFILE environment variables are set - please fix; some assets might be stored in ./caddy")
278 + logger.Warn("neither HOME nor USERPROFILE environment variables are set - please fix; some assets might be stored in ./caddy")
279 }
280 case "plan9":
281 if os.Getenv("home") == "" && !hasXDG {
279 - caddy.Log().Warn("$home environment variable is empty - please fix; some assets might be stored in ./caddy")
282 + logger.Warn("$home environment variable is empty - please fix; some assets might be stored in ./caddy")
283 }
284 default:
285 if os.Getenv("HOME") == "" && !hasXDG {
283 - caddy.Log().Warn("$HOME environment variable is empty - please fix; some assets might be stored in ./caddy")
286 + logger.Warn("$HOME environment variable is empty - please fix; some assets might be stored in ./caddy")
287 }
288 }
289
cmd/main.go
+24 -24
@@ -69,30 +69,6 @@ func Main() {
69 os.Exit(caddy.ExitCodeFailedStartup)
70 }
71
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 -
72 if err := defaultFactory.Build().Execute(); err != nil {
73 var exitError *exitError
74 if errors.As(err, &exitError) {
@@ -488,6 +464,30 @@ func printEnvironment() {
464 }
465 }
466
467 +func setResourceLimits(logger *zap.Logger) {
468 + // Configure the maximum number of CPUs to use to match the Linux container quota (if any)
469 + // See https://pkg.go.dev/runtime#GOMAXPROCS
470 + undo, err := maxprocs.Set(maxprocs.Logger(logger.Sugar().Infof))
471 + defer undo()
472 + if err != nil {
473 + caddy.Log().Warn("failed to set GOMAXPROCS", zap.Error(err))
474 + }
475 +
476 + // Configure the maximum memory to use to match the Linux container quota (if any) or system memory
477 + // See https://pkg.go.dev/runtime/debug#SetMemoryLimit
478 + _, _ = memlimit.SetGoMemLimitWithOpts(
479 + memlimit.WithLogger(
480 + slog.New(zapslog.NewHandler(logger.Core())),
481 + ),
482 + memlimit.WithProvider(
483 + memlimit.ApplyFallback(
484 + memlimit.FromCgroup,
485 + memlimit.FromSystem,
486 + ),
487 + ),
488 + )
489 +}
490 +
491 // StringSlice is a flag.Value that enables repeated use of a string flag.
492 type StringSlice []string
493