From bd41dbee63a3d2ed8501599eb01f5852fca38203 Mon Sep 17 00:00:00 2001 From: jholdstock Date: Sat, 19 Aug 2023 09:01:18 +0100 Subject: [PATCH] vspd: Listen for dcrd notifs after startup. Only attach the dcrd notification listener after all other startup tasks have been completed. Attaching the listener before this can result in the notification handler being invoked whilst startup tasks are still running, which isn't necessarily a problem, but it is unnecessary and makes a mess of logging. --- cmd/vspd/main.go | 76 ++++++++++++++++++++++++------------------------ 1 file changed, 38 insertions(+), 38 deletions(-) diff --git a/cmd/vspd/main.go b/cmd/vspd/main.go index 4831b60..cd78acf 100644 --- a/cmd/vspd/main.go +++ b/cmd/vspd/main.go @@ -100,44 +100,6 @@ func run() int { wallets := rpc.SetupWallet(cfg.walletUsers, cfg.walletPasswords, cfg.walletHosts, cfg.walletCerts, cfg.netParams.Params, rpcLog) defer wallets.Close() - // Create a channel to receive blockConnected notifications from dcrd. - notifChan := make(chan *wire.BlockHeader) - shutdownWg.Add(1) - go func() { - for { - select { - case <-shutdownCtx.Done(): - shutdownWg.Done() - return - case header := <-notifChan: - log.Debugf("Block notification %d (%s)", header.Height, header.BlockHash().String()) - blockConnected(dcrd, wallets, db, log) - } - } - }() - - // Attach notification listener to dcrd client. - dcrd.BlockConnectedHandler(notifChan) - - // Loop forever attempting ensuring a dcrd connection is available, so - // notifications are received. - shutdownWg.Add(1) - go func() { - for { - select { - case <-shutdownCtx.Done(): - shutdownWg.Done() - return - case <-time.After(time.Second * 15): - // Ensure dcrd client is still connected. - _, _, err := dcrd.Client() - if err != nil { - log.Errorf("dcrd connect error: %v", err) - } - } - } - }() - // Ensure all data in database is present and up-to-date. err = db.CheckIntegrity(dcrd) if err != nil { @@ -190,6 +152,44 @@ func run() int { return 1 } + // Create a channel to receive blockConnected notifications from dcrd. + notifChan := make(chan *wire.BlockHeader) + shutdownWg.Add(1) + go func() { + for { + select { + case <-shutdownCtx.Done(): + shutdownWg.Done() + return + case header := <-notifChan: + log.Debugf("Block notification %d (%s)", header.Height, header.BlockHash().String()) + blockConnected(dcrd, wallets, db, log) + } + } + }() + + // Attach notification listener to dcrd client. + dcrd.BlockConnectedHandler(notifChan) + + // Loop forever attempting ensuring a dcrd connection is available, so + // notifications are received. + shutdownWg.Add(1) + go func() { + for { + select { + case <-shutdownCtx.Done(): + shutdownWg.Done() + return + case <-time.After(time.Second * 15): + // Ensure dcrd client is still connected. + _, _, err := dcrd.Client() + if err != nil { + log.Errorf("dcrd connect error: %v", err) + } + } + } + }() + // Wait for shutdown tasks to complete before running deferred tasks and // returning. shutdownWg.Wait()