From f881179024a0b573fce91e1df3f9ef7189068268 Mon Sep 17 00:00:00 2001 From: jholdstock Date: Fri, 15 Sep 2023 12:04:08 +0100 Subject: [PATCH] webapi: Make Run func blocking. This updates the Run func to make it blocking. When the provided context is canceled the server is cleanly shut down and the func returns. --- cmd/vspd/signal.go | 6 ------ cmd/vspd/vspd.go | 7 ++++++- internal/webapi/webapi.go | 31 ++++++++++++++++--------------- 3 files changed, 22 insertions(+), 22 deletions(-) diff --git a/cmd/vspd/signal.go b/cmd/vspd/signal.go index af67708..0809929 100644 --- a/cmd/vspd/signal.go +++ b/cmd/vspd/signal.go @@ -53,9 +53,3 @@ func shutdownListener(log slog.Logger) context.Context { }() return ctx } - -// requestShutdown signals for starting the clean shutdown of the process -// through an internal component. -func requestShutdown() { - shutdownRequestChannel <- struct{}{} -} diff --git a/cmd/vspd/vspd.go b/cmd/vspd/vspd.go index 8872998..f7f1bfd 100644 --- a/cmd/vspd/vspd.go +++ b/cmd/vspd/vspd.go @@ -159,7 +159,12 @@ func (v *vspd) run() int { // WaitGroup for services to signal when they have shutdown cleanly. var shutdownWg sync.WaitGroup - api.Run(ctx, requestShutdown, &shutdownWg) + // Start the webapi server. + shutdownWg.Add(1) + go func() { + api.Run(ctx) + shutdownWg.Done() + }() // Start all background tasks and notification handlers. shutdownWg.Add(1) diff --git a/internal/webapi/webapi.go b/internal/webapi/webapi.go index 203b7b4..941b091 100644 --- a/internal/webapi/webapi.go +++ b/internal/webapi/webapi.go @@ -142,10 +142,11 @@ func New(vdb *database.VspDatabase, log slog.Logger, dcrd rpc.DcrdConnect, return w, nil } -func (w *WebAPI) Run(ctx context.Context, requestShutdown func(), shutdownWg *sync.WaitGroup) { +func (w *WebAPI) Run(ctx context.Context) { + var wg sync.WaitGroup // Add the graceful shutdown to the waitgroup. - shutdownWg.Add(1) + wg.Add(1) go func() { // Wait until shutdown is signaled before shutting down. <-ctx.Done() @@ -159,34 +160,32 @@ func (w *WebAPI) Run(ctx context.Context, requestShutdown func(), shutdownWg *sy } else { w.log.Debug("Webserver stopped") } - shutdownWg.Done() + wg.Done() }() // Start webserver. + wg.Add(1) go func() { err := w.server.Serve(w.listener) - // If the server dies for any reason other than ErrServerClosed (from - // graceful server.Shutdown), log the error and request vspd be - // shutdown. + // ErrServerClosed is expected from a graceful server shutdown, it can + // be ignored. Anything else should be logged. if err != nil && !errors.Is(err, http.ErrServerClosed) { w.log.Errorf("Unexpected webserver error: %v", err) - requestShutdown() } + wg.Done() }() // Periodically update cached VSP stats. - var refresh time.Duration - if w.cfg.Debug { - refresh = 1 * time.Second - } else { - refresh = 1 * time.Minute - } - shutdownWg.Add(1) + wg.Add(1) go func() { + refresh := 1 * time.Minute + if w.cfg.Debug { + refresh = 1 * time.Second + } for { select { case <-ctx.Done(): - shutdownWg.Done() + wg.Done() return case <-time.After(refresh): err := w.cache.update() @@ -196,6 +195,8 @@ func (w *WebAPI) Run(ctx context.Context, requestShutdown func(), shutdownWg *sy } } }() + + wg.Wait() } func (w *WebAPI) router(cookieSecret []byte, dcrd rpc.DcrdConnect, wallets rpc.WalletConnect) *gin.Engine {