-
Notifications
You must be signed in to change notification settings - Fork 4
/
config.go
201 lines (175 loc) · 4.79 KB
/
config.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package network
import (
"fmt"
"reflect"
"time"
"github.com/libp2p/go-libp2p-core/connmgr"
"github.com/libp2p/go-libp2p-core/crypto"
"github.com/libp2p/go-libp2p-core/network"
"github.com/libp2p/go-libp2p-core/protocol"
"github.com/libp2p/go-libp2p-core/sec"
"github.com/meshplus/bitxhub-kit/log"
"github.com/sirupsen/logrus"
)
type connMgr struct {
enabled bool
lo int
hi int
grace time.Duration
}
var (
defaultConnectTimeout = 10 * time.Second
defaultSendTimeout = 2 * time.Second
defaultWaitTimeout = 5 * time.Second
defaultReusableProtocolIndex = 0
defaultNonReusableProtocolIndex = 1
)
type timeout struct {
connectTimeout time.Duration
sendTimeout time.Duration
waitTimeout time.Duration
}
func defaultTimeout() *timeout {
return &timeout{
connectTimeout: defaultConnectTimeout,
sendTimeout: defaultSendTimeout,
waitTimeout: defaultWaitTimeout,
}
}
type Config struct {
localAddr string
privKey crypto.PrivKey
protocolIDs []protocol.ID
logger logrus.FieldLogger
bootstrap []string
connMgr *connMgr
notify network.Notifiee
gater connmgr.ConnectionGater
transport sec.SecureTransport
transportID string
reusableProtocolIndex int
nonReusableProtocolIndex int
timeout *timeout
}
type Option func(*Config)
func WithTransportId(tid string) Option {
return func(config *Config) {
config.transportID = tid
}
}
func WithTransport(tpt sec.SecureTransport) Option {
return func(config *Config) {
config.transport = tpt
}
}
func WithPrivateKey(privKey crypto.PrivKey) Option {
return func(config *Config) {
config.privKey = privKey
}
}
func WithLocalAddr(addr string) Option {
return func(config *Config) {
config.localAddr = addr
}
}
func WithProtocolIDs(ids []string) Option {
return func(config *Config) {
config.protocolIDs = []protocol.ID{}
for _, id := range ids {
config.protocolIDs = append(config.protocolIDs, protocol.ID(id))
}
}
}
func WithBootstrap(peers []string) Option {
return func(config *Config) {
config.bootstrap = peers
}
}
func WithNotify(notify network.Notifiee) Option {
return func(config *Config) {
config.notify = notify
}
}
func WithConnectionGater(gater connmgr.ConnectionGater) Option {
return func(config *Config) {
config.gater = gater
}
}
// WithConnMgr * enable is the enable signal of the connection manager module.
// - lo and hi are watermarks governing the number of connections that'll be maintained.
// When the peer count exceeds the 'high watermark', as many peers will be pruned (and
// their connections terminated) until 'low watermark' peers remain.
// - grace is the amount of time a newly opened connection is given before it becomes
// subject to pruning.
func WithConnMgr(enable bool, lo int, hi int, grace time.Duration) Option {
return func(config *Config) {
config.connMgr = &connMgr{
enabled: enable,
lo: lo,
hi: hi,
grace: grace,
}
}
}
func WithLogger(logger logrus.FieldLogger) Option {
return func(config *Config) {
config.logger = logger
}
}
func WithConnectTimeout(time time.Duration) Option {
return func(config *Config) {
config.timeout.connectTimeout = time
}
}
func WithSendTimeout(time time.Duration) Option {
return func(config *Config) {
config.timeout.sendTimeout = time
}
}
func WithWaitTimeout(time time.Duration) Option {
return func(config *Config) {
config.timeout.waitTimeout = time
}
}
func WithReusableProtocolIndex(reusableProtocolIndex int) Option {
return func(config *Config) {
config.reusableProtocolIndex = reusableProtocolIndex
}
}
func WithNonReusableProtocolIndex(nonReusableProtocolIndex int) Option {
return func(config *Config) {
config.nonReusableProtocolIndex = nonReusableProtocolIndex
}
}
func checkConfig(config *Config) error {
if reflect.ValueOf(config.logger).IsZero() {
config.logger = log.NewWithModule("p2p")
}
if config.localAddr == "" {
return fmt.Errorf("empty local address")
}
if config.timeout.sendTimeout < 0 {
config.timeout.sendTimeout = defaultSendTimeout
}
if config.timeout.waitTimeout < 0 {
config.timeout.waitTimeout = defaultWaitTimeout
}
if config.timeout.connectTimeout < 0 {
config.timeout.connectTimeout = defaultConnectTimeout
}
return nil
}
func generateConfig(opts ...Option) (*Config, error) {
conf := &Config{}
timeout := defaultTimeout()
conf.timeout = timeout
conf.reusableProtocolIndex = defaultReusableProtocolIndex
conf.nonReusableProtocolIndex = defaultNonReusableProtocolIndex
for _, opt := range opts {
opt(conf)
}
if err := checkConfig(conf); err != nil {
return nil, fmt.Errorf("create p2p: %w", err)
}
return conf, nil
}