Skip to content
This repository was archived by the owner on Oct 20, 2024. It is now read-only.

Commit 3ca4a1d

Browse files
authored
reuse http client (metachris#14)
1 parent 1146f37 commit 3ca4a1d

File tree

3 files changed

+11
-15
lines changed

3 files changed

+11
-15
lines changed

flashbotsrpc.go

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"crypto/ecdsa"
66
"encoding/json"
77
"fmt"
8-
"io/ioutil"
8+
"io"
99
"log"
1010
"math/big"
1111
"net/http"
@@ -56,15 +56,16 @@ type FlashbotsRPC struct {
5656
func New(url string, options ...func(rpc *FlashbotsRPC)) *FlashbotsRPC {
5757
rpc := &FlashbotsRPC{
5858
url: url,
59-
client: http.DefaultClient,
6059
log: log.New(os.Stderr, "", log.LstdFlags),
6160
Headers: make(map[string]string),
6261
Timeout: 30 * time.Second,
6362
}
6463
for _, option := range options {
6564
option(rpc)
6665
}
67-
66+
rpc.client = &http.Client{
67+
Timeout: rpc.Timeout,
68+
}
6869
return rpc
6970
}
7071

@@ -115,19 +116,16 @@ func (rpc *FlashbotsRPC) Call(method string, params ...interface{}) (json.RawMes
115116
for k, v := range rpc.Headers {
116117
req.Header.Add(k, v)
117118
}
118-
httpClient := &http.Client{
119-
Timeout: rpc.Timeout,
120-
}
121119

122-
response, err := httpClient.Do(req)
120+
response, err := rpc.client.Do(req)
123121
if response != nil {
124122
defer response.Body.Close()
125123
}
126124
if err != nil {
127125
return nil, err
128126
}
129127

130-
data, err := ioutil.ReadAll(response.Body)
128+
data, err := io.ReadAll(response.Body)
131129
if err != nil {
132130
return nil, err
133131
}
@@ -181,19 +179,16 @@ func (rpc *FlashbotsRPC) CallWithFlashbotsSignature(method string, privKey *ecds
181179
for k, v := range rpc.Headers {
182180
req.Header.Add(k, v)
183181
}
184-
httpClient := &http.Client{
185-
Timeout: rpc.Timeout,
186-
}
187182

188-
response, err := httpClient.Do(req)
183+
response, err := rpc.client.Do(req)
189184
if response != nil {
190185
defer response.Body.Close()
191186
}
192187
if err != nil {
193188
return nil, err
194189
}
195190

196-
data, err := ioutil.ReadAll(response.Body)
191+
data, err := io.ReadAll(response.Body)
197192
if err != nil {
198193
return nil, err
199194
}

flashbotsrpc_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"crypto/ecdsa"
77
"errors"
88
"fmt"
9-
"io/ioutil"
9+
"io"
1010
"math/big"
1111
"net/http"
1212
"strconv"
@@ -44,7 +44,7 @@ func (s *FlashbotsRPCTestSuite) registerResponseError(err error) {
4444

4545
func (s *FlashbotsRPCTestSuite) getBody(request *http.Request) []byte {
4646
defer request.Body.Close()
47-
body, err := ioutil.ReadAll(request.Body)
47+
body, err := io.ReadAll(request.Body)
4848
s.Require().Nil(err)
4949

5050
return body

options.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77

88
type httpClient interface {
99
Post(url string, contentType string, body io.Reader) (*http.Response, error)
10+
Do(req *http.Request) (*http.Response, error)
1011
}
1112

1213
type logger interface {

0 commit comments

Comments
 (0)