-
Notifications
You must be signed in to change notification settings - Fork 3
/
dialoptions.go
117 lines (98 loc) · 2.85 KB
/
dialoptions.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
package wsrpc
import (
"crypto/ed25519"
"time"
"github.com/smartcontractkit/wsrpc/credentials"
"github.com/smartcontractkit/wsrpc/internal/backoff"
"github.com/smartcontractkit/wsrpc/internal/transport"
"github.com/smartcontractkit/wsrpc/logger"
)
// dialOptions configure a Dial call. dialOptions are set by the DialOption
// values passed to Dial.
type dialOptions struct {
copts transport.ConnectOptions
bs backoff.Strategy
block bool
logger logger.Logger
}
// DialOption configures how we set up the connection.
type DialOption interface {
apply(*dialOptions) error
}
// funcDialOption wraps a function that modifies dialOptions into an
// implementation of the DialOption interface.
type funcDialOption struct {
f func(*dialOptions)
}
func (fdo *funcDialOption) apply(do *dialOptions) error {
fdo.f(do)
return nil
}
func newFuncDialOption(f func(*dialOptions)) *funcDialOption {
return &funcDialOption{
f: f,
}
}
type funcDialOptionWithErr struct {
funcWithErr func(*dialOptions) error
}
func (fdo *funcDialOptionWithErr) apply(do *dialOptions) error {
return fdo.funcWithErr(do)
}
func newFuncDialOptionWithErr(f func(*dialOptions) error) *funcDialOptionWithErr {
return &funcDialOptionWithErr{
funcWithErr: f,
}
}
// WithTransportCredentials returns a DialOption which configures a connection
// level security credentials (e.g., TLS/SSL).
func WithTransportCreds(privKey ed25519.PrivateKey, serverPubKey ed25519.PublicKey) DialOption {
return newFuncDialOptionWithErr(func(o *dialOptions) error {
privKey, err := credentials.ValidPrivateKeyFromEd25519(privKey)
if err != nil {
return err
}
pubs, err := credentials.ValidPublicKeysFromEd25519(serverPubKey)
if err != nil {
return err
}
// Generate the TLS config for the client
config, err := credentials.NewClientTLSConfig(privKey, pubs)
if err != nil {
return err
}
o.copts.TransportCredentials = credentials.NewTLS(config, pubs)
return nil
})
}
// WithBlock returns a DialOption which makes caller of Dial blocks until the
// underlying connection is up. Without this, Dial returns immediately and
// connecting the server happens in background.
func WithBlock() DialOption {
return newFuncDialOption(func(o *dialOptions) {
o.block = true
})
}
// WithWriteTimeout returns a DialOption which sets the write timeout for a
// message to be sent on the wire.
func WithWriteTimeout(d time.Duration) DialOption {
return newFuncDialOption(func(o *dialOptions) {
o.copts.WriteTimeout = d
})
}
func WithReadLimit(size int64) DialOption {
return newFuncDialOption(func(o *dialOptions) {
o.copts.ReadLimit = size
})
}
func WithLogger(lggr logger.Logger) DialOption {
return newFuncDialOption(func(o *dialOptions) {
o.logger = lggr
})
}
func defaultDialOptions() dialOptions {
return dialOptions{
copts: transport.ConnectOptions{},
logger: logger.DefaultLogger,
}
}