les: don't bother cmManager if client request cost exceeds the limit

This commit is contained in:
rjl493456442 2018-07-29 19:57:45 +08:00
parent 93c0f1715d
commit e5f9c5e60f
3 changed files with 12 additions and 7 deletions

View file

@ -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) {

View file

@ -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) {

View file

@ -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