Add cache to governor endpoints (#1271)

This commit is contained in:
ftocal 2024-04-03 14:58:04 -03:00 committed by GitHub
parent 3d60517757
commit aabad70e4d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 25 additions and 5 deletions

View File

@ -4,10 +4,14 @@ package governor
import (
"context"
"fmt"
"time"
"github.com/wormhole-foundation/wormhole-explorer/api/cacheable"
errs "github.com/wormhole-foundation/wormhole-explorer/api/internal/errors"
"github.com/wormhole-foundation/wormhole-explorer/api/internal/metrics"
"github.com/wormhole-foundation/wormhole-explorer/api/internal/pagination"
"github.com/wormhole-foundation/wormhole-explorer/api/response"
"github.com/wormhole-foundation/wormhole-explorer/common/client/cache"
"github.com/wormhole-foundation/wormhole-explorer/common/domain"
"github.com/wormhole-foundation/wormhole-explorer/common/types"
"github.com/wormhole-foundation/wormhole/sdk/vaa"
@ -16,14 +20,21 @@ import (
type Service struct {
repo *Repository
cache cache.Cache
metrics metrics.Metrics
supportedChainIDs map[vaa.ChainID]string
logger *zap.Logger
}
const (
availableNotionByChain = "wormscan:available-notion-by-chain"
tokenList = "wormscan:token-list"
)
// NewService create a new governor.Service.
func NewService(dao *Repository, logger *zap.Logger) *Service {
func NewService(dao *Repository, cache cache.Cache, metrics metrics.Metrics, logger *zap.Logger) *Service {
supportedChainIDs := domain.GetSupportedChainIDs()
return &Service{repo: dao, supportedChainIDs: supportedChainIDs, logger: logger.With(zap.String("module", "GovernorService"))}
return &Service{repo: dao, cache: cache, metrics: metrics, supportedChainIDs: supportedChainIDs, logger: logger.With(zap.String("module", "GovernorService"))}
}
// FindGovernorConfig get a list of governor configurations.
@ -173,13 +184,22 @@ func (s *Service) GetGovernorLimit(ctx context.Context, p *pagination.Pagination
// GetAvailNotionByChain get governor limit for each chainID.
// Guardian api migration.
func (s *Service) GetAvailNotionByChain(ctx context.Context) ([]*AvailableNotionalByChain, error) {
return s.repo.GetAvailNotionByChain(ctx)
key := availableNotionByChain
return cacheable.GetOrLoad(ctx, s.logger, s.cache, 1*time.Minute, key, s.metrics,
func() ([]*AvailableNotionalByChain, error) {
return s.repo.GetAvailNotionByChain(ctx)
})
}
// Get governor token list.
// Guardian api migration.
func (s *Service) GetTokenList(ctx context.Context) ([]*TokenList, error) {
return s.repo.GetTokenList(ctx)
key := tokenList
return cacheable.GetOrLoad(ctx, s.logger, s.cache, 1*time.Minute, key, s.metrics,
func() ([]*TokenList, error) {
return s.repo.GetTokenList(ctx)
})
}
// GetEnqueuedVaas get enqueued vaas.

View File

@ -173,7 +173,7 @@ func main() {
addressService := address.NewService(addressRepo, rootLogger)
vaaService := vaa.NewService(vaaRepo, cache.Get, vaaParserFunc, rootLogger)
obsService := observations.NewService(obsRepo, rootLogger)
governorService := governor.NewService(governorRepo, rootLogger)
governorService := governor.NewService(governorRepo, cache, metrics, rootLogger)
infrastructureService := infrastructure.NewService(infrastructureRepo, rootLogger)
heartbeatsService := heartbeats.NewService(heartbeatsRepo, rootLogger)
transactionsService := transactions.NewService(transactionsRepo, cache, expirationTime, tokenProvider, metrics, rootLogger)