control.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /*
  2. *
  3. * Copyright 2014, Google Inc.
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are
  8. * met:
  9. *
  10. * * Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * * Redistributions in binary form must reproduce the above
  13. * copyright notice, this list of conditions and the following disclaimer
  14. * in the documentation and/or other materials provided with the
  15. * distribution.
  16. * * Neither the name of Google Inc. nor the names of its
  17. * contributors may be used to endorse or promote products derived from
  18. * this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. *
  32. */
  33. package transport
  34. import (
  35. "fmt"
  36. "sync"
  37. "golang.org/x/net/http2"
  38. )
  39. const (
  40. // The default value of flow control window size in HTTP2 spec.
  41. defaultWindowSize = 65535
  42. // The initial window size for flow control.
  43. initialWindowSize = defaultWindowSize // for an RPC
  44. initialConnWindowSize = defaultWindowSize * 16 // for a connection
  45. )
  46. // The following defines various control items which could flow through
  47. // the control buffer of transport. They represent different aspects of
  48. // control tasks, e.g., flow control, settings, streaming resetting, etc.
  49. type windowUpdate struct {
  50. streamID uint32
  51. increment uint32
  52. }
  53. func (*windowUpdate) item() {}
  54. type settings struct {
  55. ack bool
  56. ss []http2.Setting
  57. }
  58. func (*settings) item() {}
  59. type resetStream struct {
  60. streamID uint32
  61. code http2.ErrCode
  62. }
  63. func (*resetStream) item() {}
  64. type flushIO struct {
  65. }
  66. func (*flushIO) item() {}
  67. type ping struct {
  68. ack bool
  69. data [8]byte
  70. }
  71. func (*ping) item() {}
  72. // quotaPool is a pool which accumulates the quota and sends it to acquire()
  73. // when it is available.
  74. type quotaPool struct {
  75. c chan int
  76. mu sync.Mutex
  77. quota int
  78. }
  79. // newQuotaPool creates a quotaPool which has quota q available to consume.
  80. func newQuotaPool(q int) *quotaPool {
  81. qb := &quotaPool{
  82. c: make(chan int, 1),
  83. }
  84. if q > 0 {
  85. qb.c <- q
  86. } else {
  87. qb.quota = q
  88. }
  89. return qb
  90. }
  91. // add adds n to the available quota and tries to send it on acquire.
  92. func (qb *quotaPool) add(n int) {
  93. qb.mu.Lock()
  94. defer qb.mu.Unlock()
  95. qb.quota += n
  96. if qb.quota <= 0 {
  97. return
  98. }
  99. select {
  100. case qb.c <- qb.quota:
  101. qb.quota = 0
  102. default:
  103. }
  104. }
  105. // cancel cancels the pending quota sent on acquire, if any.
  106. func (qb *quotaPool) cancel() {
  107. qb.mu.Lock()
  108. defer qb.mu.Unlock()
  109. select {
  110. case n := <-qb.c:
  111. qb.quota += n
  112. default:
  113. }
  114. }
  115. // reset cancels the pending quota sent on acquired, incremented by v and sends
  116. // it back on acquire.
  117. func (qb *quotaPool) reset(v int) {
  118. qb.mu.Lock()
  119. defer qb.mu.Unlock()
  120. select {
  121. case n := <-qb.c:
  122. qb.quota += n
  123. default:
  124. }
  125. qb.quota += v
  126. if qb.quota <= 0 {
  127. return
  128. }
  129. select {
  130. case qb.c <- qb.quota:
  131. qb.quota = 0
  132. default:
  133. }
  134. }
  135. // acquire returns the channel on which available quota amounts are sent.
  136. func (qb *quotaPool) acquire() <-chan int {
  137. return qb.c
  138. }
  139. // inFlow deals with inbound flow control
  140. type inFlow struct {
  141. // The inbound flow control limit for pending data.
  142. limit uint32
  143. // conn points to the shared connection-level inFlow that is shared
  144. // by all streams on that conn. It is nil for the inFlow on the conn
  145. // directly.
  146. conn *inFlow
  147. mu sync.Mutex
  148. // pendingData is the overall data which have been received but not been
  149. // consumed by applications.
  150. pendingData uint32
  151. // The amount of data the application has consumed but grpc has not sent
  152. // window update for them. Used to reduce window update frequency.
  153. pendingUpdate uint32
  154. }
  155. // onData is invoked when some data frame is received. It increments not only its
  156. // own pendingData but also that of the associated connection-level flow.
  157. func (f *inFlow) onData(n uint32) error {
  158. if n == 0 {
  159. return nil
  160. }
  161. f.mu.Lock()
  162. defer f.mu.Unlock()
  163. if f.pendingData+f.pendingUpdate+n > f.limit {
  164. return fmt.Errorf("received %d-bytes data exceeding the limit %d bytes", f.pendingData+f.pendingUpdate+n, f.limit)
  165. }
  166. if f.conn != nil {
  167. if err := f.conn.onData(n); err != nil {
  168. return ConnectionErrorf("%v", err)
  169. }
  170. }
  171. f.pendingData += n
  172. return nil
  173. }
  174. // connOnRead updates the connection level states when the application consumes data.
  175. func (f *inFlow) connOnRead(n uint32) uint32 {
  176. if n == 0 || f.conn != nil {
  177. return 0
  178. }
  179. f.mu.Lock()
  180. defer f.mu.Unlock()
  181. f.pendingData -= n
  182. f.pendingUpdate += n
  183. if f.pendingUpdate >= f.limit/4 {
  184. ret := f.pendingUpdate
  185. f.pendingUpdate = 0
  186. return ret
  187. }
  188. return 0
  189. }
  190. // onRead is invoked when the application reads the data. It returns the window updates
  191. // for both stream and connection level.
  192. func (f *inFlow) onRead(n uint32) (swu, cwu uint32) {
  193. if n == 0 {
  194. return
  195. }
  196. f.mu.Lock()
  197. defer f.mu.Unlock()
  198. if f.pendingData == 0 {
  199. // pendingData has been adjusted by restoreConn.
  200. return
  201. }
  202. f.pendingData -= n
  203. f.pendingUpdate += n
  204. if f.pendingUpdate >= f.limit/4 {
  205. swu = f.pendingUpdate
  206. f.pendingUpdate = 0
  207. }
  208. cwu = f.conn.connOnRead(n)
  209. return
  210. }
  211. // restoreConn is invoked when a stream is terminated. It removes its stake in
  212. // the connection-level flow and resets its own state.
  213. func (f *inFlow) restoreConn() uint32 {
  214. if f.conn == nil {
  215. return 0
  216. }
  217. f.mu.Lock()
  218. defer f.mu.Unlock()
  219. n := f.pendingData
  220. f.pendingData = 0
  221. f.pendingUpdate = 0
  222. return f.conn.connOnRead(n)
  223. }