From e5f9c5e60fca68424ee3f3e9533c745c5c9aeb81 Mon Sep 17 00:00:00 2001 From: rjl493456442 Date: Sun, 29 Jul 2018 19:57:45 +0800 Subject: [PATCH] les: don't bother cmManager if client request cost exceeds the limit --- les/flowcontrol/control.go | 10 ++++++++-- les/flowcontrol/manager.go | 5 ++--- les/handler.go | 4 ++-- 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/les/flowcontrol/control.go b/les/flowcontrol/control.go index d50eb809cc..bcea5c3945 100644 --- a/les/flowcontrol/control.go +++ b/les/flowcontrol/control.go @@ -66,13 +66,19 @@ func (peer *ClientNode) recalcBV(time mclock.AbsTime) { peer.lastTime = time } -func (peer *ClientNode) AcceptRequest() (uint64, bool) { +func (peer *ClientNode) AcceptRequest(maxCost uint64) (uint64, bool) { peer.lock.Lock() defer peer.lock.Unlock() time := mclock.Now() peer.recalcBV(time) - return peer.bufValue, peer.cm.accept(peer.cmNode, time) + // Reject request directly if the client doesn't comply with the rate limit rules. + if peer.bufValue < maxCost { + return peer.bufValue, false + } + peer.cm.accept(peer.cmNode, time) + peer.recalcBV(time) + return peer.bufValue, true } func (peer *ClientNode) RequestProcessed(cost uint64) (bv, realCost uint64) { diff --git a/les/flowcontrol/manager.go b/les/flowcontrol/manager.go index 28cc6f0fe7..6a08786b20 100644 --- a/les/flowcontrol/manager.go +++ b/les/flowcontrol/manager.go @@ -184,7 +184,7 @@ func (self *ClientManager) queueProc() { } } -func (self *ClientManager) accept(node *cmNode, time mclock.AbsTime) bool { +func (self *ClientManager) accept(node *cmNode, time mclock.AbsTime) { self.lock.Lock() defer self.lock.Unlock() @@ -196,14 +196,13 @@ func (self *ClientManager) accept(node *cmNode, time mclock.AbsTime) bool { <-resume self.lock.Lock() if _, ok := self.nodes[node]; !ok { - return false // reject if node has been removed or manager has been stopped + panic("the node should never be removed during the request waiting") } } self.simReqCnt++ node.set(true, self.simReqCnt, self.sumWeight) node.startValue = node.rcValue self.update(self.time) - return true } func (self *ClientManager) stop(node *cmNode, time mclock.AbsTime) { diff --git a/les/handler.go b/les/handler.go index a1c16cb875..769b039b6c 100644 --- a/les/handler.go +++ b/les/handler.go @@ -350,12 +350,12 @@ func (pm *ProtocolManager) handleMsg(p *peer) error { if p.fcClient == nil || reqCnt > maxCnt { return true } - bufValue, _ := p.fcClient.AcceptRequest() cost := costs.baseCost + reqCnt*costs.reqCost if cost > pm.server.defParams.BufLimit { cost = pm.server.defParams.BufLimit } - if cost > bufValue { + bufValue, serve := p.fcClient.AcceptRequest(cost) + if !serve { recharge := time.Duration((cost - bufValue) * 1000000 / pm.server.defParams.MinRecharge) p.Log().Error("Request came too early", "recharge", common.PrettyDuration(recharge)) return true