-
Notifications
You must be signed in to change notification settings - Fork 3
/
serveroptions.go
118 lines (96 loc) · 3.07 KB
/
serveroptions.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
package wsrpc
import (
"crypto/ed25519"
"time"
"github.com/smartcontractkit/wsrpc/credentials"
)
// A ServerOption sets options such as credentials, codec and keepalive parameters, etc.
type ServerOption interface {
apply(*serverOptions)
}
type serverOptions struct {
// Buffer
writeBufferSize int
readBufferSize int
// Transport Credentials
creds credentials.TransportCredentials
// The address that the healthcheck will run on
healthcheckAddr string
// The HTTP ReadTimeout the healthcheck will use. Set to 0 for no timeout
healthcheckTimeout time.Duration
// The HTTP ReadTimeout the ws server will use. Set to 0 for no timeout
wsTimeout time.Duration
// The request size limit the ws server will use in bytes. Defaults to 10MB.
wsReadLimit int64
}
// funcServerOption wraps a function that modifies serverOptions into an
// implementation of the ServerOption interface.
type funcServerOption struct {
f func(*serverOptions)
}
func newFuncServerOption(f func(*serverOptions)) *funcServerOption {
return &funcServerOption{
f: f,
}
}
func (fdo *funcServerOption) apply(do *serverOptions) {
fdo.f(do)
}
// returns a ServerOption that sets the healthcheck HTTP read timeout and the server HTTP read timeout
func WithHTTPReadTimeout(hctime time.Duration, wstime time.Duration) ServerOption {
return newFuncServerOption(func(o *serverOptions) {
o.healthcheckTimeout = hctime
o.wsTimeout = wstime
})
}
func WithWSReadLimit(numBytes int64) ServerOption {
return newFuncServerOption(func(o *serverOptions) {
o.wsReadLimit = numBytes
})
}
// WithCreds returns a ServerOption that sets credentials for server connections.
func WithCreds(privKey ed25519.PrivateKey, pubKeys []ed25519.PublicKey) ServerOption {
return newFuncServerOption(func(o *serverOptions) {
privKey, err := credentials.ValidPrivateKeyFromEd25519(privKey)
if err != nil {
return
}
pubs, err := credentials.ValidPublicKeysFromEd25519(pubKeys...)
if err != nil {
return
}
config, err := credentials.NewServerTLSConfig(privKey, pubs)
if err != nil {
return
}
o.creds = credentials.NewTLS(config, pubs)
})
}
// WriteBufferSize specifies the I/O write buffer size in bytes. If a buffer
// size is zero, then a useful default size is used.
func WriteBufferSize(s int) ServerOption {
return newFuncServerOption(func(o *serverOptions) {
o.writeBufferSize = s
})
}
// WriteBufferSize specifies the I/O read buffer size in bytes. If a buffer
// size is zero, then a useful default size is used.
func ReadBufferSize(s int) ServerOption {
return newFuncServerOption(func(o *serverOptions) {
o.readBufferSize = s
})
}
var defaultServerOptions = serverOptions{
writeBufferSize: 4096,
readBufferSize: 4096,
healthcheckTimeout: 5 * time.Second,
wsTimeout: 10 * time.Second,
wsReadLimit: int64(10_000_000),
}
// WithHealthcheck specifies whether to run a healthcheck endpoint. If a url
// is not provided, a healthcheck endpoint is not started.
func WithHealthcheck(addr string) ServerOption {
return newFuncServerOption(func(o *serverOptions) {
o.healthcheckAddr = addr
})
}