This commit is contained in:
gary rong 2018-10-08 05:34:32 +00:00 committed by GitHub
commit 6f0aa907e4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
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 peer.lastTime = time
} }
func (peer *ClientNode) AcceptRequest() (uint64, bool) { func (peer *ClientNode) AcceptRequest(maxCost uint64) (uint64, bool) {
peer.lock.Lock() peer.lock.Lock()
defer peer.lock.Unlock() defer peer.lock.Unlock()
time := mclock.Now() time := mclock.Now()
peer.recalcBV(time) 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) { 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() self.lock.Lock()
defer self.lock.Unlock() defer self.lock.Unlock()
@ -196,14 +196,13 @@ func (self *ClientManager) accept(node *cmNode, time mclock.AbsTime) bool {
<-resume <-resume
self.lock.Lock() self.lock.Lock()
if _, ok := self.nodes[node]; !ok { 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++ self.simReqCnt++
node.set(true, self.simReqCnt, self.sumWeight) node.set(true, self.simReqCnt, self.sumWeight)
node.startValue = node.rcValue node.startValue = node.rcValue
self.update(self.time) self.update(self.time)
return true
} }
func (self *ClientManager) stop(node *cmNode, time mclock.AbsTime) { func (self *ClientManager) stop(node *cmNode, time mclock.AbsTime) {

View file

@ -341,12 +341,12 @@ func (pm *ProtocolManager) handleMsg(p *peer) error {
if p.fcClient == nil || reqCnt > maxCnt { if p.fcClient == nil || reqCnt > maxCnt {
return true return true
} }
bufValue, _ := p.fcClient.AcceptRequest()
cost := costs.baseCost + reqCnt*costs.reqCost cost := costs.baseCost + reqCnt*costs.reqCost
if cost > pm.server.defParams.BufLimit { if cost > pm.server.defParams.BufLimit {
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) recharge := time.Duration((cost - bufValue) * 1000000 / pm.server.defParams.MinRecharge)
p.Log().Error("Request came too early", "recharge", common.PrettyDuration(recharge)) p.Log().Error("Request came too early", "recharge", common.PrettyDuration(recharge))
return true return true