diff options
author | Gabriel A. Giovanini <mail@gabrielgio.me> | 2024-05-04 23:28:05 +0200 |
---|---|---|
committer | Gabriel A. Giovanini <mail@gabrielgio.me> | 2024-05-04 23:56:53 +0200 |
commit | fadc75c731368d39edc718246b6dedff40e097e3 (patch) | |
tree | 103d9673d0c33c1fc18dfa0ee55e4ed618e3ce64 /pkg/worker/http.go | |
parent | 8a2461aa05895cc7828bc9619b50fa5dee5ed1f4 (diff) | |
download | cerrado-fadc75c731368d39edc718246b6dedff40e097e3.tar.gz cerrado-fadc75c731368d39edc718246b6dedff40e097e3.tar.bz2 cerrado-fadc75c731368d39edc718246b6dedff40e097e3.zip |
feat: Close worker on error
If a worker error the application, as a whole, is terminadated.
Diffstat (limited to 'pkg/worker/http.go')
-rw-r--r-- | pkg/worker/http.go | 20 |
1 files changed, 16 insertions, 4 deletions
diff --git a/pkg/worker/http.go b/pkg/worker/http.go index 1d56f86..973775e 100644 --- a/pkg/worker/http.go +++ b/pkg/worker/http.go @@ -16,11 +16,23 @@ func NewServerTask(server *http.Server) *ServerTask { } func (self *ServerTask) Start(ctx context.Context) error { + done := make(chan error) + go func() { - // nolint: errcheck - self.server.ListenAndServe() + done <- self.server.ListenAndServe() }() - <-ctx.Done() - return self.server.Shutdown(ctx) + select { + // if ListenAndServe error for something other than context.Canceled + //(e.g.: address already in use) it trigger done to return sonner with + // the return error + case err := <-done: + return err + + // in case of context canceled it will manually trigger the server to + // shutdown, and return its error, which is most cases, but not limited, is + // context.Canceled. + case <-ctx.Done(): + return self.server.Shutdown(ctx) + } } |