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