control.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 = 1000000
  24. type ServerParams struct {
  25. BufLimit, MinRecharge uint64
  26. }
  27. type ClientNode struct {
  28. params *ServerParams
  29. bufValue uint64
  30. lastTime int64
  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: getTime(),
  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 int64) {
  49. dt := uint64(time - peer.lastTime)
  50. if time < peer.lastTime {
  51. dt = 0
  52. }
  53. peer.bufValue += peer.params.MinRecharge * dt / 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 := getTime()
  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 := getTime()
  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 int64
  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: getTime(),
  94. params: params,
  95. pending: make(map[uint64]uint64),
  96. }
  97. }
  98. func getTime() int64 {
  99. return int64(mclock.Now())
  100. }
  101. func (peer *ServerNode) recalcBLE(time int64) {
  102. dt := uint64(time - peer.lastTime)
  103. if time < peer.lastTime {
  104. dt = 0
  105. }
  106. peer.bufEstimate += peer.params.MinRecharge * dt / fcTimeConst
  107. if peer.bufEstimate > peer.params.BufLimit {
  108. peer.bufEstimate = peer.params.BufLimit
  109. }
  110. peer.lastTime = time
  111. }
  112. func (peer *ServerNode) canSend(maxCost uint64) uint64 {
  113. if peer.bufEstimate >= maxCost {
  114. return 0
  115. }
  116. return (maxCost - peer.bufEstimate) * fcTimeConst / peer.params.MinRecharge
  117. }
  118. func (peer *ServerNode) CanSend(maxCost uint64) uint64 {
  119. peer.lock.RLock()
  120. defer peer.lock.RUnlock()
  121. return peer.canSend(maxCost)
  122. }
  123. // blocks until request can be sent
  124. func (peer *ServerNode) SendRequest(reqID, maxCost uint64) {
  125. peer.lock.Lock()
  126. defer peer.lock.Unlock()
  127. peer.recalcBLE(getTime())
  128. for peer.bufEstimate < maxCost {
  129. wait := time.Duration(peer.canSend(maxCost))
  130. peer.lock.Unlock()
  131. time.Sleep(wait)
  132. peer.lock.Lock()
  133. peer.recalcBLE(getTime())
  134. }
  135. peer.bufEstimate -= maxCost
  136. peer.sumCost += maxCost
  137. if reqID >= 0 {
  138. peer.pending[reqID] = peer.sumCost
  139. }
  140. }
  141. func (peer *ServerNode) GotReply(reqID, bv uint64) {
  142. peer.lock.Lock()
  143. defer peer.lock.Unlock()
  144. sc, ok := peer.pending[reqID]
  145. if !ok {
  146. return
  147. }
  148. delete(peer.pending, reqID)
  149. peer.bufEstimate = bv - (peer.sumCost - sc)
  150. peer.lastTime = getTime()
  151. }