Skip to content

Commit

Permalink
ethrpc: add mutex to memoized chain id
Browse files Browse the repository at this point in the history
  • Loading branch information
attente committed Sep 6, 2024
1 parent ad7994c commit 6cd867a
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion ethrpc/ethrpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"io"
"math/big"
"net/http"
"sync"
"sync/atomic"

"github.com/0xsequence/ethkit/ethrpc/jsonrpc"
Expand All @@ -30,7 +31,9 @@ type Provider struct {
br breaker.Breaker
jwtToken string // optional

chainID *big.Int
chainID *big.Int
chainIDMu sync.Mutex

// cache cachestore.Store[[]byte] // NOTE: unused for now
lastRequestID uint64

Expand Down Expand Up @@ -165,6 +168,8 @@ func (p *Provider) Do(ctx context.Context, calls ...Call) ([]byte, error) {
}

func (p *Provider) ChainID(ctx context.Context) (*big.Int, error) {
p.chainIDMu.Lock()
defer p.chainIDMu.Unlock()
if p.chainID != nil {
// chainID is memoized
return p.chainID, nil
Expand Down

0 comments on commit 6cd867a

Please sign in to comment.