use super::Limit;
use crate::App;
#[derive(Debug, Clone, Copy)]
pub struct Http2Limits {
pub(crate) max_concurrent_streams: Limit<u32>,
pub(crate) max_frame_size: Limit<u32>,
pub(crate) max_pending_reset_streams: Limit<usize>,
pub(crate) max_local_error_reset_streams: Limit<usize>,
}
impl Default for Http2Limits {
#[inline]
fn default() -> Self {
Self {
max_concurrent_streams: Limit::Default,
max_frame_size: Limit::Default,
max_pending_reset_streams: Limit::Default,
max_local_error_reset_streams: Limit::Default,
}
}
}
impl Http2Limits {
#[inline]
pub fn new() -> Self {
Self::default()
}
#[inline]
pub fn with_max_concurrent_streams(mut self, limit: Limit<u32>) -> Self {
self.max_concurrent_streams = limit;
self
}
#[inline]
pub fn with_max_frame_size(mut self, limit: Limit<u32>) -> Self {
self.max_frame_size = limit;
self
}
#[inline]
pub fn with_max_pending_reset_streams(mut self, limit: Limit<usize>) -> Self {
self.max_pending_reset_streams = limit;
self
}
#[inline]
pub fn with_max_local_error_reset_streams(mut self, limit: Limit<usize>) -> Self {
self.max_local_error_reset_streams = limit;
self
}
}
impl App {
pub fn with_http2_limits<F>(mut self, config: F) -> Self
where
F: FnOnce(Http2Limits) -> Http2Limits,
{
self.http2_limits = config(self.http2_limits);
self
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn it_sets_http2_limits() {
let app = App::new().with_http2_limits(|limits| {
limits
.with_max_concurrent_streams(Limit::Limited(100))
.with_max_frame_size(Limit::Default)
.with_max_pending_reset_streams(Limit::Limited(1024))
.with_max_local_error_reset_streams(Limit::Unlimited)
});
assert_eq!(app.http2_limits.max_concurrent_streams, Limit::Limited(100));
assert_eq!(
app.http2_limits.max_pending_reset_streams,
Limit::Limited(1024)
);
assert_eq!(app.http2_limits.max_frame_size, Limit::Default);
assert_eq!(
app.http2_limits.max_local_error_reset_streams,
Limit::Unlimited
);
}
#[test]
fn it_creates_and_configures_http2_limits() {
let limits = Http2Limits::new()
.with_max_concurrent_streams(Limit::Limited(100))
.with_max_frame_size(Limit::Default)
.with_max_pending_reset_streams(Limit::Limited(1024))
.with_max_local_error_reset_streams(Limit::Unlimited);
assert_eq!(limits.max_concurrent_streams, Limit::Limited(100));
assert_eq!(limits.max_pending_reset_streams, Limit::Limited(1024));
assert_eq!(limits.max_frame_size, Limit::Default);
assert_eq!(limits.max_local_error_reset_streams, Limit::Unlimited);
}
}