@@ -42,15 +42,7 @@ const (
42
42
persistCumulativeTimeRefresh = time .Minute * 5 // refresh period of the cumulative running time persistence
43
43
posBalanceCacheLimit = 8192 // the maximum number of cached items in positive balance queue
44
44
negBalanceCacheLimit = 8192 // the maximum number of cached items in negative balance queue
45
-
46
- // connectedBias is applied to already connected clients So that
47
- // already connected client won't be kicked out very soon and we
48
- // can ensure all connected clients can have enough time to request
49
- // or sync some data.
50
- //
51
- // todo(rjl493456442) make it configurable. It can be the option of
52
- // free trial time!
53
- connectedBias = time .Minute * 3
45
+ defaultConnectedBias = time .Minute * 3 // the default connectedBias used in clientPool
54
46
)
55
47
56
48
// clientPool implements a client database that assigns a priority to each client
@@ -94,7 +86,7 @@ type clientPool struct {
94
86
freeClientCap uint64 // The capacity value of each free client
95
87
startTime mclock.AbsTime // The timestamp at which the clientpool started running
96
88
cumulativeTime int64 // The cumulative running time of clientpool at the start point.
97
- disableBias bool // Disable connection bias(used in testing)
89
+ connectedBias time. Duration // The connection bias. 0: Disable connection bias(used in testing)
98
90
}
99
91
100
92
// clientPoolPeer represents a client peer in the pool.
@@ -171,6 +163,7 @@ func newClientPool(db ethdb.Database, freeClientCap uint64, clock mclock.Clock,
171
163
startTime : clock .Now (),
172
164
cumulativeTime : ndb .getCumulativeTime (),
173
165
stopCh : make (chan struct {}),
166
+ connectedBias : defaultConnectedBias ,
174
167
}
175
168
// If the negative balance of free client is even lower than 1,
176
169
// delete this entry.
@@ -279,11 +272,7 @@ func (f *clientPool) connect(peer clientPoolPeer, capacity uint64) bool {
279
272
newCount --
280
273
return newCapacity > f .capLimit || newCount > f .connLimit
281
274
})
282
- bias := connectedBias
283
- if f .disableBias {
284
- bias = 0
285
- }
286
- if newCapacity > f .capLimit || newCount > f .connLimit || (e .balanceTracker .estimatedPriority (now + mclock .AbsTime (bias ), false )- kickPriority ) > 0 {
275
+ if newCapacity > f .capLimit || newCount > f .connLimit || (e .balanceTracker .estimatedPriority (now + mclock .AbsTime (f .connectedBias ), false )- kickPriority ) > 0 {
287
276
for _ , c := range kickList {
288
277
f .connectedQueue .Push (c )
289
278
}
@@ -371,6 +360,16 @@ func (f *clientPool) setDefaultFactors(posFactors, negFactors priceFactors) {
371
360
f .defaultNegFactors = negFactors
372
361
}
373
362
363
+ // setConnectedBias sets the connection bias, which is applied to already connected clients
364
+ // So that already connected client won't be kicked out very soon and we can ensure all
365
+ // connected clients can have enough time to request or sync some data.
366
+ func (f * clientPool ) setConnectedBias (bias time.Duration ) {
367
+ f .lock .Lock ()
368
+ defer f .lock .Unlock ()
369
+
370
+ f .connectedBias = bias
371
+ }
372
+
374
373
// dropClient removes a client from the connected queue and finalizes its balance.
375
374
// If kick is true then it also initiates the disconnection.
376
375
func (f * clientPool ) dropClient (e * clientInfo , now mclock.AbsTime , kick bool ) {
0 commit comments