Skip to content

Commit

Permalink
feat(BUX-641): cache TTL for shared & public config
Browse files Browse the repository at this point in the history
  • Loading branch information
chris-4chain committed Mar 12, 2024
1 parent 9cdcd3b commit 28689c0
Showing 1 changed file with 22 additions and 18 deletions.
40 changes: 22 additions & 18 deletions domain/config/config_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package config
import (
"slices"
"sync"
"time"

backendconfig "github.com/bitcoin-sv/spv-wallet-web-backend/config"
"github.com/bitcoin-sv/spv-wallet-web-backend/domain/users"
Expand All @@ -11,6 +12,8 @@ import (
"github.com/spf13/viper"
)

const cacheTTL = 10 * time.Minute

// ConfigService is a service for fetching and caching SharedConfig from the spv-wallet and providing PublicConfig.
type ConfigService struct {
adminWalletClient users.AdminWalletClient
Expand All @@ -19,6 +22,7 @@ type ConfigService struct {
sharedConfig *models.SharedConfig
publicConfig *PublicConfig
mutex sync.Mutex
lastFetch time.Time
}

// NewConfigService creates a new ConfigService.
Expand All @@ -35,33 +39,33 @@ func NewConfigService(adminWalletClient users.AdminWalletClient, log *zerolog.Lo
// If shared config is not cached, it will be fetched from the spv-wallet.
// SharedConfig should not be exposed to the public - use PublicConfig instead.
func (s *ConfigService) GetSharedConfig() *models.SharedConfig {
if s.sharedConfig != nil {
return s.sharedConfig
}

s.mutex.Lock()
defer s.mutex.Unlock()
model, err := s.adminWalletClient.GetSharedConfig()
if err != nil {
s.log.Error().Err(err).Msg("Failed to get shared config")
return nil
}
s.sharedConfig = model
s.makeConfigs()
return s.sharedConfig
}

// GetPublicConfig returns public config.
func (s *ConfigService) GetPublicConfig() *PublicConfig {
if s.publicConfig != nil {
return s.publicConfig
s.makeConfigs()
return s.publicConfig
}

func (s *ConfigService) makeConfigs() {
s.mutex.Lock()
defer s.mutex.Unlock()

if s.sharedConfig != nil && time.Since(s.lastFetch) < cacheTTL {
return
}
shared := s.GetSharedConfig()
if shared == nil {
return nil
s.lastFetch = time.Now()

shared, err := s.adminWalletClient.GetSharedConfig()
if err != nil {
s.log.Error().Err(err).Msg("Failed to get shared config")
return
}

s.sharedConfig = shared
s.publicConfig = s.makePublicConfig(shared)
return s.publicConfig
}

func (s *ConfigService) makePublicConfig(shared *models.SharedConfig) *PublicConfig {
Expand Down

0 comments on commit 28689c0

Please sign in to comment.