Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 20 additions & 6 deletions cmd/commandfuncs.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,9 @@ func cmdRun(fl Flags) (int, error) {

undoMaxProcs := setResourceLimits(logger)
defer undoMaxProcs()
// release the local reference to the undo function so it can be GC'd;
// the deferred call above has already captured the actual function value.
undoMaxProcs = nil //nolint:ineffassign,wastedassign

configFlag := fl.String("config")
configAdapterFlag := fl.String("adapter")
Expand Down Expand Up @@ -252,12 +255,16 @@ func cmdRun(fl Flags) (int, error) {
logBuffer.FlushTo(defaultLogger)
return caddy.ExitCodeFailedStartup, fmt.Errorf("loading initial config: %v", err)
}
// release the reference to the config so it can be GC'd
config = nil //nolint:ineffassign,wastedassign

// at this stage the config will have replaced
// the default logger to the configured one, so
// we can now flush the buffered logs, then log
// that the config is running.
logBuffer.FlushTo(caddy.Log())
// at this stage the config will have replaced the
// default logger to the configured one, so we can
// log normally, now that the config is running.
// also clear our ref to the buffer so it can get GC'd
logger = caddy.Log()
defaultLogger = nil //nolint:ineffassign,wastedassign
logBuffer = nil //nolint:wastedassign,ineffassign
logger.Info("serving initial configuration")

// if we are to report to another process the successful start
Expand All @@ -273,12 +280,16 @@ func cmdRun(fl Flags) (int, error) {
return caddy.ExitCodeFailedStartup,
fmt.Errorf("dialing confirmation address: %v", err)
}
defer conn.Close()
_, err = conn.Write(confirmationBytes)
if err != nil {
return caddy.ExitCodeFailedStartup,
fmt.Errorf("writing confirmation bytes to %s: %v", pingbackFlag, err)
}
// close (non-defer because we `select {}` below)
// and release references so they can be GC'd
conn.Close()
confirmationBytes = nil //nolint:ineffassign,wastedassign
conn = nil //nolint:wastedassign,ineffassign
}

// if enabled, reload config file automatically on changes
Expand Down Expand Up @@ -306,6 +317,9 @@ func cmdRun(fl Flags) (int, error) {
}
}

// release the last local logger reference
logger = nil //nolint:wastedassign,ineffassign

select {}
}

Expand Down
10 changes: 10 additions & 0 deletions internal/logbuffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ type LogBufferCore struct {
level zapcore.LevelEnabler
}

type LogBufferCoreInterface interface {
zapcore.Core
FlushTo(*zap.Logger)
}

func NewLogBufferCore(level zapcore.LevelEnabler) *LogBufferCore {
return &LogBufferCore{
level: level,
Expand Down Expand Up @@ -70,3 +75,8 @@ func (c *LogBufferCore) FlushTo(logger *zap.Logger) {
c.entries = nil
c.fields = nil
}

var (
_ zapcore.Core = (*LogBufferCore)(nil)
_ LogBufferCoreInterface = (*LogBufferCore)(nil)
)
7 changes: 7 additions & 0 deletions logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,13 @@ func (logging *Logging) setupNewDefault(ctx Context) error {
)
}

// if we had a buffered core, flush its contents ASAP
// before we try to log anything else, so the order of
// logs is preserved
if oldBufferCore, ok := oldDefault.logger.Core().(*internal.LogBufferCore); ok {
oldBufferCore.FlushTo(newDefault.logger)
}

return nil
}

Expand Down
Loading