control.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. headsUp bool
  67. closeConn bool
  68. }
  69. func (*goAway) item() {}
  70. type flushIO struct {
  71. }
  72. func (*flushIO) item() {}
  73. type ping struct {
  74. ack bool
  75. data [8]byte
  76. }
  77. func (*ping) item() {}
  78. // quotaPool is a pool which accumulates the quota and sends it to acquire()
  79. // when it is available.
  80. type quotaPool struct {
  81. c chan int
  82. mu sync.Mutex
  83. quota int
  84. }
  85. // newQuotaPool creates a quotaPool which has quota q available to consume.
  86. func newQuotaPool(q int) *quotaPool {
  87. qb := &quotaPool{
  88. c: make(chan int, 1),
  89. }
  90. if q > 0 {
  91. qb.c <- q
  92. } else {
  93. qb.quota = q
  94. }
  95. return qb
  96. }
  97. // add cancels the pending quota sent on acquired, incremented by v and sends
  98. // it back on acquire.
  99. func (qb *quotaPool) add(v int) {
  100. qb.mu.Lock()
  101. defer qb.mu.Unlock()
  102. select {
  103. case n := <-qb.c:
  104. qb.quota += n
  105. default:
  106. }
  107. qb.quota += v
  108. if qb.quota <= 0 {
  109. return
  110. }
  111. // After the pool has been created, this is the only place that sends on
  112. // the channel. Since mu is held at this point and any quota that was sent
  113. // on the channel has been retrieved, we know that this code will always
  114. // place any positive quota value on the channel.
  115. select {
  116. case qb.c <- qb.quota:
  117. qb.quota = 0
  118. default:
  119. }
  120. }
  121. // acquire returns the channel on which available quota amounts are sent.
  122. func (qb *quotaPool) acquire() <-chan int {
  123. return qb.c
  124. }
  125. // inFlow deals with inbound flow control
  126. type inFlow struct {
  127. mu sync.Mutex
  128. // The inbound flow control limit for pending data.
  129. limit uint32
  130. // pendingData is the overall data which have been received but not been
  131. // consumed by applications.
  132. pendingData uint32
  133. // The amount of data the application has consumed but grpc has not sent
  134. // window update for them. Used to reduce window update frequency.
  135. pendingUpdate uint32
  136. // delta is the extra window update given by receiver when an application
  137. // is reading data bigger in size than the inFlow limit.
  138. delta uint32
  139. }
  140. // newLimit updates the inflow window to a new value n.
  141. // It assumes that n is always greater than the old limit.
  142. func (f *inFlow) newLimit(n uint32) uint32 {
  143. f.mu.Lock()
  144. defer f.mu.Unlock()
  145. d := n - f.limit
  146. f.limit = n
  147. return d
  148. }
  149. func (f *inFlow) maybeAdjust(n uint32) uint32 {
  150. if n > uint32(math.MaxInt32) {
  151. n = uint32(math.MaxInt32)
  152. }
  153. f.mu.Lock()
  154. defer f.mu.Unlock()
  155. // estSenderQuota is the receiver's view of the maximum number of bytes the sender
  156. // can send without a window update.
  157. estSenderQuota := int32(f.limit - (f.pendingData + f.pendingUpdate))
  158. // estUntransmittedData is the maximum number of bytes the sends might not have put
  159. // on the wire yet. A value of 0 or less means that we have already received all or
  160. // more bytes than the application is requesting to read.
  161. estUntransmittedData := int32(n - f.pendingData) // Casting into int32 since it could be negative.
  162. // This implies that unless we send a window update, the sender won't be able to send all the bytes
  163. // for this message. Therefore we must send an update over the limit since there's an active read
  164. // request from the application.
  165. if estUntransmittedData > estSenderQuota {
  166. // Sender's window shouldn't go more than 2^31 - 1 as speecified in the HTTP spec.
  167. if f.limit+n > maxWindowSize {
  168. f.delta = maxWindowSize - f.limit
  169. } else {
  170. // Send a window update for the whole message and not just the difference between
  171. // estUntransmittedData and estSenderQuota. This will be helpful in case the message
  172. // is padded; We will fallback on the current available window(at least a 1/4th of the limit).
  173. f.delta = n
  174. }
  175. return f.delta
  176. }
  177. return 0
  178. }
  179. // onData is invoked when some data frame is received. It updates pendingData.
  180. func (f *inFlow) onData(n uint32) error {
  181. f.mu.Lock()
  182. defer f.mu.Unlock()
  183. f.pendingData += n
  184. if f.pendingData+f.pendingUpdate > f.limit+f.delta {
  185. return fmt.Errorf("received %d-bytes data exceeding the limit %d bytes", f.pendingData+f.pendingUpdate, f.limit)
  186. }
  187. return nil
  188. }
  189. // onRead is invoked when the application reads the data. It returns the window size
  190. // to be sent to the peer.
  191. func (f *inFlow) onRead(n uint32) uint32 {
  192. f.mu.Lock()
  193. defer f.mu.Unlock()
  194. if f.pendingData == 0 {
  195. return 0
  196. }
  197. f.pendingData -= n
  198. if n > f.delta {
  199. n -= f.delta
  200. f.delta = 0
  201. } else {
  202. f.delta -= n
  203. n = 0
  204. }
  205. f.pendingUpdate += n
  206. if f.pendingUpdate >= f.limit/4 {
  207. wu := f.pendingUpdate
  208. f.pendingUpdate = 0
  209. return wu
  210. }
  211. return 0
  212. }
  213. func (f *inFlow) resetPendingUpdate() uint32 {
  214. f.mu.Lock()
  215. defer f.mu.Unlock()
  216. n := f.pendingUpdate
  217. f.pendingUpdate = 0
  218. return n
  219. }