Skip to content

Commit

Permalink
Remove logger from config
Browse files Browse the repository at this point in the history
  • Loading branch information
samsondav committed Dec 1, 2023
1 parent 6601b59 commit 77bf317
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 31 deletions.
1 change: 0 additions & 1 deletion core/cmd/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,6 @@ func (n ChainlinkAppFactory) NewApplication(ctx context.Context, cfg chainlink.G
loopRegistry := plugins.NewLoopRegistry(appLggr, cfg.Tracing())

mercuryPool := wsrpc.NewPool(appLggr, cache.Config{
Logger: appLggr,
LatestReportTTL: cfg.Mercury().Cache().LatestReportTTL(),
MaxStaleAge: cfg.Mercury().Cache().MaxStaleAge(),
LatestReportDeadline: cfg.Mercury().Cache().LatestReportDeadline(),
Expand Down
1 change: 0 additions & 1 deletion core/internal/cltest/cltest.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,6 @@ func NewApplicationWithConfig(t testing.TB, cfg chainlink.GeneralConfig, flagsAn
loopRegistry := plugins.NewLoopRegistry(lggr, nil)

mercuryPool := wsrpc.NewPool(lggr, cache.Config{
Logger: lggr,
LatestReportTTL: cfg.Mercury().Cache().LatestReportTTL(),
MaxStaleAge: cfg.Mercury().Cache().MaxStaleAge(),
LatestReportDeadline: cfg.Mercury().Cache().LatestReportDeadline(),
Expand Down
10 changes: 4 additions & 6 deletions core/services/relay/evm/mercury/wsrpc/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ type Cache interface {
}

type Config struct {
Logger logger.Logger
// LatestReportTTL controls how "stale" we will allow a price to be e.g. if
// set to 1s, a new price will always be fetched if the last result was
// from more than 1 second ago.
Expand All @@ -84,8 +83,8 @@ type Config struct {
LatestReportDeadline time.Duration
}

func NewCache(client Client, cfg Config) Cache {
return newMemCache(client, cfg)
func NewCache(lggr logger.Logger, client Client, cfg Config) Cache {
return newMemCache(lggr, client, cfg)
}

type cacheVal struct {
Expand Down Expand Up @@ -172,11 +171,10 @@ type memCache struct {
chStop services.StopChan
}

func newMemCache(client Client, cfg Config) *memCache {
cfg.Logger = cfg.Logger.Named("MemCache")
func newMemCache(lggr logger.Logger, client Client, cfg Config) *memCache {
return &memCache{
services.StateMachine{},
cfg.Logger,
lggr.Named("MemCache"),
client,
cfg,
sync.Map{},
Expand Down
13 changes: 5 additions & 8 deletions core/services/relay/evm/mercury/wsrpc/cache/cache_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,15 @@ type cacheSet struct {
cfg Config
}

func NewCacheSet(cfg Config) CacheSet {
return newCacheSet(cfg)
func NewCacheSet(lggr logger.Logger, cfg Config) CacheSet {
return newCacheSet(lggr, cfg)
}

func newCacheSet(cfg Config) *cacheSet {
cfg.Logger = cfg.Logger.Named("CacheSet")
func newCacheSet(lggr logger.Logger, cfg Config) *cacheSet {
return &cacheSet{
sync.RWMutex{},
services.StateMachine{},
cfg.Logger,
lggr.Named("CacheSet"),
make(map[string]Cache),
cfg,
}
Expand Down Expand Up @@ -92,9 +91,7 @@ func (cs *cacheSet) get(ctx context.Context, client Client) (Fetcher, error) {
if exists {
return c, nil
}
cfg := cs.cfg
cfg.Logger = cfg.Logger.With("serverURL", sURL)
c = newMemCache(client, cfg)
c = newMemCache(cs.lggr, client, cs.cfg)
if err := c.Start(ctx); err != nil {
return nil, err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

func Test_CacheSet(t *testing.T) {
lggr := logger.TestLogger(t)
cs := newCacheSet(Config{Logger: lggr})
cs := newCacheSet(lggr, Config{})
ctx := testutils.Context(t)
require.NoError(t, cs.Start(ctx))
t.Cleanup(func() {
Expand Down
22 changes: 12 additions & 10 deletions core/services/relay/evm/mercury/wsrpc/cache/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,33 @@ import (

"github.com/smartcontractkit/chainlink/v2/core/internal/testutils"
"github.com/smartcontractkit/chainlink/v2/core/logger"
mercuryutils "github.com/smartcontractkit/chainlink/v2/core/services/relay/evm/mercury/utils"
"github.com/smartcontractkit/chainlink/v2/core/services/relay/evm/mercury/wsrpc/pb"
)

const neverExpireTTL = 1000 * time.Hour // some massive value that will never expire during a test

func Test_Cache(t *testing.T) {
lggr := logger.TestLogger(t)
client := &mockClient{}
cfg := Config{
Logger: logger.TestLogger(t),
}
cfg := Config{}
ctx := testutils.Context(t)

req1 := &pb.LatestReportRequest{FeedId: []byte{1}}
req2 := &pb.LatestReportRequest{FeedId: []byte{2}}
req3 := &pb.LatestReportRequest{FeedId: []byte{3}}

feedID1Hex := mercuryutils.BytesToFeedID(req1.FeedId).String()

t.Run("errors with nil req", func(t *testing.T) {
c := newMemCache(client, cfg)
c := newMemCache(lggr, client, cfg)

_, err := c.LatestReport(ctx, nil)
assert.EqualError(t, err, "req must not be nil")
})

t.Run("with LatestReportTTL=0 does no caching", func(t *testing.T) {
c := newMemCache(client, cfg)
c := newMemCache(lggr, client, cfg)

req := &pb.LatestReportRequest{}
for i := 0; i < 5; i++ {
Expand All @@ -58,7 +60,7 @@ func Test_Cache(t *testing.T) {
t.Run("caches repeated calls to LatestReport, keyed by request", func(t *testing.T) {
cfg.LatestReportTTL = neverExpireTTL
client.err = nil
c := newMemCache(client, cfg)
c := newMemCache(lggr, client, cfg)

t.Run("if cache is unstarted, returns error", func(t *testing.T) {
// starting the cache is required for state management if we
Expand Down Expand Up @@ -122,8 +124,8 @@ func Test_Cache(t *testing.T) {
})

t.Run("re-queries when a cache item has expired", func(t *testing.T) {
vi, exists := c.cache.Load(req1)
assert.True(t, exists)
vi, exists := c.cache.Load(feedID1Hex)
require.True(t, exists)
v := vi.(*cacheVal)
v.expiresAt = time.Now().Add(-1 * time.Second)

Expand Down Expand Up @@ -167,7 +169,7 @@ func Test_Cache(t *testing.T) {
})

t.Run("timeouts", func(t *testing.T) {
c := newMemCache(client, cfg)
c := newMemCache(lggr, client, cfg)
// simulate fetch already executing in background
v := &cacheVal{
fetching: true,
Expand All @@ -176,7 +178,7 @@ func Test_Cache(t *testing.T) {
err: nil,
expiresAt: time.Now().Add(-1 * time.Second),
}
c.cache.Store(req1, v)
c.cache.Store(feedID1Hex, v)

canceledCtx, cancel := context.WithCancel(testutils.Context(t))
cancel()
Expand Down
4 changes: 2 additions & 2 deletions core/services/relay/evm/mercury/wsrpc/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ func Test_Client_LatestReport(t *testing.T) {

t.Run("with cache disabled", func(t *testing.T) {
req := &pb.LatestReportRequest{}
cacheSet := cache.NewCacheSet(cache.Config{LatestReportTTL: 0, Logger: lggr})
cacheSet := cache.NewCacheSet(lggr, cache.Config{LatestReportTTL: 0})
resp := &pb.LatestReportResponse{}

var calls int
Expand Down Expand Up @@ -178,7 +178,7 @@ func Test_Client_LatestReport(t *testing.T) {
t.Run("with caching", func(t *testing.T) {
req := &pb.LatestReportRequest{}
const neverExpireTTL = 1000 * time.Hour // some massive value that will never expire during a test
cacheSet := cache.NewCacheSet(cache.Config{LatestReportTTL: neverExpireTTL, Logger: lggr})
cacheSet := cache.NewCacheSet(lggr, cache.Config{LatestReportTTL: neverExpireTTL})
resp := &pb.LatestReportResponse{}

var calls int
Expand Down
5 changes: 3 additions & 2 deletions core/services/relay/evm/mercury/wsrpc/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,10 @@ type pool struct {
}

func NewPool(lggr logger.Logger, cacheCfg cache.Config) Pool {
p := newPool(lggr.Named("Mercury.WSRPCPool"))
lggr = lggr.Named("Mercury.WSRPCPool")
p := newPool(lggr)
p.newClient = NewClient
p.cacheSet = cache.NewCacheSet(cacheCfg)
p.cacheSet = cache.NewCacheSet(lggr, cacheCfg)
return p
}

Expand Down

0 comments on commit 77bf317

Please sign in to comment.