mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-19 18:32:23 +00:00
Merge e5f9c5e60f into f95811e65b
This commit is contained in:
commit
6f0aa907e4
3 changed files with 12 additions and 7 deletions
|
|
@ -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) {
|
||||||
|
|
|
||||||
|
|
@ -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) {
|
||||||
|
|
|
||||||
|
|
@ -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
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue