flow.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // Copyright 2014 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // Flow control
  5. package http2
  6. // inflowMinRefresh is the minimum number of bytes we'll send for a
  7. // flow control window update.
  8. const inflowMinRefresh = 4 << 10
  9. // inflow accounts for an inbound flow control window.
  10. // It tracks both the latest window sent to the peer (used for enforcement)
  11. // and the accumulated unsent window.
  12. type inflow struct {
  13. avail int32
  14. unsent int32
  15. }
  16. // init sets the initial window.
  17. func (f *inflow) init(n int32) {
  18. f.avail = n
  19. }
  20. // add adds n bytes to the window, with a maximum window size of max,
  21. // indicating that the peer can now send us more data.
  22. // For example, the user read from a {Request,Response} body and consumed
  23. // some of the buffered data, so the peer can now send more.
  24. // It returns the number of bytes to send in a WINDOW_UPDATE frame to the peer.
  25. // Window updates are accumulated and sent when the unsent capacity
  26. // is at least inflowMinRefresh or will at least double the peer's available window.
  27. func (f *inflow) add(n int) (connAdd int32) {
  28. if n < 0 {
  29. panic("negative update")
  30. }
  31. unsent := int64(f.unsent) + int64(n)
  32. // "A sender MUST NOT allow a flow-control window to exceed 2^31-1 octets."
  33. // RFC 7540 Section 6.9.1.
  34. const maxWindow = 1<<31 - 1
  35. if unsent+int64(f.avail) > maxWindow {
  36. panic("flow control update exceeds maximum window size")
  37. }
  38. f.unsent = int32(unsent)
  39. if f.unsent < inflowMinRefresh && f.unsent < f.avail {
  40. // If there aren't at least inflowMinRefresh bytes of window to send,
  41. // and this update won't at least double the window, buffer the update for later.
  42. return 0
  43. }
  44. f.avail += f.unsent
  45. f.unsent = 0
  46. return int32(unsent)
  47. }
  48. // take attempts to take n bytes from the peer's flow control window.
  49. // It reports whether the window has available capacity.
  50. func (f *inflow) take(n uint32) bool {
  51. if n > uint32(f.avail) {
  52. return false
  53. }
  54. f.avail -= int32(n)
  55. return true
  56. }
  57. // takeInflows attempts to take n bytes from two inflows,
  58. // typically connection-level and stream-level flows.
  59. // It reports whether both windows have available capacity.
  60. func takeInflows(f1, f2 *inflow, n uint32) bool {
  61. if n > uint32(f1.avail) || n > uint32(f2.avail) {
  62. return false
  63. }
  64. f1.avail -= int32(n)
  65. f2.avail -= int32(n)
  66. return true
  67. }
  68. // outflow is the outbound flow control window's size.
  69. type outflow struct {
  70. _ incomparable
  71. // n is the number of DATA bytes we're allowed to send.
  72. // An outflow is kept both on a conn and a per-stream.
  73. n int32
  74. // conn points to the shared connection-level outflow that is
  75. // shared by all streams on that conn. It is nil for the outflow
  76. // that's on the conn directly.
  77. conn *outflow
  78. }
  79. func (f *outflow) setConnFlow(cf *outflow) { f.conn = cf }
  80. func (f *outflow) available() int32 {
  81. n := f.n
  82. if f.conn != nil && f.conn.n < n {
  83. n = f.conn.n
  84. }
  85. return n
  86. }
  87. func (f *outflow) take(n int32) {
  88. if n > f.available() {
  89. panic("internal error: took too much")
  90. }
  91. f.n -= n
  92. if f.conn != nil {
  93. f.conn.n -= n
  94. }
  95. }
  96. // add adds n bytes (positive or negative) to the flow control window.
  97. // It returns false if the sum would exceed 2^31-1.
  98. func (f *outflow) add(n int32) bool {
  99. sum := f.n + n
  100. if (sum > n) == (f.n > 0) {
  101. f.n = sum
  102. return true
  103. }
  104. return false
  105. }