@@ -27,18 +27,21 @@ import (
27
27
"github.com/ava-labs/gecko/version"
28
28
)
29
29
30
+ // reasonable default values
30
31
const (
31
- defaultInitialReconnectDelay = time .Second
32
- defaultMaxReconnectDelay = time .Hour
33
- DefaultMaxMessageSize uint32 = 1 << 21
34
- defaultSendQueueSize = 1 << 10
35
- defaultMaxClockDifference = time .Minute
36
- defaultPeerListGossipSpacing = time .Minute
37
- defaultPeerListGossipSize = 100
38
- defaultPeerListStakerGossipFraction = 2
39
- defaultGetVersionTimeout = 2 * time .Second
40
- defaultAllowPrivateIPs = true
41
- defaultGossipSize = 50
32
+ defaultInitialReconnectDelay = time .Second
33
+ defaultMaxReconnectDelay = time .Hour
34
+ DefaultMaxMessageSize uint32 = 1 << 21
35
+ defaultSendQueueSize = 1 << 10
36
+ defaultMaxNetworkPendingSendBytes = 1 << 29 // 512MB
37
+ defaultNetworkPendingSendBytesToRateLimit = defaultMaxNetworkPendingSendBytes / 4
38
+ defaultMaxClockDifference = time .Minute
39
+ defaultPeerListGossipSpacing = time .Minute
40
+ defaultPeerListGossipSize = 100
41
+ defaultPeerListStakerGossipFraction = 2
42
+ defaultGetVersionTimeout = 2 * time .Second
43
+ defaultAllowPrivateIPs = true
44
+ defaultGossipSize = 50
42
45
)
43
46
44
47
// Network defines the functionality of the networking library.
@@ -102,23 +105,26 @@ type network struct {
102
105
clock timer.Clock
103
106
lastHeartbeat int64
104
107
105
- initialReconnectDelay time.Duration
106
- maxReconnectDelay time.Duration
107
- maxMessageSize uint32
108
- sendQueueSize int
109
- maxClockDifference time.Duration
110
- peerListGossipSpacing time.Duration
111
- peerListGossipSize int
112
- peerListStakerGossipFraction int
113
- getVersionTimeout time.Duration
114
- allowPrivateIPs bool
115
- gossipSize int
108
+ initialReconnectDelay time.Duration
109
+ maxReconnectDelay time.Duration
110
+ maxMessageSize uint32
111
+ sendQueueSize int
112
+ maxNetworkPendingSendBytes int
113
+ networkPendingSendBytesToRateLimit int
114
+ maxClockDifference time.Duration
115
+ peerListGossipSpacing time.Duration
116
+ peerListGossipSize int
117
+ peerListStakerGossipFraction int
118
+ getVersionTimeout time.Duration
119
+ allowPrivateIPs bool
120
+ gossipSize int
116
121
117
122
executor timer.Executor
118
123
119
124
b Builder
120
125
121
126
stateLock sync.Mutex
127
+ pendingBytes int
122
128
closed bool
123
129
disconnectedIPs map [string ]struct {}
124
130
connectedIPs map [string ]struct {}
@@ -164,6 +170,8 @@ func NewDefaultNetwork(
164
170
defaultMaxReconnectDelay ,
165
171
DefaultMaxMessageSize ,
166
172
defaultSendQueueSize ,
173
+ defaultMaxNetworkPendingSendBytes ,
174
+ defaultNetworkPendingSendBytesToRateLimit ,
167
175
defaultMaxClockDifference ,
168
176
defaultPeerListGossipSpacing ,
169
177
defaultPeerListGossipSize ,
@@ -193,6 +201,8 @@ func NewNetwork(
193
201
maxReconnectDelay time.Duration ,
194
202
maxMessageSize uint32 ,
195
203
sendQueueSize int ,
204
+ maxNetworkPendingSendBytes int ,
205
+ networkPendingSendBytesToRateLimit int ,
196
206
maxClockDifference time.Duration ,
197
207
peerListGossipSpacing time.Duration ,
198
208
peerListGossipSize int ,
@@ -202,35 +212,37 @@ func NewNetwork(
202
212
gossipSize int ,
203
213
) Network {
204
214
net := & network {
205
- log : log ,
206
- id : id ,
207
- ip : ip ,
208
- networkID : networkID ,
209
- version : version ,
210
- parser : parser ,
211
- listener : listener ,
212
- dialer : dialer ,
213
- serverUpgrader : serverUpgrader ,
214
- clientUpgrader : clientUpgrader ,
215
- vdrs : vdrs ,
216
- router : router ,
217
- nodeID : rand .Uint32 (),
218
- initialReconnectDelay : initialReconnectDelay ,
219
- maxReconnectDelay : maxReconnectDelay ,
220
- maxMessageSize : maxMessageSize ,
221
- sendQueueSize : sendQueueSize ,
222
- maxClockDifference : maxClockDifference ,
223
- peerListGossipSpacing : peerListGossipSpacing ,
224
- peerListGossipSize : peerListGossipSize ,
225
- peerListStakerGossipFraction : peerListStakerGossipFraction ,
226
- getVersionTimeout : getVersionTimeout ,
227
- allowPrivateIPs : allowPrivateIPs ,
228
- gossipSize : gossipSize ,
215
+ log : log ,
216
+ id : id ,
217
+ ip : ip ,
218
+ networkID : networkID ,
219
+ version : version ,
220
+ parser : parser ,
221
+ listener : listener ,
222
+ dialer : dialer ,
223
+ serverUpgrader : serverUpgrader ,
224
+ clientUpgrader : clientUpgrader ,
225
+ vdrs : vdrs ,
226
+ router : router ,
227
+ nodeID : rand .Uint32 (),
228
+ initialReconnectDelay : initialReconnectDelay ,
229
+ maxReconnectDelay : maxReconnectDelay ,
230
+ maxMessageSize : maxMessageSize ,
231
+ sendQueueSize : sendQueueSize ,
232
+ maxNetworkPendingSendBytes : maxNetworkPendingSendBytes ,
233
+ networkPendingSendBytesToRateLimit : networkPendingSendBytesToRateLimit ,
234
+ maxClockDifference : maxClockDifference ,
235
+ peerListGossipSpacing : peerListGossipSpacing ,
236
+ peerListGossipSize : peerListGossipSize ,
237
+ peerListStakerGossipFraction : peerListStakerGossipFraction ,
238
+ getVersionTimeout : getVersionTimeout ,
239
+ allowPrivateIPs : allowPrivateIPs ,
240
+ gossipSize : gossipSize ,
229
241
230
242
disconnectedIPs : make (map [string ]struct {}),
231
243
connectedIPs : make (map [string ]struct {}),
232
244
retryDelay : make (map [string ]time.Duration ),
233
- myIPs : map [string ]struct {}{ip .String (): struct {} {}},
245
+ myIPs : map [string ]struct {}{ip .String (): {}},
234
246
peers : make (map [[20 ]byte ]* peer ),
235
247
}
236
248
net .initialize (registerer )
@@ -738,11 +750,12 @@ func (n *network) connectTo(ip utils.IPDesc) {
738
750
739
751
if delay == 0 {
740
752
delay = n .initialReconnectDelay
741
- } else {
742
- delay *= 2
743
753
}
754
+
755
+ delay = time .Duration (float64 (delay ) * (1 + rand .Float64 ()))
744
756
if delay > n .maxReconnectDelay {
745
- delay = n .maxReconnectDelay
757
+ // set the timeout to [.75, 1) * maxReconnectDelay
758
+ delay = time .Duration (float64 (n .maxReconnectDelay ) * (3 + rand .Float64 ()) / 4 )
746
759
}
747
760
748
761
n .stateLock .Lock ()
0 commit comments