@@ -15,6 +15,11 @@ import (
1515 "golang.org/x/time/rate"
1616)
1717
18+ const (
19+ // RPC type constants
20+ nodeRPCType = "Node"
21+ )
22+
1823// Manager unified RPC resource manager
1924type Manager struct {
2025 // Configuration
@@ -74,17 +79,17 @@ type Stats struct {
7479func NewManager (config Config ) (* Manager , error ) {
7580 // Validate configuration
7681 if len (config .L1RPCUrls ) == 0 {
77- return nil , fmt .Errorf ("L1 RPC URLs cannot be empty" )
82+ return nil , fmt .Errorf ("l1 RPC URLs cannot be empty" )
7883 }
7984 if len (config .L2RPCUrls ) == 0 {
80- return nil , fmt .Errorf ("L2 RPC URLs cannot be empty" )
85+ return nil , fmt .Errorf ("l2 RPC URLs cannot be empty" )
8186 }
8287 if len (config .NodeRPCUrls ) == 0 {
83- return nil , fmt .Errorf ("Node RPC URLs cannot be empty" )
88+ return nil , fmt .Errorf ("node RPC URLs cannot be empty" )
8489 }
8590
8691 // Create L1 client pool
87- var l1Clients []* ethclient.Client
92+ l1Clients := make ( []* ethclient.Client , 0 , len ( config . L1RPCUrls ))
8893 for _ , url := range config .L1RPCUrls {
8994 client , err := ethclient .Dial (url )
9095 if err != nil {
@@ -94,7 +99,7 @@ func NewManager(config Config) (*Manager, error) {
9499 }
95100
96101 // Create L2 client pool
97- var l2Clients []* ethclient.Client
102+ l2Clients := make ( []* ethclient.Client , 0 , len ( config . L2RPCUrls ))
98103 for _ , url := range config .L2RPCUrls {
99104 client , err := ethclient .Dial (url )
100105 if err != nil {
@@ -104,11 +109,11 @@ func NewManager(config Config) (*Manager, error) {
104109 }
105110
106111 // Create Node client pool
107- var nodeClients []* ethclient.Client
112+ nodeClients := make ( []* ethclient.Client , 0 , len ( config . NodeRPCUrls ))
108113 for _ , url := range config .NodeRPCUrls {
109114 client , err := ethclient .Dial (url )
110115 if err != nil {
111- return nil , fmt .Errorf ("failed to connect to Node RPC %s: %w" , url , err )
116+ return nil , fmt .Errorf ("failed to connect to %s RPC %s: %w" , nodeRPCType , url , err )
112117 }
113118 nodeClients = append (nodeClients , client )
114119 }
@@ -361,7 +366,7 @@ func (m *Manager) UpdateRateLimit(rateLimit int, rateBurst int, rpcType string)
361366 case "L2" :
362367 m .l2Limiter .SetLimit (rate .Limit (rateLimit ))
363368 m .l2Limiter .SetBurst (rateBurst )
364- case "Node" :
369+ case nodeRPCType :
365370 m .nodeLimiter .SetLimit (rate .Limit (rateLimit ))
366371 m .nodeLimiter .SetBurst (rateBurst )
367372 }
@@ -377,7 +382,7 @@ func (m *Manager) GetRateLimit(rpcType string) (rateLimit float64, rateBurst int
377382 return float64 (m .l1Limiter .Limit ()), m .l1Limiter .Burst ()
378383 case "L2" :
379384 return float64 (m .l2Limiter .Limit ()), m .l2Limiter .Burst ()
380- case "Node" :
385+ case nodeRPCType :
381386 return float64 (m .nodeLimiter .Limit ()), m .nodeLimiter .Burst ()
382387 default :
383388 return 0 , 0
@@ -391,7 +396,7 @@ func (m *Manager) GetTokens(rpcType string) float64 {
391396 return m .l1Limiter .Tokens ()
392397 case "L2" :
393398 return m .l2Limiter .Tokens ()
394- case "Node" :
399+ case nodeRPCType :
395400 return m .nodeLimiter .Tokens ()
396401 default :
397402 return 0
@@ -438,7 +443,7 @@ func (m *Manager) updateRequestStats(rpcType string) {
438443 m .stats .L1RequestCount ++
439444 case "L2" :
440445 m .stats .L2RequestCount ++
441- case "Node" :
446+ case nodeRPCType :
442447 m .stats .NodeRequestCount ++
443448 }
444449}
@@ -453,7 +458,7 @@ func (m *Manager) updateRateLimitedStats(rpcType string) {
453458 m .stats .L1RateLimitedCount ++
454459 case "L2" :
455460 m .stats .L2RateLimitedCount ++
456- case "Node" :
461+ case nodeRPCType :
457462 m .stats .NodeRateLimitedCount ++
458463 }
459464}
0 commit comments