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