From 280dc391be70c35f0545b1ba3fbe16f553bf2696 Mon Sep 17 00:00:00 2001 From: jholdstock Date: Thu, 14 Sep 2023 17:47:56 +0100 Subject: [PATCH] webapi: Keep DB and RPC clients inside cache. Storing references to the database/dcrd/dcrwallet clients inside the cache struct means they don't need to be passed in every time the cache is updated. --- internal/webapi/cache.go | 26 ++++++++++++++++---------- internal/webapi/webapi.go | 7 ++++--- 2 files changed, 20 insertions(+), 13 deletions(-) diff --git a/internal/webapi/cache.go b/internal/webapi/cache.go index a2e3622..19dcd79 100644 --- a/internal/webapi/cache.go +++ b/internal/webapi/cache.go @@ -22,7 +22,11 @@ type cache struct { data cacheData // mtx must be held to read/write cache data. mtx sync.RWMutex - log slog.Logger + + log slog.Logger + db *database.VspDatabase + dcrd rpc.DcrdConnect + wallets rpc.WalletConnect } type cacheData struct { @@ -49,33 +53,35 @@ func (c *cache) getData() cacheData { } // newCache creates a new cache and initializes it with static values. -func newCache(signPubKey string, log slog.Logger) *cache { +func newCache(signPubKey string, log slog.Logger, db *database.VspDatabase, + dcrd rpc.DcrdConnect, wallets rpc.WalletConnect) *cache { return &cache{ data: cacheData{ PubKey: signPubKey, }, - log: log, + log: log, + db: db, + dcrd: dcrd, + wallets: wallets, } } // update will use the provided database and RPC connections to update the // dynamic values in the cache. -func (c *cache) update(db *database.VspDatabase, dcrd rpc.DcrdConnect, - wallets rpc.WalletConnect) error { - - dbSize, err := db.Size() +func (c *cache) update() error { + dbSize, err := c.db.Size() if err != nil { return err } // Get latest counts of voting, voted, expired and missed tickets. - voting, voted, expired, missed, err := db.CountTickets() + voting, voted, expired, missed, err := c.db.CountTickets() if err != nil { return err } // Get latest best block height. - dcrdClient, _, err := dcrd.Client() + dcrdClient, _, err := c.dcrd.Client() if err != nil { return err } @@ -89,7 +95,7 @@ func (c *cache) update(db *database.VspDatabase, dcrd rpc.DcrdConnect, return errors.New("dcr node reports a network ticket pool size of zero") } - clients, failedConnections := wallets.Clients() + clients, failedConnections := c.wallets.Clients() if len(clients) == 0 { c.log.Error("Could not connect to any wallets") } else if len(failedConnections) > 0 { diff --git a/internal/webapi/webapi.go b/internal/webapi/webapi.go index 73bcdfa..d80dbf5 100644 --- a/internal/webapi/webapi.go +++ b/internal/webapi/webapi.go @@ -94,8 +94,9 @@ func Start(ctx context.Context, requestShutdown func(), shutdownWg *sync.WaitGro } // Populate cached VSP stats before starting webserver. - s.cache = newCache(base64.StdEncoding.EncodeToString(s.signPubKey), log) - err = s.cache.update(vdb, dcrd, wallets) + encodedPubKey := base64.StdEncoding.EncodeToString(s.signPubKey) + s.cache = newCache(encodedPubKey, log, vdb, dcrd, wallets) + err = s.cache.update() if err != nil { log.Errorf("Could not initialize VSP stats cache: %v", err) } @@ -180,7 +181,7 @@ func Start(ctx context.Context, requestShutdown func(), shutdownWg *sync.WaitGro shutdownWg.Done() return case <-time.After(refresh): - err := s.cache.update(vdb, dcrd, wallets) + err := s.cache.update() if err != nil { log.Errorf("Failed to update cached VSP stats: %v", err) }