flow.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. // flow is the flow control window's size.
  7. type flow struct {
  8. _ incomparable
  9. // n is the number of DATA bytes we're allowed to send.
  10. // A flow is kept both on a conn and a per-stream.
  11. n int32
  12. // conn points to the shared connection-level flow that is
  13. // shared by all streams on that conn. It is nil for the flow
  14. // that's on the conn directly.
  15. conn *flow
  16. }
  17. func (f *flow) setConnFlow(cf *flow) { f.conn = cf }
  18. func (f *flow) available() int32 {
  19. n := f.n
  20. if f.conn != nil && f.conn.n < n {
  21. n = f.conn.n
  22. }
  23. return n
  24. }
  25. func (f *flow) take(n int32) {
  26. if n > f.available() {
  27. panic("internal error: took too much")
  28. }
  29. f.n -= n
  30. if f.conn != nil {
  31. f.conn.n -= n
  32. }
  33. }
  34. // add adds n bytes (positive or negative) to the flow control window.
  35. // It returns false if the sum would exceed 2^31-1.
  36. func (f *flow) add(n int32) bool {
  37. sum := f.n + n
  38. if (sum > n) == (f.n > 0) {
  39. f.n = sum
  40. return true
  41. }
  42. return false
  43. }