control.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. mu sync.Mutex
  144. // pendingData is the overall data which have been received but not been
  145. // consumed by applications.
  146. pendingData uint32
  147. // The amount of data the application has consumed but grpc has not sent
  148. // window update for them. Used to reduce window update frequency.
  149. pendingUpdate uint32
  150. }
  151. // onData is invoked when some data frame is received. It updates pendingData.
  152. func (f *inFlow) onData(n uint32) error {
  153. f.mu.Lock()
  154. defer f.mu.Unlock()
  155. f.pendingData += n
  156. if f.pendingData+f.pendingUpdate > f.limit {
  157. return fmt.Errorf("received %d-bytes data exceeding the limit %d bytes", f.pendingData+f.pendingUpdate, f.limit)
  158. }
  159. return nil
  160. }
  161. // onRead is invoked when the application reads the data. It returns the window size
  162. // to be sent to the peer.
  163. func (f *inFlow) onRead(n uint32) uint32 {
  164. f.mu.Lock()
  165. defer f.mu.Unlock()
  166. if f.pendingData == 0 {
  167. return 0
  168. }
  169. f.pendingData -= n
  170. f.pendingUpdate += n
  171. if f.pendingUpdate >= f.limit/4 {
  172. wu := f.pendingUpdate
  173. f.pendingUpdate = 0
  174. return wu
  175. }
  176. return 0
  177. }
  178. func (f *inFlow) resetPendingData() uint32 {
  179. f.mu.Lock()
  180. defer f.mu.Unlock()
  181. n := f.pendingData
  182. f.pendingData = 0
  183. return n
  184. }