forked from filecoin-project/go-jsonrpc
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathoptions.go
125 lines (103 loc) · 2.75 KB
/
options.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package jsonrpc
import (
"net"
"net/http"
"reflect"
"time"
"github.com/gorilla/websocket"
)
var _defaultTransport = &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
DualStack: true,
}).DialContext,
ForceAttemptHTTP2: true,
MaxIdleConns: 100,
MaxIdleConnsPerHost: 100,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
}
type ParamEncoder func(reflect.Value) (reflect.Value, error)
type Config struct {
reconnectBackoff backoff
pingInterval time.Duration
timeout time.Duration
paramEncoders map[reflect.Type]ParamEncoder
noReconnect bool
proxyConnFactory func(func() (*websocket.Conn, error)) func() (*websocket.Conn, error) // for testing
transport *http.Transport
}
func defaultConfig() Config {
return Config{
reconnectBackoff: backoff{
minDelay: 100 * time.Millisecond,
maxDelay: 5 * time.Second,
},
pingInterval: 5 * time.Second,
timeout: 30 * time.Second,
paramEncoders: map[reflect.Type]ParamEncoder{},
transport: _defaultTransport,
}
}
type Option func(c *Config)
func WithReconnectBackoff(minDelay, maxDelay time.Duration) func(c *Config) {
return func(c *Config) {
c.reconnectBackoff = backoff{
minDelay: minDelay,
maxDelay: maxDelay,
}
}
}
// Must be < Timeout/2
func WithPingInterval(d time.Duration) func(c *Config) {
return func(c *Config) {
c.pingInterval = d
}
}
func WithTimeout(d time.Duration) func(c *Config) {
return func(c *Config) {
c.timeout = d
}
}
func WithNoReconnect() func(c *Config) {
return func(c *Config) {
c.noReconnect = true
}
}
func WithParamEncoder(t interface{}, encoder ParamEncoder) func(c *Config) {
return func(c *Config) {
c.paramEncoders[reflect.TypeOf(t).Elem()] = encoder
}
}
func WithTransportMaxIdleConns(value int) func(c *Config) {
return func(c *Config) {
c.transport.MaxIdleConns = value
}
}
func WithTransportMaxIdleConnsPerHost(value int) func(c *Config) {
return func(c *Config) {
c.transport.MaxIdleConnsPerHost = value
}
}
func WithTransportIdleConnTimeout(valueSecond time.Duration) func(c *Config) {
return func(c *Config) {
c.transport.IdleConnTimeout = valueSecond * time.Second
}
}
func WithTransportTLSHandshakeTimeout(valueSecond time.Duration) func(c *Config) {
return func(c *Config) {
c.transport.TLSHandshakeTimeout = valueSecond * time.Second
}
}
func WithTransportDialContext(timeoutSecond, keepAliveSecond time.Duration) func(c *Config) {
return func(c *Config) {
c.transport.DialContext = (&net.Dialer{
Timeout: timeoutSecond * time.Second,
KeepAlive: keepAliveSecond * time.Second,
DualStack: true,
}).DialContext
}
}