@@ -42,15 +42,7 @@ const (
4242 persistCumulativeTimeRefresh = time .Minute * 5 // refresh period of the cumulative running time persistence
4343 posBalanceCacheLimit = 8192 // the maximum number of cached items in positive balance queue
4444 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
5446)
5547
5648// clientPool implements a client database that assigns a priority to each client
@@ -94,7 +86,7 @@ type clientPool struct {
9486 freeClientCap uint64 // The capacity value of each free client
9587 startTime mclock.AbsTime // The timestamp at which the clientpool started running
9688 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)
9890}
9991
10092// clientPoolPeer represents a client peer in the pool.
@@ -171,6 +163,7 @@ func newClientPool(db ethdb.Database, freeClientCap uint64, clock mclock.Clock,
171163 startTime : clock .Now (),
172164 cumulativeTime : ndb .getCumulativeTime (),
173165 stopCh : make (chan struct {}),
166+ connectedBias : defaultConnectedBias ,
174167 }
175168 // If the negative balance of free client is even lower than 1,
176169 // delete this entry.
@@ -279,11 +272,7 @@ func (f *clientPool) connect(peer clientPoolPeer, capacity uint64) bool {
279272 newCount --
280273 return newCapacity > f .capLimit || newCount > f .connLimit
281274 })
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 {
287276 for _ , c := range kickList {
288277 f .connectedQueue .Push (c )
289278 }
@@ -371,6 +360,16 @@ func (f *clientPool) setDefaultFactors(posFactors, negFactors priceFactors) {
371360 f .defaultNegFactors = negFactors
372361}
373362
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+
374373// dropClient removes a client from the connected queue and finalizes its balance.
375374// If kick is true then it also initiates the disconnection.
376375func (f * clientPool ) dropClient (e * clientInfo , now mclock.AbsTime , kick bool ) {
0 commit comments