Skip to content

Commit 4366c45

Browse files
authored
les: make clientPool.connectedBias configurable (#21305)
1 parent 3a52c4d commit 4366c45

File tree

3 files changed

+28
-17
lines changed

3 files changed

+28
-17
lines changed

les/api.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,18 @@ func (api *PrivateLightServerAPI) SetDefaultParams(params map[string]interface{}
202202
return err
203203
}
204204

205+
// SetConnectedBias set the connection bias, which is applied to already connected clients
206+
// So that already connected client won't be kicked out very soon and we can ensure all
207+
// connected clients can have enough time to request or sync some data.
208+
// When the input parameter `bias` < 0 (illegal), return error.
209+
func (api *PrivateLightServerAPI) SetConnectedBias(bias time.Duration) error {
210+
if bias < time.Duration(0) {
211+
return fmt.Errorf("bias illegal: %v less than 0", bias)
212+
}
213+
api.server.clientPool.setConnectedBias(bias)
214+
return nil
215+
}
216+
205217
// Benchmark runs a request performance benchmark with a given set of measurement setups
206218
// in multiple passes specified by passCount. The measurement time for each setup in each
207219
// pass is specified in milliseconds by length.

les/clientpool.go

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -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.
376375
func (f *clientPool) dropClient(e *clientInfo, now mclock.AbsTime, kick bool) {

les/clientpool_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ func testClientPool(t *testing.T, connLimit, clientCount, paidCount int, randomD
9191
}
9292
pool = newClientPool(db, 1, &clock, disconnFn)
9393
)
94-
pool.disableBias = true
94+
pool.setConnectedBias(0)
9595
pool.setLimits(connLimit, uint64(connLimit))
9696
pool.setDefaultFactors(priceFactors{1, 0, 1}, priceFactors{1, 0, 1})
9797

@@ -248,7 +248,7 @@ func TestPaidClientKickedOut(t *testing.T) {
248248
clock.Run(time.Millisecond)
249249
}
250250
clock.Run(time.Second)
251-
clock.Run(connectedBias)
251+
clock.Run(defaultConnectedBias)
252252
if !pool.connect(poolTestPeer(11), 0) {
253253
t.Fatalf("Free client should be accectped")
254254
}

0 commit comments

Comments
 (0)