# counts.rs - source
Source of the Rust file `/home/runner/.cargo/registry/src/github.com-1ecc6299db9ec823/h2-0.3.2/src/proto/streams/counts.rs`.
Keywords: rust, rustlang, rust-lang

      1
      2
      3
      4
      5
      6
      7
      8
      9
     10
     11
     12
     13
     14
     15
     16
     17
     18
     19
     20
     21
     22
     23
     24
     25
     26
     27
     28
     29
     30
     31
     32
     33
     34
     35
     36
     37
     38
     39
     40
     41
     42
     43
     44
     45
     46
     47
     48
     49
     50
     51
     52
     53
     54
     55
     56
     57
     58
     59
     60
     61
     62
     63
     64
     65
     66
     67
     68
     69
     70
     71
     72
     73
     74
     75
     76
     77
     78
     79
     80
     81
     82
     83
     84
     85
     86
     87
     88
     89
     90
     91
     92
     93
     94
     95
     96
     97
     98
     99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210

    use super::*;

    use std::usize;

    #[derive(Debug)]
    pub(super) struct Counts {
        /// Acting as a client or server. This allows us to track which values to
        /// inc / dec.
        peer: peer::Dyn,

        /// Maximum number of locally initiated streams
        max_send_streams: usize,

        /// Current number of remote initiated streams
        num_send_streams: usize,

        /// Maximum number of remote initiated streams
        max_recv_streams: usize,

        /// Current number of locally initiated streams
        num_recv_streams: usize,

        /// Maximum number of pending locally reset streams
        max_reset_streams: usize,

        /// Current number of pending locally reset streams
        num_reset_streams: usize,
    }

    impl Counts {
        /// Create a new `Counts` using the provided configuration values.
        pub fn new(peer: peer::Dyn, config: &Config) -> Self {
            Counts {
                peer,
                max_send_streams: config.initial_max_send_streams,
                num_send_streams: 0,
                max_recv_streams: config.remote_max_initiated.unwrap_or(usize::MAX),
                num_recv_streams: 0,
                max_reset_streams: config.local_reset_max,
                num_reset_streams: 0,
            }
        }

        /// Returns the current peer
        pub fn peer(&self) -> peer::Dyn {
            self.peer
        }

        pub fn has_streams(&self) -> bool {
            self.num_send_streams != 0 || self.num_recv_streams != 0
        }

        /// Returns true if the receive stream concurrency can be incremented
        pub fn can_inc_num_recv_streams(&self) -> bool {
            self.max_recv_streams > self.num_recv_streams
        }

        /// Increments the number of concurrent receive streams.
        ///
        /// # Panics
        ///
        /// Panics on failure as this should have been validated before hand.
        pub fn inc_num_recv_streams(&mut self, stream: &mut store::Ptr) {
            assert!(self.can_inc_num_recv_streams());
            assert!(!stream.is_counted);

            // Increment the number of remote initiated streams
            self.num_recv_streams += 1;
            stream.is_counted = true;
        }

        /// Returns true if the send stream concurrency can be incremented
        pub fn can_inc_num_send_streams(&self) -> bool {
            self.max_send_streams > self.num_send_streams
        }

        /// Increments the number of concurrent send streams.
        ///
        /// # Panics
        ///
        /// Panics on failure as this should have been validated before hand.
        pub fn inc_num_send_streams(&mut self, stream: &mut store::Ptr) {
            assert!(self.can_inc_num_send_streams());
            assert!(!stream.is_counted);

            // Increment the number of remote initiated streams
            self.num_send_streams += 1;
            stream.is_counted = true;
        }

        /// Returns true if the number of pending reset streams can be incremented.
        pub fn can_inc_num_reset_streams(&self) -> bool {
            self.max_reset_streams > self.num_reset_streams
        }

        /// Increments the number of pending reset streams.
        ///
        /// # Panics
        ///
        /// Panics on failure as this should have been validated before hand.
        pub fn inc_num_reset_streams(&mut self) {
            assert!(self.can_inc_num_reset_streams());

            self.num_reset_streams += 1;
        }

        pub fn apply_remote_settings(&mut self, settings: &frame::Settings) {
            if let Some(val) = settings.max_concurrent_streams() {
                self.max_send_streams = val as usize;
            }
        }

        /// Run a block of code that could potentially transition a stream's state.
        ///
        /// If the stream state transitions to closed, this function will perform
        /// all necessary cleanup.
        ///
        /// TODO: Is this function still needed?
        pub fn transition<F, U>(&mut self, mut stream: store::Ptr, f: F) -> U
        where
            F: FnOnce(&mut Self, &mut store::Ptr) -> U,
        {
            // TODO: Does this need to be computed before performing the action?
            let is_pending_reset = stream.is_pending_reset_expiration();

            // Run the action
            let ret = f(self, &mut stream);

            self.transition_after(stream, is_pending_reset);

            ret
        }

        // TODO: move this to macro?
        pub fn transition_after(&mut self, mut stream: store::Ptr, is_reset_counted: bool) {
            tracing::trace!(
                "transition_after; stream={:?}; state={:?}; is_closed={:?}; \
                 pending_send_empty={:?}; buffered_send_data={}; \
                 num_recv={}; num_send={}",
                stream.id,
                stream.state,
                stream.is_closed(),
                stream.pending_send.is_empty(),
                stream.buffered_send_data,
                self.num_recv_streams,
                self.num_send_streams
            );

            if stream.is_closed() {
                if !stream.is_pending_reset_expiration() {
                    stream.unlink();
                    if is_reset_counted {
                        self.dec_num_reset_streams();
                    }
                }

                if stream.is_counted {
                    tracing::trace!("dec_num_streams; stream={:?}", stream.id);
                    // Decrement the number of active streams.
                    self.dec_num_streams(&mut stream);
                }
            }

            // Release the stream if it requires releasing
            if stream.is_released() {
                stream.remove();
            }
        }

        /// Returns the maximum number of streams that can be initiated by this
        /// peer.
        pub(crate) fn max_send_streams(&self) -> usize {
            self.max_send_streams
        }

        /// Returns the maximum number of streams that can be initiated by the
        /// remote peer.
        pub(crate) fn max_recv_streams(&self) -> usize {
            self.max_recv_streams
        }

        fn dec_num_streams(&mut self, stream: &mut store::Ptr) {
            assert!(stream.is_counted);

            if self.peer.is_local_init(stream.id) {
                assert!(self.num_send_streams > 0);
                self.num_send_streams -= 1;
                stream.is_counted = false;
            } else {
                assert!(self.num_recv_streams > 0);
                self.num_recv_streams -= 1;
                stream.is_counted = false;
            }
        }

        fn dec_num_reset_streams(&mut self) {
            assert!(self.num_reset_streams > 0);
            self.num_reset_streams -= 1;
        }
    }

    impl Drop for Counts {
        fn drop(&mut self) {
            use std::thread;

            if !thread::panicking() {
                debug_assert!(!self.has_streams());
            }
        }
    }