@@ -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 {
4747func 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
9299func (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 }
0 commit comments