|
| 1 | +package server |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "net/http" |
| 8 | + |
| 9 | + lctx "github.com/hamba/logger/v2/ctx" |
| 10 | +) |
| 11 | + |
| 12 | +// PostStartHookFunc is a function called after server start. |
| 13 | +type PostStartHookFunc[T context.Context] func(T) error |
| 14 | + |
| 15 | +// PreShutdownHookFunc is a function called before server shutdown. |
| 16 | +type PreShutdownHookFunc func() error |
| 17 | + |
| 18 | +type postStartHookEntry[T context.Context] struct { |
| 19 | + fn PostStartHookFunc[T] |
| 20 | + doneCh chan struct{} |
| 21 | +} |
| 22 | + |
| 23 | +// MustAddPostStartHook adds a post-start hook, panicking if there is an error. |
| 24 | +func (s *GenericServer[T]) MustAddPostStartHook(name string, fn PostStartHookFunc[T]) { |
| 25 | + if err := s.AddPostStartHook(name, fn); err != nil { |
| 26 | + panic(err) |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +// AddPostStartHook adds a post-start hook. |
| 31 | +func (s *GenericServer[T]) AddPostStartHook(name string, fn PostStartHookFunc[T]) error { |
| 32 | + if name == "" { |
| 33 | + return errors.New("name is required") |
| 34 | + } |
| 35 | + if fn == nil { |
| 36 | + return errors.New("fn is required") |
| 37 | + } |
| 38 | + |
| 39 | + s.postStartHookMu.Lock() |
| 40 | + defer s.postStartHookMu.Unlock() |
| 41 | + |
| 42 | + if s.postStartHooksCalled { |
| 43 | + return errors.New("hooks have already been called") |
| 44 | + } |
| 45 | + if _, exists := s.postStartHooks[name]; exists { |
| 46 | + return fmt.Errorf("hook %q as it is already registered", name) |
| 47 | + } |
| 48 | + |
| 49 | + if s.postStartHooks == nil { |
| 50 | + s.postStartHooks = map[string]postStartHookEntry[T]{} |
| 51 | + } |
| 52 | + |
| 53 | + doneCh := make(chan struct{}) |
| 54 | + err := s.AddReadyzChecks(postStartHookHealth{ |
| 55 | + name: "postStartHook:" + name, |
| 56 | + doneCh: doneCh, |
| 57 | + }) |
| 58 | + if err != nil { |
| 59 | + return fmt.Errorf("adding readyz check: %w", err) |
| 60 | + } |
| 61 | + |
| 62 | + s.postStartHooks[name] = postStartHookEntry[T]{ |
| 63 | + fn: fn, |
| 64 | + doneCh: doneCh, |
| 65 | + } |
| 66 | + return nil |
| 67 | +} |
| 68 | + |
| 69 | +// MustAddPreShutdownHook adds a pre-shutdown hook, panicking if there is an error. |
| 70 | +func (s *GenericServer[T]) MustAddPreShutdownHook(name string, fn PreShutdownHookFunc) { |
| 71 | + if err := s.AddPreShutdownHook(name, fn); err != nil { |
| 72 | + panic(err) |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +// AddPreShutdownHook adds a pre-shutdown hook. |
| 77 | +func (s *GenericServer[T]) AddPreShutdownHook(name string, fn PreShutdownHookFunc) error { |
| 78 | + if name == "" { |
| 79 | + return errors.New("name is required") |
| 80 | + } |
| 81 | + if fn == nil { |
| 82 | + return errors.New("fn is required") |
| 83 | + } |
| 84 | + |
| 85 | + s.preShutdownHookMu.Lock() |
| 86 | + defer s.preShutdownHookMu.Unlock() |
| 87 | + |
| 88 | + if s.preShutdownHooksCalled { |
| 89 | + return errors.New("hooks have already been called") |
| 90 | + } |
| 91 | + if _, exists := s.preShutdownHooks[name]; exists { |
| 92 | + return fmt.Errorf("hook %q as it is already registered", name) |
| 93 | + } |
| 94 | + |
| 95 | + if s.preShutdownHooks == nil { |
| 96 | + s.preShutdownHooks = map[string]PreShutdownHookFunc{} |
| 97 | + } |
| 98 | + |
| 99 | + s.preShutdownHooks[name] = fn |
| 100 | + return nil |
| 101 | +} |
| 102 | + |
| 103 | +func (s *GenericServer[T]) runPostStartHooks(ctx T) { |
| 104 | + s.postStartHookMu.Lock() |
| 105 | + defer s.postStartHookMu.Unlock() |
| 106 | + |
| 107 | + s.postStartHooksCalled = true |
| 108 | + |
| 109 | + for name, entry := range s.postStartHooks { |
| 110 | + go s.runPostStartHook(ctx, name, entry) |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +func (s *GenericServer[T]) runPostStartHook(ctx T, name string, entry postStartHookEntry[T]) { |
| 115 | + defer func() { |
| 116 | + if v := recover(); v != nil { |
| 117 | + s.Log.Error("Panic while running post-start hook", |
| 118 | + lctx.Interface("error", v), |
| 119 | + lctx.Stack("stack"), |
| 120 | + ) |
| 121 | + } |
| 122 | + }() |
| 123 | + |
| 124 | + s.Log.Info("Running post-start hook", lctx.Str("hook", name)) |
| 125 | + |
| 126 | + if err := entry.fn(ctx); err != nil { |
| 127 | + s.Log.Error("Could not run post-start hook", lctx.Str("name", name), lctx.Err(err)) |
| 128 | + } |
| 129 | + close(entry.doneCh) |
| 130 | +} |
| 131 | + |
| 132 | +func (s *GenericServer[T]) hasPreShutdownHooks() bool { |
| 133 | + s.preShutdownHookMu.Lock() |
| 134 | + defer s.preShutdownHookMu.Unlock() |
| 135 | + |
| 136 | + return len(s.preShutdownHooks) > 0 |
| 137 | +} |
| 138 | + |
| 139 | +func (s *GenericServer[T]) runPreShutdownHooks() error { |
| 140 | + s.preShutdownHookMu.Lock() |
| 141 | + defer s.preShutdownHookMu.Unlock() |
| 142 | + |
| 143 | + s.preShutdownHooksCalled = true |
| 144 | + |
| 145 | + var errs error |
| 146 | + for name, fn := range s.preShutdownHooks { |
| 147 | + if err := s.runPreShutdownHook(name, fn); err != nil { |
| 148 | + errs = errors.Join(errs, err) |
| 149 | + } |
| 150 | + } |
| 151 | + return errs |
| 152 | +} |
| 153 | + |
| 154 | +func (s *GenericServer[T]) runPreShutdownHook(name string, fn PreShutdownHookFunc) error { |
| 155 | + defer func() { |
| 156 | + if v := recover(); v != nil { |
| 157 | + s.Log.Error("Panic while running pre-shutdown hook", |
| 158 | + lctx.Interface("error", v), |
| 159 | + lctx.Stack("stack"), |
| 160 | + ) |
| 161 | + } |
| 162 | + }() |
| 163 | + |
| 164 | + s.Log.Info("Running pre-shutdown hook", lctx.Str("hook", name)) |
| 165 | + |
| 166 | + if err := fn(); err != nil { |
| 167 | + return fmt.Errorf("running preshutdown hook %q: %w", name, err) |
| 168 | + } |
| 169 | + return nil |
| 170 | +} |
| 171 | + |
| 172 | +type postStartHookHealth struct { |
| 173 | + name string |
| 174 | + doneCh chan struct{} |
| 175 | +} |
| 176 | + |
| 177 | +func (h postStartHookHealth) Name() string { |
| 178 | + return h.name |
| 179 | +} |
| 180 | + |
| 181 | +func (h postStartHookHealth) Check(*http.Request) error { |
| 182 | + select { |
| 183 | + case <-h.doneCh: |
| 184 | + return nil |
| 185 | + default: |
| 186 | + return errors.New("not finished") |
| 187 | + } |
| 188 | +} |
0 commit comments