feat: tokenization start, waitlist for background task graceful exit

This commit is contained in:
2026-04-07 14:29:25 +02:00
parent 6dd51b7d04
commit 120c12a1f1
6 changed files with 35 additions and 23 deletions
+20 -21
View File
@@ -20,31 +20,39 @@ func (app *application) serve() error {
WriteTimeout: 30 * time.Second,
}
// Create a shutdownError channel. We will use this to receive any errors returned
// by the graceful Shutdown() function.
shutdownError := make(chan error)
go func() {
quit := make(chan os.Signal, 1)
// Intercept the signals, as before.
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
s := <-quit
// Update the log entry to say "shutting down server" instead of "caught signal".
app.logger.PrintInfo("shutting down server", map[string]string{
app.logger.PrintInfo("caught signal", map[string]string{
"signal": s.String(),
})
// Create a context with a 5-second timeout.
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
// Call Shutdown() on our server, passing in the context we just made.
// Shutdown() will return nil if the graceful shutdown was successful, or an
// error (which may happen because of a problem closing the listeners, or
// because the shutdown didn't complete before the 5-second context deadline is
// hit). We relay this return value to the shutdownError channel.
shutdownError <- srv.Shutdown(ctx)
// Call Shutdown() on the server like before, but now we only send on the
// shutdownError channel if it returns an error.
err := srv.Shutdown(ctx)
if err != nil {
shutdownError <- err
}
// Log a message to say that we're waiting for any background goroutines to
// complete their tasks.
app.logger.PrintInfo("completing background tasks", map[string]string{
"addr": srv.Addr,
})
// Call Wait() to block until our WaitGroup counter is zero --- essentially
// blocking until the background goroutines have finished. Then we return nil on
// the shutdownError channel, to indicate that the shutdown completed without
// any issues.
app.wg.Wait()
shutdownError <- nil
}()
app.logger.PrintInfo("starting server", map[string]string{
@@ -52,25 +60,16 @@ func (app *application) serve() error {
"env": app.config.env,
})
// Calling Shutdown() on our server will cause ListenAndServe() to immediately
// return a http.ErrServerClosed error. So if we see this error, it is actually a
// good thing and an indication that the graceful shutdown has started. So we check
// specifically for this, only returning the error if it is NOT http.ErrServerClosed.
err := srv.ListenAndServe()
if !errors.Is(err, http.ErrServerClosed) {
return err
}
// Otherwise, we wait to receive the return value from Shutdown() on the
// shutdownError channel. If return value is an error, we know that there was a
// problem with the graceful shutdown and we return the error.
err = <-shutdownError
if err != nil {
return err
}
// At this point we know that the graceful shutdown completed successfully and we
// log a "stopped server" message.
app.logger.PrintInfo("stopped server", map[string]string{
"addr": srv.Addr,
})