This repository has no description
0

Configure Feed

Select the types of activity you want to include in your feed.

core / knot2 / crates / knot-edge / src / limits.rs
2.5 kB 104 lines
1use std::num::{NonZeroU32, NonZeroU64}; 2use std::time::Duration; 3 4const MAX_CONCURRENT_STREAMS: u32 = 256; 5const STREAM_RECEIVE_WINDOW: u32 = 8 * 1024 * 1024; 6const CONNECTION_RECEIVE_WINDOW: u32 = 32 * 1024 * 1024; 7 8knot_types::scalar_newtype! { 9 pub struct MaxConcurrentStreams(u32) => sealed; 10} 11 12#[derive(Debug, Clone, Copy)] 13pub struct ConnectionBudget { 14 max_concurrent_streams: MaxConcurrentStreams, 15 stream_receive_window: u32, 16 connection_receive_window: u32, 17} 18 19impl ConnectionBudget { 20 const DEFAULT: Self = Self { 21 max_concurrent_streams: MaxConcurrentStreams::new(MAX_CONCURRENT_STREAMS), 22 stream_receive_window: STREAM_RECEIVE_WINDOW, 23 connection_receive_window: CONNECTION_RECEIVE_WINDOW, 24 }; 25 26 pub fn max_concurrent_streams(self) -> MaxConcurrentStreams { 27 self.max_concurrent_streams 28 } 29 30 pub fn stream_receive_window(self) -> u32 { 31 self.stream_receive_window 32 } 33 34 pub fn connection_receive_window(self) -> u32 { 35 self.connection_receive_window 36 } 37} 38 39// Separate types that `ListenLimits::new` used to take as 40// a bunch of `NonZeroU64` in a row. 41// Swapping them would incorrectly compile and 42// gave the conn the wrong deadline. 43#[derive(Debug, Clone, Copy)] 44pub struct HeaderTimeout(Duration); 45 46impl HeaderTimeout { 47 pub fn from_millis(millis: NonZeroU64) -> Self { 48 Self(Duration::from_millis(millis.get())) 49 } 50 51 pub fn get(self) -> Duration { 52 self.0 53 } 54} 55 56#[derive(Debug, Clone, Copy)] 57pub struct IdleTimeout(Duration); 58 59impl IdleTimeout { 60 pub fn from_millis(millis: NonZeroU64) -> Self { 61 Self(Duration::from_millis(millis.get())) 62 } 63 64 pub fn get(self) -> Duration { 65 self.0 66 } 67} 68 69#[derive(Debug, Clone, Copy)] 70pub struct ListenLimits { 71 header_timeout: HeaderTimeout, 72 idle_timeout: IdleTimeout, 73 max_connections: usize, 74} 75 76impl ListenLimits { 77 pub fn new( 78 header_timeout: HeaderTimeout, 79 idle_timeout: IdleTimeout, 80 max_connections: NonZeroU32, 81 ) -> Self { 82 Self { 83 header_timeout, 84 idle_timeout, 85 max_connections: max_connections.get() as usize, 86 } 87 } 88 89 pub fn header_timeout(&self) -> HeaderTimeout { 90 self.header_timeout 91 } 92 93 pub fn idle_timeout(&self) -> IdleTimeout { 94 self.idle_timeout 95 } 96 97 pub fn max_connections(&self) -> usize { 98 self.max_connections 99 } 100 101 pub fn connection_budget(&self) -> ConnectionBudget { 102 ConnectionBudget::DEFAULT 103 } 104}