control.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /*
  2. *
  3. * Copyright 2014 gRPC authors.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. package transport
  19. import (
  20. "fmt"
  21. "math"
  22. "sync"
  23. "time"
  24. "golang.org/x/net/http2"
  25. )
  26. const (
  27. // The default value of flow control window size in HTTP2 spec.
  28. defaultWindowSize = 65535
  29. // The initial window size for flow control.
  30. initialWindowSize = defaultWindowSize // for an RPC
  31. infinity = time.Duration(math.MaxInt64)
  32. defaultClientKeepaliveTime = infinity
  33. defaultClientKeepaliveTimeout = time.Duration(20 * time.Second)
  34. defaultMaxStreamsClient = 100
  35. defaultMaxConnectionIdle = infinity
  36. defaultMaxConnectionAge = infinity
  37. defaultMaxConnectionAgeGrace = infinity
  38. defaultServerKeepaliveTime = time.Duration(2 * time.Hour)
  39. defaultServerKeepaliveTimeout = time.Duration(20 * time.Second)
  40. defaultKeepalivePolicyMinTime = time.Duration(5 * time.Minute)
  41. // max window limit set by HTTP2 Specs.
  42. maxWindowSize = math.MaxInt32
  43. )
  44. // The following defines various control items which could flow through
  45. // the control buffer of transport. They represent different aspects of
  46. // control tasks, e.g., flow control, settings, streaming resetting, etc.
  47. type windowUpdate struct {
  48. streamID uint32
  49. increment uint32
  50. flush bool
  51. }
  52. func (*windowUpdate) item() {}
  53. type settings struct {
  54. ack bool
  55. ss []http2.Setting
  56. }
  57. func (*settings) item() {}
  58. type resetStream struct {
  59. streamID uint32
  60. code http2.ErrCode
  61. }
  62. func (*resetStream) item() {}
  63. type goAway struct {
  64. code http2.ErrCode
  65. debugData []byte
  66. }
  67. func (*goAway) item() {}
  68. type flushIO struct {
  69. }
  70. func (*flushIO) item() {}
  71. type ping struct {
  72. ack bool
  73. data [8]byte
  74. }
  75. func (*ping) item() {}
  76. // quotaPool is a pool which accumulates the quota and sends it to acquire()
  77. // when it is available.
  78. type quotaPool struct {
  79. c chan int
  80. mu sync.Mutex
  81. quota int
  82. }
  83. // newQuotaPool creates a quotaPool which has quota q available to consume.
  84. func newQuotaPool(q int) *quotaPool {
  85. qb := &quotaPool{
  86. c: make(chan int, 1),
  87. }
  88. if q > 0 {
  89. qb.c <- q
  90. } else {
  91. qb.quota = q
  92. }
  93. return qb
  94. }
  95. // add cancels the pending quota sent on acquired, incremented by v and sends
  96. // it back on acquire.
  97. func (qb *quotaPool) add(v int) {
  98. qb.mu.Lock()
  99. defer qb.mu.Unlock()
  100. select {
  101. case n := <-qb.c:
  102. qb.quota += n
  103. default:
  104. }
  105. qb.quota += v
  106. if qb.quota <= 0 {
  107. return
  108. }
  109. // After the pool has been created, this is the only place that sends on
  110. // the channel. Since mu is held at this point and any quota that was sent
  111. // on the channel has been retrieved, we know that this code will always
  112. // place any positive quota value on the channel.
  113. select {
  114. case qb.c <- qb.quota:
  115. qb.quota = 0
  116. default:
  117. }
  118. }
  119. // acquire returns the channel on which available quota amounts are sent.
  120. func (qb *quotaPool) acquire() <-chan int {
  121. return qb.c
  122. }
  123. // inFlow deals with inbound flow control
  124. type inFlow struct {
  125. mu sync.Mutex
  126. // The inbound flow control limit for pending data.
  127. limit uint32
  128. // pendingData is the overall data which have been received but not been
  129. // consumed by applications.
  130. pendingData uint32
  131. // The amount of data the application has consumed but grpc has not sent
  132. // window update for them. Used to reduce window update frequency.
  133. pendingUpdate uint32
  134. // delta is the extra window update given by receiver when an application
  135. // is reading data bigger in size than the inFlow limit.
  136. delta uint32
  137. }
  138. // newLimit updates the inflow window to a new value n.
  139. // It assumes that n is always greater than the old limit.
  140. func (f *inFlow) newLimit(n uint32) uint32 {
  141. f.mu.Lock()
  142. defer f.mu.Unlock()
  143. d := n - f.limit
  144. f.limit = n
  145. return d
  146. }
  147. func (f *inFlow) maybeAdjust(n uint32) uint32 {
  148. if n > uint32(math.MaxInt32) {
  149. n = uint32(math.MaxInt32)
  150. }
  151. f.mu.Lock()
  152. defer f.mu.Unlock()
  153. // estSenderQuota is the receiver's view of the maximum number of bytes the sender
  154. // can send without a window update.
  155. estSenderQuota := int32(f.limit - (f.pendingData + f.pendingUpdate))
  156. // estUntransmittedData is the maximum number of bytes the sends might not have put
  157. // on the wire yet. A value of 0 or less means that we have already received all or
  158. // more bytes than the application is requesting to read.
  159. estUntransmittedData := int32(n - f.pendingData) // Casting into int32 since it could be negative.
  160. // This implies that unless we send a window update, the sender won't be able to send all the bytes
  161. // for this message. Therefore we must send an update over the limit since there's an active read
  162. // request from the application.
  163. if estUntransmittedData > estSenderQuota {
  164. // Sender's window shouldn't go more than 2^31 - 1 as speecified in the HTTP spec.
  165. if f.limit+n > maxWindowSize {
  166. f.delta = maxWindowSize - f.limit
  167. } else {
  168. // Send a window update for the whole message and not just the difference between
  169. // estUntransmittedData and estSenderQuota. This will be helpful in case the message
  170. // is padded; We will fallback on the current available window(at least a 1/4th of the limit).
  171. f.delta = n
  172. }
  173. return f.delta
  174. }
  175. return 0
  176. }
  177. // onData is invoked when some data frame is received. It updates pendingData.
  178. func (f *inFlow) onData(n uint32) error {
  179. f.mu.Lock()
  180. defer f.mu.Unlock()
  181. f.pendingData += n
  182. if f.pendingData+f.pendingUpdate > f.limit+f.delta {
  183. return fmt.Errorf("received %d-bytes data exceeding the limit %d bytes", f.pendingData+f.pendingUpdate, f.limit)
  184. }
  185. return nil
  186. }
  187. // onRead is invoked when the application reads the data. It returns the window size
  188. // to be sent to the peer.
  189. func (f *inFlow) onRead(n uint32) uint32 {
  190. f.mu.Lock()
  191. defer f.mu.Unlock()
  192. if f.pendingData == 0 {
  193. return 0
  194. }
  195. f.pendingData -= n
  196. if n > f.delta {
  197. n -= f.delta
  198. f.delta = 0
  199. } else {
  200. f.delta -= n
  201. n = 0
  202. }
  203. f.pendingUpdate += n
  204. if f.pendingUpdate >= f.limit/4 {
  205. wu := f.pendingUpdate
  206. f.pendingUpdate = 0
  207. return wu
  208. }
  209. return 0
  210. }
  211. func (f *inFlow) resetPendingUpdate() uint32 {
  212. f.mu.Lock()
  213. defer f.mu.Unlock()
  214. n := f.pendingUpdate
  215. f.pendingUpdate = 0
  216. return n
  217. }