MCPcopy
hub / github.com/caddyserver/caddy / NewContextWithCause

Function NewContextWithCause

context.go:72–96  ·  view source on GitHub ↗

NewContextWithCause is like NewContext but returns a context.CancelCauseFunc. EXPERIMENTAL: This API is subject to change.

(ctx Context)

Source from the content-addressed store, hash-verified

70// NewContextWithCause is like NewContext but returns a context.CancelCauseFunc.
71// EXPERIMENTAL: This API is subject to change.
72func NewContextWithCause(ctx Context) (Context, context.CancelCauseFunc) {
73 newCtx := Context{moduleInstances: make(map[string][]Module), cfg: ctx.cfg, metricsRegistry: prometheus.NewPedanticRegistry()}
74 c, cancel := context.WithCancelCause(ctx.Context)
75 wrappedCancel := func(cause error) {
76 cancel(cause)
77
78 for _, f := range ctx.cleanupFuncs {
79 f()
80 }
81
82 for modName, modInstances := range newCtx.moduleInstances {
83 for _, inst := range modInstances {
84 if cu, ok := inst.(CleanerUpper); ok {
85 err := cu.Cleanup()
86 if err != nil {
87 log.Printf("[ERROR] %s (%p): cleanup: %v", modName, inst, err)
88 }
89 }
90 }
91 }
92 }
93 newCtx.Context = c
94 newCtx.initMetrics()
95 return newCtx, wrappedCancel
96}
97
98// OnCancel executes f when ctx is canceled.
99func (ctx *Context) OnCancel(f func()) {

Callers 2

NewContextFunction · 0.85
provisionContextFunction · 0.85

Calls 2

initMetricsMethod · 0.95
CleanupMethod · 0.65

Tested by

no test coverage detected