Skip to content

Commit 2d61ad8

Browse files
committed
ethreceipts: port #188
1 parent 9093498 commit 2d61ad8

File tree

3 files changed

+23
-13
lines changed

3 files changed

+23
-13
lines changed

ethreceipts/ethreceipts_test.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -756,6 +756,8 @@ func TestFiltersAddDeadlock(t *testing.T) {
756756
}
757757

758758
func TestFlakyProvider(t *testing.T) {
759+
const subscribers = 20
760+
759761
t.Run("Wait for txn receipts with a healthy monitor and a healthy provider", func(t *testing.T) {
760762
ctx, cancel := context.WithCancel(context.Background())
761763
defer cancel()
@@ -800,7 +802,7 @@ func TestFlakyProvider(t *testing.T) {
800802

801803
var wg sync.WaitGroup
802804

803-
for i := 0; i < 10; i++ {
805+
for i := 0; i < subscribers; i++ {
804806
txr := &ethtxn.TransactionRequest{
805807
To: &walletBAddress,
806808
ETHValue: ethtest.ETHValue(0.01),
@@ -917,7 +919,7 @@ func TestFlakyProvider(t *testing.T) {
917919
var wg sync.WaitGroup
918920

919921
// Add a bunch of subscribers that will each send a txn and wait for it
920-
for i := 0; i < 10; i++ {
922+
for i := 0; i < subscribers; i++ {
921923
txr := &ethtxn.TransactionRequest{
922924
To: &walletBAddress,
923925
ETHValue: ethtest.ETHValue(0.01),
@@ -1034,7 +1036,7 @@ func TestFlakyProvider(t *testing.T) {
10341036
var wg sync.WaitGroup
10351037

10361038
// Add a bunch of subscribers that will each send a txn and wait for it
1037-
for i := 0; i < 10; i++ {
1039+
for i := 0; i < subscribers; i++ {
10381040
txr := &ethtxn.TransactionRequest{
10391041
To: &walletBAddress,
10401042
ETHValue: ethtest.ETHValue(0.01),
@@ -1151,7 +1153,7 @@ func TestFlakyProvider(t *testing.T) {
11511153
var wg sync.WaitGroup
11521154

11531155
// Add a bunch of subscribers that will each send a txn and wait for it
1154-
for i := 0; i < 10; i++ {
1156+
for i := 0; i < subscribers; i++ {
11551157
txr := &ethtxn.TransactionRequest{
11561158
To: &walletBAddress,
11571159
ETHValue: ethtest.ETHValue(0.01),

ethrpc/ethrpc.go

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ type Provider struct {
2828
log logger.Logger
2929
nodeURL string
3030
nodeWSURL string
31-
httpClient httpClient
31+
httpClient atomic.Value
3232
br breaker.Breaker
3333
jwtToken string // optional
3434
streamClosers []StreamCloser
@@ -47,17 +47,20 @@ type Provider struct {
4747
func NewProvider(nodeURL string, options ...Option) (*Provider, error) {
4848
p := &Provider{
4949
nodeURL: nodeURL,
50-
httpClient: &http.Client{
51-
// default timeout of 60 seconds
52-
Timeout: 60 * time.Second,
53-
},
5450
}
5551
for _, opt := range options {
5652
if opt == nil {
5753
continue
5854
}
5955
opt(p)
6056
}
57+
if p.httpClient.Load() == nil {
58+
httpClient := &http.Client{
59+
// default timeout of 60 seconds
60+
Timeout: 60 * time.Second,
61+
}
62+
p.httpClient.Store(httpClient)
63+
}
6164
return p, nil
6265
}
6366

@@ -85,8 +88,12 @@ type StreamUnsubscriber interface {
8588
Unsubscribe()
8689
}
8790

88-
func (s *Provider) SetHTTPClient(httpClient *http.Client) {
89-
s.httpClient = httpClient
91+
func (s *Provider) SetHTTPClient(client httpClient) {
92+
s.httpClient.Store(client)
93+
}
94+
95+
func (p *Provider) getHTTPClient() httpClient {
96+
return p.httpClient.Load().(httpClient)
9097
}
9198

9299
func (p *Provider) StrictnessLevel() StrictnessLevel {
@@ -126,7 +133,8 @@ func (p *Provider) Do(ctx context.Context, calls ...Call) ([]byte, error) {
126133
req.Header.Set("Authorization", fmt.Sprintf("BEARER %s", p.jwtToken))
127134
}
128135

129-
res, err := p.httpClient.Do(req)
136+
httpClient := p.getHTTPClient()
137+
res, err := httpClient.Do(req)
130138
if err != nil {
131139
return nil, superr.Wrap(ErrRequestFail, fmt.Errorf("failed to send request: %w", err))
132140
}

ethrpc/option.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func WithStreaming(nodeWebsocketURL string) Option {
2525

2626
func WithHTTPClient(c httpClient) Option {
2727
return func(p *Provider) {
28-
p.httpClient = c
28+
p.httpClient.Store(c)
2929
}
3030
}
3131

0 commit comments

Comments
 (0)