Skip to content

Commit

Permalink
poem v3.1.4
Browse files Browse the repository at this point in the history
  • Loading branch information
sunli829 committed Nov 20, 2024
1 parent 0cecfd7 commit 9fe0c68
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 12 deletions.
4 changes: 4 additions & 0 deletions poem-grpc/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

# [0.5.2] 2024-11-20

- Add `ClientConfigBuilder::http2_max_header_list_size` method to set the max size of received header frames.

# [0.5.1] 2024-09-12

- set the correct `content-type` for `GrpcClient`
Expand Down
4 changes: 2 additions & 2 deletions poem-grpc/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "poem-grpc"
version = "0.5.1"
version = "0.5.2"
authors.workspace = true
edition.workspace = true
license.workspace = true
Expand Down Expand Up @@ -42,7 +42,7 @@ thiserror.workspace = true
fastrand = "2.0.0"
http.workspace = true
hyper = { version = "1.0.0", features = ["http1", "http2"] }
hyper-util = { version = "0.1.3", features = ["client-legacy", "tokio"] }
hyper-util = { version = "0.1.10", features = ["client-legacy", "tokio"] }
http-body-util = "0.1.0"
tokio-rustls.workspace = true
tower-service = "0.3.2"
Expand Down
21 changes: 19 additions & 2 deletions poem-grpc/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,25 @@ use crate::{
pub(crate) type BoxBody = http_body_util::combinators::BoxBody<Bytes, IoError>;

/// A configuration for GRPC client
#[derive(Default)]
pub struct ClientConfig {
uris: Vec<Uri>,
origin: Option<Uri>,
user_agent: Option<HeaderValue>,
tls_config: Option<TlsClientConfig>,
max_header_list_size: u32,
}

impl ClientConfig {
/// Create a `ClientConfig` builder
pub fn builder() -> ClientConfigBuilder {
ClientConfigBuilder {
config: Ok(ClientConfig::default()),
config: Ok(ClientConfig {
uris: vec![],
origin: None,
user_agent: None,
tls_config: None,
max_header_list_size: 16384,
}),
}
}
}
Expand Down Expand Up @@ -146,6 +152,16 @@ impl ClientConfigBuilder {
self
}

/// Sets the max size of received header frames.
///
/// Default is `16384` bytes.
pub fn http2_max_header_list_size(mut self, max: u32) -> Self {
if let Ok(config) = &mut self.config {
config.max_header_list_size = max;
}
self
}

/// Consumes this builder and returns the `ClientConfig`
pub fn build(self) -> Result<ClientConfig, ClientBuilderError> {
self.config
Expand Down Expand Up @@ -451,6 +467,7 @@ fn create_client_endpoint(
let mut config = config;
let cli = Client::builder(TokioExecutor::new())
.http2_only(true)
.http2_max_header_list_size(config.max_header_list_size)
.build(HttpsConnector::new(config.tls_config.take()));

let config = Arc::new(config);
Expand Down
4 changes: 4 additions & 0 deletions poem/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

# [3.1.4] 2024-11-20

- Add `Server::http2_max_header_list_size` method to set the max size of received header frames.

# [3.1.3] 2024-10-21

- Add `Middlware::combine_if` method.
Expand Down
2 changes: 1 addition & 1 deletion poem/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "poem"
version = "3.1.3"
version = "3.1.4"
authors.workspace = true
edition.workspace = true
license.workspace = true
Expand Down
33 changes: 26 additions & 7 deletions poem/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ pub struct Server<L, A> {
idle_timeout: Option<Duration>,
http2_max_concurrent_streams: Option<u32>,
http2_max_pending_accept_reset_streams: Option<u32>,
http2_max_header_list_size: u32,
}

impl<L: Listener> Server<L, Infallible> {
Expand All @@ -55,6 +56,7 @@ impl<L: Listener> Server<L, Infallible> {
idle_timeout: None,
http2_max_concurrent_streams: None,
http2_max_pending_accept_reset_streams: Some(20),
http2_max_header_list_size: 16384,
}
}
}
Expand All @@ -68,6 +70,7 @@ impl<A: Acceptor> Server<Infallible, A> {
idle_timeout: None,
http2_max_concurrent_streams: None,
http2_max_pending_accept_reset_streams: Some(20),
http2_max_header_list_size: 16384,
}
}
}
Expand Down Expand Up @@ -110,6 +113,16 @@ where
}
}

/// Sets the max size of received header frames.
///
/// Default is `16384` bytes.
pub fn http2_max_header_list_size(self, max: u32) -> Self {
Self {
http2_max_header_list_size: max,
..self
}
}

/// Configures the maximum number of pending reset streams allowed before a
/// GOAWAY will be sent.
///
Expand Down Expand Up @@ -150,6 +163,7 @@ where
idle_timeout,
http2_max_concurrent_streams,
http2_max_pending_accept_reset_streams,
http2_max_header_list_size,
} = self;
let name = name.as_deref();
let alive_connections = Arc::new(AtomicUsize::new(0));
Expand Down Expand Up @@ -212,6 +226,7 @@ where
idle_connection_close_timeout: idle_timeout,
http2_max_concurrent_streams,
http2_max_pending_accept_reset_streams,
http2_max_header_list_size,
});

if timeout.is_some() {
Expand Down Expand Up @@ -376,10 +391,14 @@ struct ConnectionOptions<Io> {
idle_connection_close_timeout: Option<Duration>,
http2_max_concurrent_streams: Option<u32>,
http2_max_pending_accept_reset_streams: Option<u32>,
http2_max_header_list_size: u32,
}

async fn serve_connection<Io>(
ConnectionOptions {
async fn serve_connection<Io>(opts: ConnectionOptions<Io>)
where
Io: AsyncRead + AsyncWrite + Send + Unpin + 'static,
{
let ConnectionOptions {
socket,
local_addr,
remote_addr,
Expand All @@ -389,10 +408,9 @@ async fn serve_connection<Io>(
idle_connection_close_timeout,
http2_max_concurrent_streams,
http2_max_pending_accept_reset_streams,
}: ConnectionOptions<Io>,
) where
Io: AsyncRead + AsyncWrite + Send + Unpin + 'static,
{
http2_max_header_list_size,
} = opts;

let connection_shutdown_token = CancellationToken::new();

let service = hyper::service::service_fn({
Expand Down Expand Up @@ -435,7 +453,8 @@ async fn serve_connection<Io>(
.max_concurrent_streams(http2_max_concurrent_streams)
.max_pending_accept_reset_streams(
http2_max_pending_accept_reset_streams.map(|x| x as usize),
);
)
.max_header_list_size(http2_max_header_list_size);

let conn =
builder.serve_connection_with_upgrades(hyper_util::rt::TokioIo::new(socket), service);
Expand Down

0 comments on commit 9fe0c68

Please sign in to comment.