From b56dc2655866a45919d30dea1af15fd8ed8672d2 Mon Sep 17 00:00:00 2001 From: Jamie Holdstock Date: Wed, 9 Jun 2021 10:59:25 +0800 Subject: [PATCH] Add shutdownWg to dcrd notif handler. This prevents db/dcrd/dcrwallet connections from being closed while the dcrd notification handler is running. --- README.md | 2 +- background/background.go | 7 ++++++- vspd.go | 3 ++- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 192e97b..8c7435f 100644 --- a/README.md +++ b/README.md @@ -42,7 +42,7 @@ VSP will add the ticket to a pool of always-online voting wallets. ## Implementation -vspd is built and tested on go 1.14 and 1.15, making use of the following +vspd is built and tested on go 1.15 and 1.16, making use of the following libraries: - [gin-gonic/gin](https://github.com/gin-gonic/gin) webserver. diff --git a/background/background.go b/background/background.go index 4f04a9b..641d125 100644 --- a/background/background.go +++ b/background/background.go @@ -26,7 +26,9 @@ var ( notifierClosed chan struct{} ) -type NotificationHandler struct{} +type NotificationHandler struct { + ShutdownWg *sync.WaitGroup +} const ( // consistencyInterval is the time period between wallet consistency checks. @@ -41,6 +43,9 @@ const ( // because that will cause the client to close and no further notifications will // be received until a new connection is established. func (n *NotificationHandler) Notify(method string, params json.RawMessage) error { + n.ShutdownWg.Add(1) + defer n.ShutdownWg.Done() + if method != "blockconnected" { return nil } diff --git a/vspd.go b/vspd.go index 74737ad..d36e44c 100644 --- a/vspd.go +++ b/vspd.go @@ -107,8 +107,9 @@ func run(ctx context.Context) error { } // Create a dcrd client with a blockconnected notification handler. + notifHandler := background.NotificationHandler{ShutdownWg: &shutdownWg} dcrdWithNotifs := rpc.SetupDcrd(cfg.DcrdUser, cfg.DcrdPass, - cfg.DcrdHost, cfg.dcrdCert, &background.NotificationHandler{}) + cfg.DcrdHost, cfg.dcrdCert, ¬ifHandler) defer dcrdWithNotifs.Close() // Start background process which will continually attempt to reconnect to