control.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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 goAway struct {
  65. }
  66. func (*goAway) item() {}
  67. type flushIO struct {
  68. }
  69. func (*flushIO) item() {}
  70. type ping struct {
  71. ack bool
  72. data [8]byte
  73. }
  74. func (*ping) item() {}
  75. // quotaPool is a pool which accumulates the quota and sends it to acquire()
  76. // when it is available.
  77. type quotaPool struct {
  78. c chan int
  79. mu sync.Mutex
  80. quota int
  81. }
  82. // newQuotaPool creates a quotaPool which has quota q available to consume.
  83. func newQuotaPool(q int) *quotaPool {
  84. qb := &quotaPool{
  85. c: make(chan int, 1),
  86. }
  87. if q > 0 {
  88. qb.c <- q
  89. } else {
  90. qb.quota = q
  91. }
  92. return qb
  93. }
  94. // add adds n to the available quota and tries to send it on acquire.
  95. func (qb *quotaPool) add(n int) {
  96. qb.mu.Lock()
  97. defer qb.mu.Unlock()
  98. qb.quota += n
  99. if qb.quota <= 0 {
  100. return
  101. }
  102. select {
  103. case qb.c <- qb.quota:
  104. qb.quota = 0
  105. default:
  106. }
  107. }
  108. // cancel cancels the pending quota sent on acquire, if any.
  109. func (qb *quotaPool) cancel() {
  110. qb.mu.Lock()
  111. defer qb.mu.Unlock()
  112. select {
  113. case n := <-qb.c:
  114. qb.quota += n
  115. default:
  116. }
  117. }
  118. // reset cancels the pending quota sent on acquired, incremented by v and sends
  119. // it back on acquire.
  120. func (qb *quotaPool) reset(v int) {
  121. qb.mu.Lock()
  122. defer qb.mu.Unlock()
  123. select {
  124. case n := <-qb.c:
  125. qb.quota += n
  126. default:
  127. }
  128. qb.quota += v
  129. if qb.quota <= 0 {
  130. return
  131. }
  132. select {
  133. case qb.c <- qb.quota:
  134. qb.quota = 0
  135. default:
  136. }
  137. }
  138. // acquire returns the channel on which available quota amounts are sent.
  139. func (qb *quotaPool) acquire() <-chan int {
  140. return qb.c
  141. }
  142. // inFlow deals with inbound flow control
  143. type inFlow struct {
  144. // The inbound flow control limit for pending data.
  145. limit uint32
  146. mu sync.Mutex
  147. // pendingData is the overall data which have been received but not been
  148. // consumed by applications.
  149. pendingData uint32
  150. // The amount of data the application has consumed but grpc has not sent
  151. // window update for them. Used to reduce window update frequency.
  152. pendingUpdate uint32
  153. }
  154. // onData is invoked when some data frame is received. It updates pendingData.
  155. func (f *inFlow) onData(n uint32) error {
  156. f.mu.Lock()
  157. defer f.mu.Unlock()
  158. f.pendingData += n
  159. if f.pendingData+f.pendingUpdate > f.limit {
  160. return fmt.Errorf("received %d-bytes data exceeding the limit %d bytes", f.pendingData+f.pendingUpdate, f.limit)
  161. }
  162. return nil
  163. }
  164. // onRead is invoked when the application reads the data. It returns the window size
  165. // to be sent to the peer.
  166. func (f *inFlow) onRead(n uint32) uint32 {
  167. f.mu.Lock()
  168. defer f.mu.Unlock()
  169. if f.pendingData == 0 {
  170. return 0
  171. }
  172. f.pendingData -= n
  173. f.pendingUpdate += n
  174. if f.pendingUpdate >= f.limit/4 {
  175. wu := f.pendingUpdate
  176. f.pendingUpdate = 0
  177. return wu
  178. }
  179. return 0
  180. }
  181. func (f *inFlow) resetPendingData() uint32 {
  182. f.mu.Lock()
  183. defer f.mu.Unlock()
  184. n := f.pendingData
  185. f.pendingData = 0
  186. return n
  187. }