control.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. assignedRequest uint64 // when != 0, only the request with the given ID can be sent to this peer
  89. assignToken chan struct{} // send to this channel before assigning, read from it after deassigning
  90. lock sync.RWMutex
  91. }
  92. func NewServerNode(params *ServerParams) *ServerNode {
  93. return &ServerNode{
  94. bufEstimate: params.BufLimit,
  95. lastTime: mclock.Now(),
  96. params: params,
  97. pending: make(map[uint64]uint64),
  98. assignToken: make(chan struct{}, 1),
  99. }
  100. }
  101. func (peer *ServerNode) recalcBLE(time mclock.AbsTime) {
  102. dt := uint64(time - peer.lastTime)
  103. if time < peer.lastTime {
  104. dt = 0
  105. }
  106. peer.bufEstimate += peer.params.MinRecharge * dt / uint64(fcTimeConst)
  107. if peer.bufEstimate > peer.params.BufLimit {
  108. peer.bufEstimate = peer.params.BufLimit
  109. }
  110. peer.lastTime = time
  111. }
  112. // safetyMargin is added to the flow control waiting time when estimated buffer value is low
  113. const safetyMargin = time.Millisecond * 200
  114. func (peer *ServerNode) canSend(maxCost uint64) time.Duration {
  115. maxCost += uint64(safetyMargin) * peer.params.MinRecharge / uint64(fcTimeConst)
  116. if maxCost > peer.params.BufLimit {
  117. maxCost = peer.params.BufLimit
  118. }
  119. if peer.bufEstimate >= maxCost {
  120. return 0
  121. }
  122. return time.Duration((maxCost - peer.bufEstimate) * uint64(fcTimeConst) / peer.params.MinRecharge)
  123. }
  124. // CanSend returns the minimum waiting time required before sending a request
  125. // with the given maximum estimated cost
  126. func (peer *ServerNode) CanSend(maxCost uint64) time.Duration {
  127. peer.lock.RLock()
  128. defer peer.lock.RUnlock()
  129. return peer.canSend(maxCost)
  130. }
  131. // AssignRequest tries to assign the server node to the given request, guaranteeing
  132. // that once it returns true, no request will be sent to the node before this one
  133. func (peer *ServerNode) AssignRequest(reqID uint64) bool {
  134. select {
  135. case peer.assignToken <- struct{}{}:
  136. default:
  137. return false
  138. }
  139. peer.lock.Lock()
  140. peer.assignedRequest = reqID
  141. peer.lock.Unlock()
  142. return true
  143. }
  144. // MustAssignRequest waits until the node can be assigned to the given request.
  145. // It is always guaranteed that assignments are released in a short amount of time.
  146. func (peer *ServerNode) MustAssignRequest(reqID uint64) {
  147. peer.assignToken <- struct{}{}
  148. peer.lock.Lock()
  149. peer.assignedRequest = reqID
  150. peer.lock.Unlock()
  151. }
  152. // DeassignRequest releases a request assignment in case the planned request
  153. // is not being sent.
  154. func (peer *ServerNode) DeassignRequest(reqID uint64) {
  155. peer.lock.Lock()
  156. if peer.assignedRequest == reqID {
  157. peer.assignedRequest = 0
  158. <-peer.assignToken
  159. }
  160. peer.lock.Unlock()
  161. }
  162. // IsAssigned returns true if the server node has already been assigned to a request
  163. // (note that this function returning false does not guarantee that you can assign a request
  164. // immediately afterwards, its only purpose is to help peer selection)
  165. func (peer *ServerNode) IsAssigned() bool {
  166. peer.lock.RLock()
  167. locked := peer.assignedRequest != 0
  168. peer.lock.RUnlock()
  169. return locked
  170. }
  171. // blocks until request can be sent
  172. func (peer *ServerNode) SendRequest(reqID, maxCost uint64) {
  173. peer.lock.Lock()
  174. defer peer.lock.Unlock()
  175. if peer.assignedRequest != reqID {
  176. peer.lock.Unlock()
  177. peer.MustAssignRequest(reqID)
  178. peer.lock.Lock()
  179. }
  180. peer.recalcBLE(mclock.Now())
  181. wait := peer.canSend(maxCost)
  182. for wait > 0 {
  183. peer.lock.Unlock()
  184. time.Sleep(wait)
  185. peer.lock.Lock()
  186. peer.recalcBLE(mclock.Now())
  187. wait = peer.canSend(maxCost)
  188. }
  189. peer.assignedRequest = 0
  190. <-peer.assignToken
  191. peer.bufEstimate -= maxCost
  192. peer.sumCost += maxCost
  193. if reqID >= 0 {
  194. peer.pending[reqID] = peer.sumCost
  195. }
  196. }
  197. func (peer *ServerNode) GotReply(reqID, bv uint64) {
  198. peer.lock.Lock()
  199. defer peer.lock.Unlock()
  200. if bv > peer.params.BufLimit {
  201. bv = peer.params.BufLimit
  202. }
  203. sc, ok := peer.pending[reqID]
  204. if !ok {
  205. return
  206. }
  207. delete(peer.pending, reqID)
  208. peer.bufEstimate = bv - (peer.sumCost - sc)
  209. peer.lastTime = mclock.Now()
  210. }