Skip to content

Commit

Permalink
Merge pull request #165 from ethpandaops/feat/ttlcache
Browse files Browse the repository at this point in the history
feat(ttlcache): move back to upstream package
  • Loading branch information
Savid authored Aug 31, 2023
2 parents 42f122f + a2f1d9e commit 32bd250
Show file tree
Hide file tree
Showing 20 changed files with 35 additions and 33 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ require (
github.com/google/uuid v1.3.0
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0
github.com/huandu/go-sqlbuilder v1.21.0
github.com/jellydator/ttlcache/v3 v3.1.0
github.com/lib/pq v1.10.9
github.com/libp2p/go-libp2p v0.27.3
github.com/mitchellh/hashstructure/v2 v2.0.2
github.com/oschwald/maxminddb-golang v1.10.0
github.com/pkg/errors v0.9.1
github.com/prometheus/client_golang v1.16.0
github.com/redis/go-redis/v9 v9.0.4
github.com/savid/ttlcache/v3 v3.0.2
github.com/sirupsen/logrus v1.9.3
github.com/spf13/cobra v1.7.0
github.com/stretchr/testify v1.8.2
Expand Down
6 changes: 3 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,8 @@ github.com/ipfs/go-cid v0.4.1 h1:A/T3qGvxi4kpKWWcPC/PgbvDA2bjVLO7n4UeVwnbs/s=
github.com/ipfs/go-cid v0.4.1/go.mod h1:uQHwDeX4c6CtyrFwdqyhpNcxVewur1M7l7fNU7LKwZk=
github.com/jackpal/go-nat-pmp v1.0.2 h1:KzKSgb7qkJvOUTqYl9/Hg/me3pWgBmERKrTGD7BdWus=
github.com/jackpal/go-nat-pmp v1.0.2/go.mod h1:QPH045xvCAeXUZOxsnwmrtiCoxIr9eob+4orBN1SBKc=
github.com/jellydator/ttlcache/v3 v3.1.0 h1:0gPFG0IHHP6xyUyXq+JaD8fwkDCqgqwohXNJBcYE71g=
github.com/jellydator/ttlcache/v3 v3.1.0/go.mod h1:hi7MGFdMAwZna5n2tuvh63DvFLzVKySzCVW6+0gA2n4=
github.com/jpillora/backoff v1.0.0/go.mod h1:J/6gKK9jxlEcS3zixgDgUAsiuZ7yrSoa/FX5e0EB2j4=
github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
github.com/json-iterator/go v1.1.10/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
Expand Down Expand Up @@ -403,8 +405,6 @@ github.com/rs/zerolog v1.29.0/go.mod h1:NILgTygv/Uej1ra5XxGf82ZFSLk58MFGAUS2o6us
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/samcm/go-eth2-client v0.15.12 h1:BB8dGvQAQFL7ol4584AQch41RQrlmw+7u2dR+VOFM2k=
github.com/samcm/go-eth2-client v0.15.12/go.mod h1:PLRKnILnr63V3yl2VagBqnhVRFBWc0V+JhQSsXQaSwQ=
github.com/savid/ttlcache/v3 v3.0.2 h1:P7LAHoFNfCjKKJyYcSTFYsNmO+xHaRXTTrLrjb2UJJY=
github.com/savid/ttlcache/v3 v3.0.2/go.mod h1:9OBR4zMFiOT3Vk2aghO8vOhmsdfTFT4orFnQ4cl+VyM=
github.com/shirou/gopsutil v3.21.11+incompatible h1:+1+c1VGhc88SSonWP6foOcLhvnKlUeu/erjjvaPEYiI=
github.com/shirou/gopsutil v3.21.11+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA=
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
Expand Down Expand Up @@ -456,7 +456,7 @@ go.opencensus.io v0.22.4/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw=
go.uber.org/atomic v1.9.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
go.uber.org/atomic v1.10.0 h1:9qC72Qh0+3MqyJbAn8YU5xVq1frD8bn3JtD2oXtafVQ=
go.uber.org/atomic v1.10.0/go.mod h1:LUxbIzbOniOlMKjJjyPfpl4v+PKK2cNJn91OQbhoJI0=
go.uber.org/goleak v1.1.12 h1:gZAh5/EyT/HQwlpkCy6wTpqfH9H8Lz8zbm3dZh+OyzA=
go.uber.org/goleak v1.2.1 h1:NBol2c7O1ZokfZ0LEU9K6Whx/KnwvepVetCUhtKja4A=
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
Expand Down
2 changes: 1 addition & 1 deletion pkg/discovery/cache/duplicate.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"time"

"github.com/go-co-op/gocron"
"github.com/savid/ttlcache/v3"
"github.com/jellydator/ttlcache/v3"
)

type DuplicateCache struct {
Expand Down
4 changes: 2 additions & 2 deletions pkg/discovery/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import (
"github.com/ethpandaops/xatu/pkg/proto/xatu"
"github.com/go-co-op/gocron"
"github.com/google/uuid"
"github.com/jellydator/ttlcache/v3"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/savid/ttlcache/v3"
"github.com/sirupsen/logrus"
)

Expand Down Expand Up @@ -219,7 +219,7 @@ func (d *Discovery) handleNewNodeRecord(ctx context.Context, node *enode.Node, s

enr := node.String()

item, retrieved := d.duplicateCache.Node.GetOrSet(enr, time.Now(), ttlcache.DefaultTTL)
item, retrieved := d.duplicateCache.Node.GetOrSet(enr, time.Now(), ttlcache.WithTTL[string, time.Time](ttlcache.DefaultTTL))
if retrieved {
d.log.WithFields(logrus.Fields{
"enr": enr,
Expand Down
2 changes: 1 addition & 1 deletion pkg/mimicry/coordinator/cache/shared.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"time"

"github.com/go-co-op/gocron"
"github.com/savid/ttlcache/v3"
"github.com/jellydator/ttlcache/v3"
)

type SharedCache struct {
Expand Down
3 changes: 2 additions & 1 deletion pkg/mimicry/p2p/execution/event_transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethpandaops/xatu/pkg/proto/xatu"
"github.com/jellydator/ttlcache/v3"
"github.com/sirupsen/logrus"
"google.golang.org/protobuf/types/known/timestamppb"
"google.golang.org/protobuf/types/known/wrapperspb"
Expand Down Expand Up @@ -133,7 +134,7 @@ func (p *Peer) ExportTransactions(ctx context.Context, items []*TransactionHashI

if txs != nil {
for _, tx := range txs.PooledTransactionsPacket {
_, retrieved := p.sharedCache.Transaction.GetOrSet(tx.Hash().String(), true, 1*time.Hour)
_, retrieved := p.sharedCache.Transaction.GetOrSet(tx.Hash().String(), true, ttlcache.WithTTL[string, bool](1*time.Hour))
// transaction was just set in shared cache, so we need to handle it
if !retrieved {
seen := seenMap[tx.Hash()]
Expand Down
3 changes: 2 additions & 1 deletion pkg/mimicry/p2p/execution/execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"sync"
"time"

"github.com/jellydator/ttlcache/v3"
"github.com/sirupsen/logrus"

"github.com/ethereum/go-ethereum/common"
Expand Down Expand Up @@ -243,7 +244,7 @@ func (p *Peer) Start(ctx context.Context) (<-chan error, error) {
if p.handlers.DecoratedEvent != nil && txs != nil {
now := time.Now()
for _, tx := range *txs {
_, retrieved := p.sharedCache.Transaction.GetOrSet(tx.Hash().String(), true, 1*time.Hour)
_, retrieved := p.sharedCache.Transaction.GetOrSet(tx.Hash().String(), true, ttlcache.WithTTL[string, bool](1*time.Hour))
// transaction was just set in shared cache, so we need to handle it
if !retrieved {
event, errT := p.handleTransaction(ctx, now, tx)
Expand Down
2 changes: 1 addition & 1 deletion pkg/sentry/cache/duplicate.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package cache
import (
"time"

"github.com/savid/ttlcache/v3"
"github.com/jellydator/ttlcache/v3"
)

type DuplicateCache struct {
Expand Down
2 changes: 1 addition & 1 deletion pkg/sentry/ethereum/services/duties.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
backoff "github.com/cenkalti/backoff/v4"
"github.com/ethpandaops/beacon/pkg/beacon"
"github.com/ethpandaops/ethwallclock"
"github.com/savid/ttlcache/v3"
"github.com/jellydator/ttlcache/v3"
"github.com/sirupsen/logrus"
)

Expand Down
4 changes: 2 additions & 2 deletions pkg/sentry/event/beacon/eth/v1/beacon_committee.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/ethpandaops/xatu/pkg/proto/xatu"
"github.com/ethpandaops/xatu/pkg/sentry/ethereum"
"github.com/google/uuid"
ttlcache "github.com/savid/ttlcache/v3"
ttlcache "github.com/jellydator/ttlcache/v3"
"github.com/sirupsen/logrus"
"google.golang.org/protobuf/types/known/timestamppb"
"google.golang.org/protobuf/types/known/wrapperspb"
Expand Down Expand Up @@ -87,7 +87,7 @@ func (e *BeaconCommittee) ShouldIgnore(ctx context.Context) (bool, error) {

key := fmt.Sprintf("%d-%d-%d", e.epoch, e.event.Index, e.event.Slot)

item, retrieved := e.duplicateCache.GetOrSet(key, e.now, ttlcache.DefaultTTL)
item, retrieved := e.duplicateCache.GetOrSet(key, e.now, ttlcache.WithTTL[string, time.Time](ttlcache.DefaultTTL))
if retrieved {
e.log.WithFields(logrus.Fields{
"epoch": e.epoch,
Expand Down
4 changes: 2 additions & 2 deletions pkg/sentry/event/beacon/eth/v1/events_attestation.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (
"github.com/ethpandaops/xatu/pkg/proto/xatu"
"github.com/ethpandaops/xatu/pkg/sentry/ethereum"
"github.com/google/uuid"
ttlcache "github.com/jellydator/ttlcache/v3"
hashstructure "github.com/mitchellh/hashstructure/v2"
ttlcache "github.com/savid/ttlcache/v3"
"github.com/sirupsen/logrus"
"google.golang.org/protobuf/types/known/timestamppb"
"google.golang.org/protobuf/types/known/wrapperspb"
Expand Down Expand Up @@ -94,7 +94,7 @@ func (e *EventsAttestation) ShouldIgnore(ctx context.Context) (bool, error) {
return true, err
}

item, retrieved := e.duplicateCache.GetOrSet(fmt.Sprint(hash), e.now, ttlcache.DefaultTTL)
item, retrieved := e.duplicateCache.GetOrSet(fmt.Sprint(hash), e.now, ttlcache.WithTTL[string, time.Time](ttlcache.DefaultTTL))
if retrieved {
e.log.WithFields(logrus.Fields{
"hash": hash,
Expand Down
4 changes: 2 additions & 2 deletions pkg/sentry/event/beacon/eth/v1/events_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (
"github.com/ethpandaops/xatu/pkg/proto/xatu"
"github.com/ethpandaops/xatu/pkg/sentry/ethereum"
"github.com/google/uuid"
ttlcache "github.com/jellydator/ttlcache/v3"
hashstructure "github.com/mitchellh/hashstructure/v2"
ttlcache "github.com/savid/ttlcache/v3"
"github.com/sirupsen/logrus"
"google.golang.org/protobuf/types/known/timestamppb"
"google.golang.org/protobuf/types/known/wrapperspb"
Expand Down Expand Up @@ -82,7 +82,7 @@ func (e *EventsBlock) ShouldIgnore(ctx context.Context) (bool, error) {
return true, err
}

item, retrieved := e.duplicateCache.GetOrSet(fmt.Sprint(hash), e.now, ttlcache.DefaultTTL)
item, retrieved := e.duplicateCache.GetOrSet(fmt.Sprint(hash), e.now, ttlcache.WithTTL[string, time.Time](ttlcache.DefaultTTL))
if retrieved {
e.log.WithFields(logrus.Fields{
"hash": hash,
Expand Down
4 changes: 2 additions & 2 deletions pkg/sentry/event/beacon/eth/v1/events_chain_reorg.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (
"github.com/ethpandaops/xatu/pkg/proto/xatu"
"github.com/ethpandaops/xatu/pkg/sentry/ethereum"
"github.com/google/uuid"
ttlcache "github.com/jellydator/ttlcache/v3"
hashstructure "github.com/mitchellh/hashstructure/v2"
ttlcache "github.com/savid/ttlcache/v3"
"github.com/sirupsen/logrus"
"google.golang.org/protobuf/types/known/timestamppb"
"google.golang.org/protobuf/types/known/wrapperspb"
Expand Down Expand Up @@ -78,7 +78,7 @@ func (e *EventsChainReorg) ShouldIgnore(ctx context.Context) (bool, error) {
return true, err
}

item, retrieved := e.duplicateCache.GetOrSet(fmt.Sprint(hash), e.now, ttlcache.DefaultTTL)
item, retrieved := e.duplicateCache.GetOrSet(fmt.Sprint(hash), e.now, ttlcache.WithTTL[string, time.Time](ttlcache.DefaultTTL))
if retrieved {
e.log.WithFields(logrus.Fields{
"hash": hash,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (
"github.com/ethpandaops/xatu/pkg/proto/xatu"
"github.com/ethpandaops/xatu/pkg/sentry/ethereum"
"github.com/google/uuid"
ttlcache "github.com/jellydator/ttlcache/v3"
hashstructure "github.com/mitchellh/hashstructure/v2"
ttlcache "github.com/savid/ttlcache/v3"
"github.com/sirupsen/logrus"
"google.golang.org/protobuf/types/known/timestamppb"
"google.golang.org/protobuf/types/known/wrapperspb"
Expand Down Expand Up @@ -91,7 +91,7 @@ func (e *EventsContributionAndProof) ShouldIgnore(ctx context.Context) (bool, er
return true, err
}

item, retrieved := e.duplicateCache.GetOrSet(fmt.Sprint(hash), e.now, ttlcache.DefaultTTL)
item, retrieved := e.duplicateCache.GetOrSet(fmt.Sprint(hash), e.now, ttlcache.WithTTL[string, time.Time](ttlcache.DefaultTTL))
if retrieved {
e.log.WithFields(logrus.Fields{
"hash": hash,
Expand Down
4 changes: 2 additions & 2 deletions pkg/sentry/event/beacon/eth/v1/events_finalized_checkpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (
"github.com/ethpandaops/xatu/pkg/proto/xatu"
"github.com/ethpandaops/xatu/pkg/sentry/ethereum"
"github.com/google/uuid"
ttlcache "github.com/jellydator/ttlcache/v3"
hashstructure "github.com/mitchellh/hashstructure/v2"
ttlcache "github.com/savid/ttlcache/v3"
"github.com/sirupsen/logrus"
"google.golang.org/protobuf/types/known/timestamppb"
"google.golang.org/protobuf/types/known/wrapperspb"
Expand Down Expand Up @@ -82,7 +82,7 @@ func (e *EventsFinalizedCheckpoint) ShouldIgnore(ctx context.Context) (bool, err
return true, err
}

item, retrieved := e.duplicateCache.GetOrSet(fmt.Sprint(hash), e.now, ttlcache.DefaultTTL)
item, retrieved := e.duplicateCache.GetOrSet(fmt.Sprint(hash), e.now, ttlcache.WithTTL[string, time.Time](ttlcache.DefaultTTL))
if retrieved {
e.log.WithFields(logrus.Fields{
"hash": hash,
Expand Down
4 changes: 2 additions & 2 deletions pkg/sentry/event/beacon/eth/v1/events_head.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (
"github.com/ethpandaops/xatu/pkg/proto/xatu"
"github.com/ethpandaops/xatu/pkg/sentry/ethereum"
"github.com/google/uuid"
ttlcache "github.com/jellydator/ttlcache/v3"
hashstructure "github.com/mitchellh/hashstructure/v2"
ttlcache "github.com/savid/ttlcache/v3"
"github.com/sirupsen/logrus"
"google.golang.org/protobuf/types/known/timestamppb"
"google.golang.org/protobuf/types/known/wrapperspb"
Expand Down Expand Up @@ -85,7 +85,7 @@ func (e *EventsHead) ShouldIgnore(ctx context.Context) (bool, error) {
return true, err
}

item, retrieved := e.duplicateCache.GetOrSet(fmt.Sprint(hash), e.now, ttlcache.DefaultTTL)
item, retrieved := e.duplicateCache.GetOrSet(fmt.Sprint(hash), e.now, ttlcache.WithTTL[string, time.Time](ttlcache.DefaultTTL))
if retrieved {
e.log.WithFields(logrus.Fields{
"hash": hash,
Expand Down
4 changes: 2 additions & 2 deletions pkg/sentry/event/beacon/eth/v1/events_voluntary_exit.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (
"github.com/ethpandaops/xatu/pkg/proto/xatu"
"github.com/ethpandaops/xatu/pkg/sentry/ethereum"
"github.com/google/uuid"
ttlcache "github.com/jellydator/ttlcache/v3"
hashstructure "github.com/mitchellh/hashstructure/v2"
ttlcache "github.com/savid/ttlcache/v3"
"github.com/sirupsen/logrus"
"google.golang.org/protobuf/types/known/timestamppb"
"google.golang.org/protobuf/types/known/wrapperspb"
Expand Down Expand Up @@ -85,7 +85,7 @@ func (e *EventsVoluntaryExit) ShouldIgnore(ctx context.Context) (bool, error) {
return true, err
}

item, retrieved := e.duplicateCache.GetOrSet(fmt.Sprint(hash), e.now, ttlcache.DefaultTTL)
item, retrieved := e.duplicateCache.GetOrSet(fmt.Sprint(hash), e.now, ttlcache.WithTTL[string, time.Time](ttlcache.DefaultTTL))
if retrieved {
e.log.WithFields(logrus.Fields{
"hash": hash,
Expand Down
4 changes: 2 additions & 2 deletions pkg/sentry/event/beacon/eth/v2/beacon_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import (
"github.com/ethpandaops/xatu/pkg/proto/xatu"
"github.com/ethpandaops/xatu/pkg/sentry/ethereum"
"github.com/google/uuid"
ttlcache "github.com/jellydator/ttlcache/v3"
hashstructure "github.com/mitchellh/hashstructure/v2"
ttlcache "github.com/savid/ttlcache/v3"
"github.com/sirupsen/logrus"
"google.golang.org/protobuf/types/known/timestamppb"
"google.golang.org/protobuf/types/known/wrapperspb"
Expand Down Expand Up @@ -103,7 +103,7 @@ func (e *BeaconBlock) ShouldIgnore(ctx context.Context) (bool, error) {
return true, err
}

item, retrieved := e.duplicateCache.GetOrSet(fmt.Sprint(hash), e.now, ttlcache.DefaultTTL)
item, retrieved := e.duplicateCache.GetOrSet(fmt.Sprint(hash), e.now, ttlcache.WithTTL[string, time.Time](ttlcache.DefaultTTL))
if retrieved {
e.log.WithFields(logrus.Fields{
"hash": hash,
Expand Down
2 changes: 1 addition & 1 deletion pkg/server/geoip/maxmind/maxmind.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

"github.com/ethpandaops/xatu/pkg/server/geoip/lookup"
"github.com/ethpandaops/xatu/pkg/server/geoip/maxmind/database"
"github.com/savid/ttlcache/v3"
"github.com/jellydator/ttlcache/v3"
"github.com/sirupsen/logrus"
)

Expand Down
4 changes: 2 additions & 2 deletions pkg/server/store/memory/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"context"
"time"

"github.com/savid/ttlcache/v3"
"github.com/jellydator/ttlcache/v3"
"github.com/sirupsen/logrus"
)

Expand Down Expand Up @@ -58,7 +58,7 @@ func (m *Memory) Get(ctx context.Context, key string) (*string, error) {
}

func (m *Memory) GetOrSet(ctx context.Context, key, value string, ttl time.Duration) (storedValue *string, retrieved bool, err error) {
item, retrieved := m.client.GetOrSet(key, value, ttl)
item, retrieved := m.client.GetOrSet(key, value, ttlcache.WithTTL[string, string](ttl))

if retrieved {
m.metrics.AddGet(1, m.Type(), "hit")
Expand Down

0 comments on commit 32bd250

Please sign in to comment.