Skip to content

Commit

Permalink
eth/executionclient: rate limit multi client Healthy call (#2010)
Browse files Browse the repository at this point in the history
* eth/executionclient: rate limit Healthy call

* use atomic instead of mutex

* use < instead of <=
  • Loading branch information
nkryuchkov authored Feb 3, 2025
1 parent 285cd60 commit 30c677f
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
2 changes: 1 addition & 1 deletion eth/executionclient/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const (
DefaultConnectionTimeout = 10 * time.Second
DefaultReconnectionInitialInterval = 1 * time.Second
DefaultReconnectionMaxInterval = 64 * time.Second
DefaultHealthInvalidationInterval = 10 * time.Second // TODO: decide on this value, for now choosing a bit less than block interval
DefaultHealthInvalidationInterval = 24 * time.Second // TODO: decide on this value, for now choosing the node prober interval but it should probably be a bit less than block interval
DefaultFollowDistance = 8
// TODO ALAN: revert
DefaultHistoricalLogsBatchSize = 500
Expand Down
6 changes: 6 additions & 0 deletions eth/executionclient/multi_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ type MultiClient struct {
clientsMu []sync.Mutex // clientsMu allow for lazy initialization of each client in `clients` slice in thread-safe manner (atomically)
clients []SingleClientProvider // nil if not connected
currentClientIndex atomic.Int64
lastHealthy atomic.Int64
}

// NewMulti creates a new instance of MultiClient.
Expand Down Expand Up @@ -278,6 +279,10 @@ func (mc *MultiClient) StreamLogs(ctx context.Context, fromBlock uint64) <-chan

// Healthy returns if execution client is currently healthy: responds to requests and not in the syncing state.
func (mc *MultiClient) Healthy(ctx context.Context) error {
if time.Since(time.Unix(mc.lastHealthy.Load(), 0)) < mc.healthInvalidationInterval {
return nil
}

Check warning on line 284 in eth/executionclient/multi_client.go

View check run for this annotation

Codecov / codecov/patch

eth/executionclient/multi_client.go#L283-L284

Added lines #L283 - L284 were not covered by tests

healthyClients := atomic.Bool{}
p := pool.New().WithErrors().WithContext(ctx)

Expand Down Expand Up @@ -307,6 +312,7 @@ func (mc *MultiClient) Healthy(ctx context.Context) error {
}
err := p.Wait()
if healthyClients.Load() {
mc.lastHealthy.Store(time.Now().Unix())
return nil
}
return fmt.Errorf("no healthy clients: %w", err)
Expand Down

0 comments on commit 30c677f

Please sign in to comment.