Skip to content

Commit c6886d1

Browse files
committed
chore: refactor to use CacheOptions struct
1 parent b03a8b3 commit c6886d1

File tree

4 files changed

+42
-49
lines changed

4 files changed

+42
-49
lines changed

cmd/p2p/sensor/sensor.go

Lines changed: 23 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,9 @@ type (
6969
DiscoveryDNS string
7070
Database string
7171
NoDiscovery bool
72-
MaxRequests int
73-
RequestsCacheTTL time.Duration
74-
MaxParents int
75-
ParentsCacheTTL time.Duration
76-
MaxBlocks int
77-
BlocksCacheTTL time.Duration
72+
RequestsCache p2p.CacheOptions
73+
ParentsCache p2p.CacheOptions
74+
BlocksCache p2p.CacheOptions
7875

7976
bootnodes []*enode.Node
8077
staticNodes []*enode.Node
@@ -198,26 +195,23 @@ var SensorCmd = &cobra.Command{
198195
// Create peer connection manager for broadcasting transactions
199196
// and managing the global blocks cache
200197
conns := p2p.NewConns(p2p.ConnsOptions{
201-
MaxBlocks: inputSensorParams.MaxBlocks,
202-
BlocksCacheTTL: inputSensorParams.BlocksCacheTTL,
198+
BlocksCache: inputSensorParams.BlocksCache,
203199
})
204200

205201
opts := p2p.EthProtocolOptions{
206-
Context: cmd.Context(),
207-
Database: db,
208-
GenesisHash: common.HexToHash(inputSensorParams.GenesisHash),
209-
RPC: inputSensorParams.RPC,
210-
SensorID: inputSensorParams.SensorID,
211-
NetworkID: inputSensorParams.NetworkID,
212-
Conns: conns,
213-
Head: &head,
214-
HeadMutex: &sync.RWMutex{},
215-
ForkID: forkid.ID{Hash: [4]byte(inputSensorParams.ForkID)},
216-
MsgCounter: msgCounter,
217-
MaxRequests: inputSensorParams.MaxRequests,
218-
RequestsCacheTTL: inputSensorParams.RequestsCacheTTL,
219-
MaxParents: inputSensorParams.MaxParents,
220-
ParentsCacheTTL: inputSensorParams.ParentsCacheTTL,
202+
Context: cmd.Context(),
203+
Database: db,
204+
GenesisHash: common.HexToHash(inputSensorParams.GenesisHash),
205+
RPC: inputSensorParams.RPC,
206+
SensorID: inputSensorParams.SensorID,
207+
NetworkID: inputSensorParams.NetworkID,
208+
Conns: conns,
209+
Head: &head,
210+
HeadMutex: &sync.RWMutex{},
211+
ForkID: forkid.ID{Hash: [4]byte(inputSensorParams.ForkID)},
212+
MsgCounter: msgCounter,
213+
RequestsCache: inputSensorParams.RequestsCache,
214+
ParentsCache: inputSensorParams.ParentsCache,
221215
}
222216

223217
config := ethp2p.Config{
@@ -490,10 +484,10 @@ will result in less chance of missing data but can significantly increase memory
490484
- json (output to stdout)
491485
- none (no persistence)`)
492486
f.BoolVar(&inputSensorParams.NoDiscovery, "no-discovery", false, "disable P2P peer discovery")
493-
f.IntVar(&inputSensorParams.MaxRequests, "max-requests", 2048, "maximum request IDs to track per peer (0 for no limit)")
494-
f.DurationVar(&inputSensorParams.RequestsCacheTTL, "requests-cache-ttl", 5*time.Minute, "time to live for requests cache entries (0 for no expiration)")
495-
f.IntVar(&inputSensorParams.MaxParents, "max-parents", 1024, "maximum parent block hashes to track per peer (0 for no limit)")
496-
f.DurationVar(&inputSensorParams.ParentsCacheTTL, "parents-cache-ttl", 5*time.Minute, "time to live for parent hash cache entries (0 for no expiration)")
497-
f.IntVar(&inputSensorParams.MaxBlocks, "max-blocks", 1024, "maximum blocks to track across all peers (0 for no limit)")
498-
f.DurationVar(&inputSensorParams.BlocksCacheTTL, "blocks-cache-ttl", 10*time.Minute, "time to live for block cache entries (0 for no expiration)")
487+
f.IntVar(&inputSensorParams.RequestsCache.MaxSize, "max-requests", 2048, "maximum request IDs to track per peer (0 for no limit)")
488+
f.DurationVar(&inputSensorParams.RequestsCache.TTL, "requests-cache-ttl", 5*time.Minute, "time to live for requests cache entries (0 for no expiration)")
489+
f.IntVar(&inputSensorParams.ParentsCache.MaxSize, "max-parents", 1024, "maximum parent block hashes to track per peer (0 for no limit)")
490+
f.DurationVar(&inputSensorParams.ParentsCache.TTL, "parents-cache-ttl", 5*time.Minute, "time to live for parent hash cache entries (0 for no expiration)")
491+
f.IntVar(&inputSensorParams.BlocksCache.MaxSize, "max-blocks", 1024, "maximum blocks to track across all peers (0 for no limit)")
492+
f.DurationVar(&inputSensorParams.BlocksCache.TTL, "blocks-cache-ttl", 10*time.Minute, "time to live for block cache entries (0 for no expiration)")
499493
}

p2p/cache.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ import (
66
"time"
77
)
88

9+
// CacheOptions contains configuration for LRU caches with TTL.
10+
type CacheOptions struct {
11+
MaxSize int
12+
TTL time.Duration
13+
}
14+
915
// Cache is a thread-safe LRU cache with optional TTL-based expiration.
1016
type Cache[K comparable, V any] struct {
1117
mu sync.RWMutex
@@ -21,13 +27,13 @@ type entry[K comparable, V any] struct {
2127
expiresAt time.Time
2228
}
2329

24-
// NewCache creates a new cache with the given max size and optional TTL.
25-
// If maxSize <= 0, the cache has no size limit.
26-
// If ttl is 0, entries never expire based on time.
27-
func NewCache[K comparable, V any](maxSize int, ttl time.Duration) *Cache[K, V] {
30+
// NewCache creates a new cache with the given options.
31+
// If opts.MaxSize <= 0, the cache has no size limit.
32+
// If opts.TTL is 0, entries never expire based on time.
33+
func NewCache[K comparable, V any](opts CacheOptions) *Cache[K, V] {
2834
return &Cache[K, V]{
29-
maxSize: maxSize,
30-
ttl: ttl,
35+
maxSize: opts.MaxSize,
36+
ttl: opts.TTL,
3137
items: make(map[K]*list.Element),
3238
list: list.New(),
3339
}

p2p/conns.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,14 @@ type Conns struct {
2424

2525
// ConnsOptions contains configuration options for creating a new Conns manager.
2626
type ConnsOptions struct {
27-
// MaxBlocks is the maximum number of blocks to track in the cache.
28-
MaxBlocks int
29-
// BlocksCacheTTL is the time to live for block cache entries.
30-
BlocksCacheTTL time.Duration
27+
BlocksCache CacheOptions
3128
}
3229

3330
// NewConns creates a new connection manager with a blocks cache.
3431
func NewConns(opts ConnsOptions) *Conns {
3532
return &Conns{
3633
conns: make(map[string]*conn),
37-
blocks: NewCache[common.Hash, BlockCache](opts.MaxBlocks, opts.BlocksCacheTTL),
34+
blocks: NewCache[common.Hash, BlockCache](opts.BlocksCache),
3835
}
3936
}
4037

p2p/protocol.go

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,9 @@ type EthProtocolOptions struct {
8484
Head *HeadBlock
8585
HeadMutex *sync.RWMutex
8686

87-
// Requests cache configuration
88-
MaxRequests int
89-
RequestsCacheTTL time.Duration
90-
91-
// Parent hash tracking cache configuration
92-
MaxParents int
93-
ParentsCacheTTL time.Duration
87+
// Cache configurations
88+
RequestsCache CacheOptions
89+
ParentsCache CacheOptions
9490
}
9591

9692
// HeadBlock contains the necessary head block data for the status message.
@@ -116,9 +112,9 @@ func NewEthProtocol(version uint, opts EthProtocolOptions) ethp2p.Protocol {
116112
logger: log.With().Str("peer", peerURL).Logger(),
117113
rw: rw,
118114
db: opts.Database,
119-
requests: NewCache[uint64, common.Hash](opts.MaxRequests, opts.RequestsCacheTTL),
115+
requests: NewCache[uint64, common.Hash](opts.RequestsCache),
120116
requestNum: 0,
121-
parents: NewCache[common.Hash, struct{}](opts.MaxParents, opts.ParentsCacheTTL),
117+
parents: NewCache[common.Hash, struct{}](opts.ParentsCache),
122118
head: opts.Head,
123119
headMutex: opts.HeadMutex,
124120
counter: opts.MsgCounter,

0 commit comments

Comments
 (0)