-
Notifications
You must be signed in to change notification settings - Fork 32
/
capture_client.go
46 lines (38 loc) · 1.14 KB
/
capture_client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package client
import (
"context"
"fmt"
"net/http"
"time"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/rpc"
"github.com/lmittmann/w3"
"github.com/synapsecns/sanguine/core/metrics"
"github.com/synapsecns/sanguine/core/metrics/instrumentation/httpcapture"
)
// captureClient is a wrapper around ethclient that can (but doesn't have to) captures requests and responses.
type captureClient struct {
ethClient *ethclient.Client
w3Client *w3.Client
rpcClient *rpc.Client
}
var defaultTimeout = 30 * time.Second
func newCaptureClient(ctx context.Context, url string, handler metrics.Handler, capture bool) (*captureClient, error) {
client := new(http.Client)
client.Timeout = defaultTimeout
if capture {
client.Transport = httpcapture.NewCaptureTransport(client.Transport, handler)
}
c, err := metrics.RPCClient(ctx, handler, url, client)
if err != nil {
return nil, fmt.Errorf("failed to create rpc client: %w", err)
}
// capture config goes here
ethClient := ethclient.NewClient(c)
w3Client := w3.NewClient(c)
return &captureClient{
ethClient: ethClient,
w3Client: w3Client,
rpcClient: c,
}, nil
}