Skip to content

Commit

Permalink
les: only allow capacity change with priority status
Browse files Browse the repository at this point in the history
  • Loading branch information
zsfelfoldi committed Sep 14, 2020
1 parent 851adce commit b6b8dae
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions les/clientpool.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,12 @@ type clientPoolPeer interface {

// clientInfo defines all information required by clientpool.
type clientInfo struct {
node *enode.Node
address string
peer clientPoolPeer
connected bool
connectedAt mclock.AbsTime
balance *lps.NodeBalance
node *enode.Node
address string
peer clientPoolPeer
connected, priority bool
connectedAt mclock.AbsTime
balance *lps.NodeBalance
}

// newClientPool creates a new client pool
Expand Down Expand Up @@ -147,6 +147,11 @@ func newClientPool(lespayDb ethdb.Database, minCap uint64, connectedBias time.Du
})

ns.SubscribeState(pool.ActiveFlag.Or(pool.PriorityFlag), func(node *enode.Node, oldState, newState nodestate.Flags) {
c, _ := ns.GetField(node, clientField).(*clientInfo)
if c == nil {
return
}
c.priority = newState.HasAll(pool.PriorityFlag)
if newState.Equals(pool.ActiveFlag) {
cap, _ := ns.GetField(node, pool.CapacityField).(uint64)
if cap > minCap {
Expand Down Expand Up @@ -331,7 +336,10 @@ func (f *clientPool) setCapacity(node *enode.Node, freeID string, capacity uint6
allowed bool
)
f.ns.Operation(func() {
minPriority, allowed = f.pp.RequestCapacity(node, capacity, bias, setCap)
if !setCap || c.priority {
// check clientInfo.priority inside Operation to ensure thread safety
minPriority, allowed = f.pp.RequestCapacity(node, capacity, bias, setCap)
}
})
if allowed {
return 0, nil
Expand Down

0 comments on commit b6b8dae

Please sign in to comment.