Use background context for background processes

This commit is contained in:
jholdstock 2020-09-15 15:53:27 +01:00 committed by David Hill
parent 5c41761f1e
commit f06d4cd8ba

View File

@ -19,13 +19,11 @@ import (
)
var (
ctx context.Context
db *database.VspDatabase
dcrdRPC rpc.DcrdConnect
walletRPC rpc.WalletConnect
netParams *chaincfg.Params
notifierClosed chan struct{}
shutdownWg *sync.WaitGroup
)
type NotificationHandler struct{}
@ -66,8 +64,7 @@ func blockConnected() {
const funcName = "blockConnected"
shutdownWg.Add(1)
defer shutdownWg.Done()
ctx := context.Background()
dcrdClient, err := dcrdRPC.Client(ctx, netParams)
if err != nil {
@ -279,10 +276,10 @@ func (n *NotificationHandler) Close() error {
return nil
}
func connectNotifier(dcrdWithNotifs rpc.DcrdConnect) error {
func connectNotifier(shutdownCtx context.Context, dcrdWithNotifs rpc.DcrdConnect) error {
notifierClosed = make(chan struct{})
dcrdClient, err := dcrdWithNotifs.Client(ctx, netParams)
dcrdClient, err := dcrdWithNotifs.Client(context.Background(), netParams)
if err != nil {
return err
}
@ -297,7 +294,7 @@ func connectNotifier(dcrdWithNotifs rpc.DcrdConnect) error {
// Wait until context is done (vspd is shutting down), or until the
// notifier is closed.
select {
case <-ctx.Done():
case <-shutdownCtx.Done():
dcrdWithNotifs.Close()
return nil
case <-notifierClosed:
@ -306,15 +303,13 @@ func connectNotifier(dcrdWithNotifs rpc.DcrdConnect) error {
}
}
func Start(c context.Context, wg *sync.WaitGroup, vdb *database.VspDatabase, drpc rpc.DcrdConnect,
func Start(shutdownCtx context.Context, wg *sync.WaitGroup, vdb *database.VspDatabase, drpc rpc.DcrdConnect,
dcrdWithNotif rpc.DcrdConnect, wrpc rpc.WalletConnect, p *chaincfg.Params) {
ctx = c
db = vdb
dcrdRPC = drpc
walletRPC = wrpc
netParams = p
shutdownWg = wg
// Run the block connected handler now to catch up with any blocks mined
// while vspd was shut down.
@ -325,24 +320,30 @@ func Start(c context.Context, wg *sync.WaitGroup, vdb *database.VspDatabase, drp
checkWalletConsistency()
// Run voting wallet consistency check periodically.
wg.Add(1)
go func() {
ticker := time.NewTicker(consistencyInterval)
consistencyLoop:
for {
select {
case <-ctx.Done():
case <-shutdownCtx.Done():
ticker.Stop()
return
break consistencyLoop
case <-ticker.C:
checkWalletConsistency()
}
}
log.Debugf("Consistency checker stopped")
wg.Done()
}()
// Loop forever attempting to create a connection to the dcrd server for
// notifications.
wg.Add(1)
go func() {
notifierLoop:
for {
err := connectNotifier(dcrdWithNotif)
err := connectNotifier(shutdownCtx, dcrdWithNotif)
if err != nil {
log.Errorf("dcrd connect error: %v", err)
}
@ -350,12 +351,14 @@ func Start(c context.Context, wg *sync.WaitGroup, vdb *database.VspDatabase, drp
// If context is done (vspd is shutting down), return,
// otherwise wait 15 seconds and try to reconnect.
select {
case <-ctx.Done():
return
case <-shutdownCtx.Done():
break notifierLoop
case <-time.After(15 * time.Second):
}
}
log.Debugf("Notification connector stopped")
wg.Done()
}()
}
@ -368,6 +371,8 @@ func checkWalletConsistency() {
log.Info("Checking voting wallet consistency")
ctx := context.Background()
dcrdClient, err := dcrdRPC.Client(ctx, netParams)
if err != nil {
log.Errorf("%s: %v", funcName, err)