|
4 | 4 | // without creating import cycles. |
5 | 5 | package shutdown |
6 | 6 |
|
7 | | -import "sync" |
| 7 | +import ( |
| 8 | + "fmt" |
| 9 | + "runtime" |
| 10 | + "strings" |
| 11 | + "sync" |
| 12 | +) |
| 13 | + |
| 14 | +// hook pairs a registered function with the source location that registered it. |
| 15 | +type hook struct { |
| 16 | + fn func() |
| 17 | + caller string // "file.go:line" |
| 18 | +} |
8 | 19 |
|
9 | 20 | var ( |
10 | 21 | mu sync.Mutex |
11 | | - hooks []func() |
| 22 | + hooks []hook |
12 | 23 | ) |
13 | 24 |
|
14 | 25 | // Register appends fn to the list of functions that will be called by Run. |
| 26 | +// The call site (file:line) is captured at registration time and printed |
| 27 | +// when the hook fires during shutdown. |
15 | 28 | // Hooks are invoked in registration order. |
16 | 29 | func Register(fn func()) { |
| 30 | + caller := captureCallerOutsidePackage() |
17 | 31 | mu.Lock() |
18 | 32 | defer mu.Unlock() |
19 | | - hooks = append(hooks, fn) |
| 33 | + hooks = append(hooks, hook{fn: fn, caller: caller}) |
20 | 34 | } |
21 | 35 |
|
22 | | -// Run calls every registered hook in registration order. |
| 36 | +// Run calls every registered hook in registration order, printing the |
| 37 | +// originating source location before each one. |
23 | 38 | // It is safe to call from multiple goroutines; only the first call executes |
24 | 39 | // the hooks — subsequent calls are no-ops. |
25 | 40 | func Run() { |
26 | 41 | mu.Lock() |
27 | | - fns := make([]func(), len(hooks)) |
| 42 | + fns := make([]hook, len(hooks)) |
28 | 43 | copy(fns, hooks) |
29 | 44 | hooks = nil // prevent double-execution |
30 | 45 | mu.Unlock() |
31 | 46 |
|
32 | | - for _, fn := range fns { |
33 | | - fn() |
| 47 | + for _, h := range fns { |
| 48 | + fmt.Printf("shutting down %s\n", h.caller) |
| 49 | + h.fn() |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +// captureCallerOutsidePackage walks the call stack and returns the file:line |
| 54 | +// of the first frame that belongs neither to this package nor to the evo |
| 55 | +// root-package wrapper (evo.OnShutdown). This means the location always |
| 56 | +// points to the actual user/connector code that registered the hook. |
| 57 | +func captureCallerOutsidePackage() string { |
| 58 | + pcs := make([]uintptr, 16) |
| 59 | + // Skip runtime.Callers + captureCallerOutsidePackage + Register itself. |
| 60 | + n := runtime.Callers(3, pcs) |
| 61 | + frames := runtime.CallersFrames(pcs[:n]) |
| 62 | + for { |
| 63 | + frame, more := frames.Next() |
| 64 | + fn := frame.Function |
| 65 | + // Skip frames that are part of the shutdown registry itself or the |
| 66 | + // thin evo.OnShutdown wrapper so the location always points to the |
| 67 | + // caller's code. |
| 68 | + if !strings.Contains(fn, "getevo/evo/v2/lib/shutdown") && |
| 69 | + fn != "github.com/getevo/evo/v2.OnShutdown" { |
| 70 | + return fmt.Sprintf("%s:%d", frame.File, frame.Line) |
| 71 | + } |
| 72 | + if !more { |
| 73 | + break |
| 74 | + } |
34 | 75 | } |
| 76 | + return "unknown" |
35 | 77 | } |
0 commit comments