Skip to content

Commit b332f52

Browse files
Allow configurable max buffer size for HTTP connections (#3349)
Signed-off-by: Brian Hardock <[email protected]>
1 parent f5b2833 commit b332f52

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

crates/trigger-http/src/lib.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ pub struct CliArgs {
5252
#[clap(long, env = "SPIN_TLS_KEY", requires = "tls-cert")]
5353
pub tls_key: Option<PathBuf>,
5454

55+
/// Sets the maximum buffer size (in bytes) for the HTTP connection. The minimum value allowed is 8192.
56+
#[clap(long, env = "SPIN_HTTP1_MAX_BUF_SIZE")]
57+
pub http1_max_buf_size: Option<usize>,
58+
5559
#[clap(long = "find-free-port")]
5660
pub find_free_port: bool,
5761
}
@@ -78,6 +82,7 @@ pub struct HttpTrigger {
7882
listen_addr: SocketAddr,
7983
tls_config: Option<TlsConfig>,
8084
find_free_port: bool,
85+
http1_max_buf_size: Option<usize>,
8186
}
8287

8388
impl<F: RuntimeFactors> Trigger<F> for HttpTrigger {
@@ -88,12 +93,14 @@ impl<F: RuntimeFactors> Trigger<F> for HttpTrigger {
8893

8994
fn new(cli_args: Self::CliArgs, app: &spin_app::App) -> anyhow::Result<Self> {
9095
let find_free_port = cli_args.find_free_port;
96+
let http1_max_buf_size = cli_args.http1_max_buf_size;
9197

9298
Self::new(
9399
app,
94100
cli_args.address,
95101
cli_args.into_tls_config(),
96102
find_free_port,
103+
http1_max_buf_size,
97104
)
98105
}
99106

@@ -117,13 +124,15 @@ impl HttpTrigger {
117124
listen_addr: SocketAddr,
118125
tls_config: Option<TlsConfig>,
119126
find_free_port: bool,
127+
http1_max_buf_size: Option<usize>,
120128
) -> anyhow::Result<Self> {
121129
Self::validate_app(app)?;
122130

123131
Ok(Self {
124132
listen_addr,
125133
tls_config,
126134
find_free_port,
135+
http1_max_buf_size,
127136
})
128137
}
129138

@@ -136,12 +145,14 @@ impl HttpTrigger {
136145
listen_addr,
137146
tls_config,
138147
find_free_port,
148+
http1_max_buf_size,
139149
} = self;
140150
let server = Arc::new(HttpServer::new(
141151
listen_addr,
142152
tls_config,
143153
find_free_port,
144154
trigger_app,
155+
http1_max_buf_size,
145156
)?);
146157
Ok(server)
147158
}

crates/trigger-http/src/server.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ pub struct HttpServer<F: RuntimeFactors> {
5858
listen_addr: SocketAddr,
5959
/// The TLS configuration for the server.
6060
tls_config: Option<TlsConfig>,
61+
/// The maximum buffer size for an HTTP1 connection.
62+
http1_max_buf_size: Option<usize>,
6163
/// Whether to find a free port if the specified port is already in use.
6264
find_free_port: bool,
6365
/// Request router.
@@ -77,6 +79,7 @@ impl<F: RuntimeFactors> HttpServer<F> {
7779
tls_config: Option<TlsConfig>,
7880
find_free_port: bool,
7981
trigger_app: TriggerApp<F>,
82+
http1_max_buf_size: Option<usize>,
8083
) -> anyhow::Result<Self> {
8184
// This needs to be a vec before building the router to handle duplicate routes
8285
let component_trigger_configs = trigger_app
@@ -139,6 +142,7 @@ impl<F: RuntimeFactors> HttpServer<F> {
139142
find_free_port,
140143
router,
141144
trigger_app,
145+
http1_max_buf_size,
142146
component_trigger_configs,
143147
component_handler_types,
144148
})
@@ -491,7 +495,13 @@ impl<F: RuntimeFactors> HttpServer<F> {
491495
client_addr: SocketAddr,
492496
) {
493497
task::spawn(async move {
494-
if let Err(err) = Builder::new(TokioExecutor::new())
498+
let mut server_builder = Builder::new(TokioExecutor::new());
499+
500+
if let Some(http1_max_buf_size) = self.http1_max_buf_size {
501+
server_builder.http1().max_buf_size(http1_max_buf_size);
502+
}
503+
504+
if let Err(err) = server_builder
495505
.serve_connection(
496506
TokioIo::new(stream),
497507
service_fn(move |request| {

tests/testing-framework/src/runtimes/in_process_spin.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ async fn initialize_trigger(
104104
.await?;
105105

106106
let app = spin_app::App::new("my-app", locked_app);
107-
let trigger = HttpTrigger::new(&app, "127.0.0.1:80".parse().unwrap(), None, false)?;
107+
let trigger = HttpTrigger::new(&app, "127.0.0.1:80".parse().unwrap(), None, false, None)?;
108108
let mut builder = TriggerAppBuilder::<_, FactorsBuilder>::new(trigger);
109109
let trigger_app = builder
110110
.build(

0 commit comments

Comments
 (0)