github.com/hxx258456/ccgo@v0.0.5-0.20230213014102-48b35f46f66f/net/http2/flow.go (about)

     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  
     5  // Flow control
     6  
     7  package http2
     8  
     9  // flow is the flow control window's size.
    10  type flow struct {
    11  	_ incomparable
    12  
    13  	// 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  
    23  func (f *flow) setConnFlow(cf *flow) { f.conn = cf }
    24  
    25  func (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  
    33  func (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.
    45  func (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  }