@marcel91200 / caddy Mirror · read-only / commits / 1b6b422c
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 14, 05:15 UTC

Add cleanup callbacks to context

Matthew Holt committed May 29, 2019 at 23:10 UTC 1b6b422c638532d49e697242d9fcf1aa0c3fdc53
1 file changed +10
context.go
+10
@@ -24,6 +24,7 @@ type Context struct {
24 context.Context
25 moduleInstances map[string][]interface{}
26 cfg *Config
27 + cleanupFuncs []func()
28 }
29
30 // NewContext provides a new context derived from the given
@@ -40,6 +41,10 @@ func NewContext(ctx Context) (Context, context.CancelFunc) {
41 wrappedCancel := func() {
42 cancel()
43
44 + for _, f := range ctx.cleanupFuncs {
45 + f()
46 + }
47 +
48 for modName, modInstances := range newCtx.moduleInstances {
49 for _, inst := range modInstances {
50 if cu, ok := inst.(CleanerUpper); ok {
@@ -55,6 +60,11 @@ func NewContext(ctx Context) (Context, context.CancelFunc) {
60 return newCtx, wrappedCancel
61 }
62
63 +// OnCancel executes f when ctx is cancelled.
64 +func (ctx *Context) OnCancel(f func()) {
65 + ctx.cleanupFuncs = append(ctx.cleanupFuncs, f)
66 +}
67 +
68 // LoadModule decodes rawMsg into a new instance of mod and
69 // returns the value. If mod.New() does not return a pointer
70 // value, it is converted to one so that it is unmarshaled