control.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. // Copyright 2016 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. // Package flowcontrol implements a client side flow control mechanism
  17. package flowcontrol
  18. import (
  19. "sync"
  20. "time"
  21. "github.com/ethereum/go-ethereum/common/mclock"
  22. )
  23. const fcTimeConst = time.Millisecond
  24. type ServerParams struct {
  25. BufLimit, MinRecharge uint64
  26. }
  27. type ClientNode struct {
  28. params *ServerParams
  29. bufValue uint64
  30. lastTime mclock.AbsTime
  31. lock sync.Mutex
  32. cm *ClientManager
  33. cmNode *cmNode
  34. }
  35. func NewClientNode(cm *ClientManager, params *ServerParams) *ClientNode {
  36. node := &ClientNode{
  37. cm: cm,
  38. params: params,
  39. bufValue: params.BufLimit,
  40. lastTime: mclock.Now(),
  41. }
  42. node.cmNode = cm.addNode(node)
  43. return node
  44. }
  45. func (peer *ClientNode) Remove(cm *ClientManager) {
  46. cm.removeNode(peer.cmNode)
  47. }
  48. func (peer *ClientNode) recalcBV(time mclock.AbsTime) {
  49. dt := uint64(time - peer.lastTime)
  50. if time < peer.lastTime {
  51. dt = 0
  52. }
  53. peer.bufValue += peer.params.MinRecharge * dt / uint64(fcTimeConst)
  54. if peer.bufValue > peer.params.BufLimit {
  55. peer.bufValue = peer.params.BufLimit
  56. }
  57. peer.lastTime = time
  58. }
  59. func (peer *ClientNode) AcceptRequest() (uint64, bool) {
  60. peer.lock.Lock()
  61. defer peer.lock.Unlock()
  62. time := mclock.Now()
  63. peer.recalcBV(time)
  64. return peer.bufValue, peer.cm.accept(peer.cmNode, time)
  65. }
  66. func (peer *ClientNode) RequestProcessed(cost uint64) (bv, realCost uint64) {
  67. peer.lock.Lock()
  68. defer peer.lock.Unlock()
  69. time := mclock.Now()
  70. peer.recalcBV(time)
  71. peer.bufValue -= cost
  72. peer.recalcBV(time)
  73. rcValue, rcost := peer.cm.processed(peer.cmNode, time)
  74. if rcValue < peer.params.BufLimit {
  75. bv := peer.params.BufLimit - rcValue
  76. if bv > peer.bufValue {
  77. peer.bufValue = bv
  78. }
  79. }
  80. return peer.bufValue, rcost
  81. }
  82. type ServerNode struct {
  83. bufEstimate uint64
  84. lastTime mclock.AbsTime
  85. params *ServerParams
  86. sumCost uint64 // sum of req costs sent to this server
  87. pending map[uint64]uint64 // value = sumCost after sending the given req
  88. lock sync.RWMutex
  89. }
  90. func NewServerNode(params *ServerParams) *ServerNode {
  91. return &ServerNode{
  92. bufEstimate: params.BufLimit,
  93. lastTime: mclock.Now(),
  94. params: params,
  95. pending: make(map[uint64]uint64),
  96. }
  97. }
  98. func (peer *ServerNode) recalcBLE(time mclock.AbsTime) {
  99. dt := uint64(time - peer.lastTime)
  100. if time < peer.lastTime {
  101. dt = 0
  102. }
  103. peer.bufEstimate += peer.params.MinRecharge * dt / uint64(fcTimeConst)
  104. if peer.bufEstimate > peer.params.BufLimit {
  105. peer.bufEstimate = peer.params.BufLimit
  106. }
  107. peer.lastTime = time
  108. }
  109. // safetyMargin is added to the flow control waiting time when estimated buffer value is low
  110. const safetyMargin = time.Millisecond
  111. func (peer *ServerNode) canSend(maxCost uint64) (time.Duration, float64) {
  112. peer.recalcBLE(mclock.Now())
  113. maxCost += uint64(safetyMargin) * peer.params.MinRecharge / uint64(fcTimeConst)
  114. if maxCost > peer.params.BufLimit {
  115. maxCost = peer.params.BufLimit
  116. }
  117. if peer.bufEstimate >= maxCost {
  118. return 0, float64(peer.bufEstimate-maxCost) / float64(peer.params.BufLimit)
  119. }
  120. return time.Duration((maxCost - peer.bufEstimate) * uint64(fcTimeConst) / peer.params.MinRecharge), 0
  121. }
  122. // CanSend returns the minimum waiting time required before sending a request
  123. // with the given maximum estimated cost. Second return value is the relative
  124. // estimated buffer level after sending the request (divided by BufLimit).
  125. func (peer *ServerNode) CanSend(maxCost uint64) (time.Duration, float64) {
  126. peer.lock.RLock()
  127. defer peer.lock.RUnlock()
  128. return peer.canSend(maxCost)
  129. }
  130. // QueueRequest should be called when the request has been assigned to the given
  131. // server node, before putting it in the send queue. It is mandatory that requests
  132. // are sent in the same order as the QueueRequest calls are made.
  133. func (peer *ServerNode) QueueRequest(reqID, maxCost uint64) {
  134. peer.lock.Lock()
  135. defer peer.lock.Unlock()
  136. peer.bufEstimate -= maxCost
  137. peer.sumCost += maxCost
  138. peer.pending[reqID] = peer.sumCost
  139. }
  140. // GotReply adjusts estimated buffer value according to the value included in
  141. // the latest request reply.
  142. func (peer *ServerNode) GotReply(reqID, bv uint64) {
  143. peer.lock.Lock()
  144. defer peer.lock.Unlock()
  145. if bv > peer.params.BufLimit {
  146. bv = peer.params.BufLimit
  147. }
  148. sc, ok := peer.pending[reqID]
  149. if !ok {
  150. return
  151. }
  152. delete(peer.pending, reqID)
  153. cc := peer.sumCost - sc
  154. peer.bufEstimate = 0
  155. if bv > cc {
  156. peer.bufEstimate = bv - cc
  157. }
  158. peer.lastTime = mclock.Now()
  159. }