| 179 | } |
| 180 | |
| 181 | func gracefulShutdown(shutdownCtx stdContext.Context, sc *StartConfig, server *http.Server, logger *slog.Logger) { |
| 182 | <-shutdownCtx.Done() // wait until shutdown context is closed. |
| 183 | // note: is server if closed by other means this method is still run but is good as no-op |
| 184 | |
| 185 | timeout := sc.GracefulTimeout |
| 186 | if timeout == 0 { |
| 187 | timeout = 10 * time.Second |
| 188 | } |
| 189 | waitShutdownCtx, cancel := stdContext.WithTimeout(stdContext.Background(), timeout) |
| 190 | defer cancel() |
| 191 | |
| 192 | if err := server.Shutdown(waitShutdownCtx); err != nil { |
| 193 | // we end up here when listeners are not shut down within given timeout |
| 194 | if sc.OnShutdownError != nil { |
| 195 | sc.OnShutdownError(err) |
| 196 | return |
| 197 | } |
| 198 | logger.Error("failed to shut down server within given timeout", "error", err) |
| 199 | } |
| 200 | } |