Skip to content

Commit bae42d4

Browse files
committed
chore: using singleton handler
1 parent 833b8bc commit bae42d4

File tree

5 files changed

+8
-10
lines changed

5 files changed

+8
-10
lines changed

crates/http/benches/http_bench.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ use std::{
1212
error::Error,
1313
io,
1414
pin::Pin,
15-
sync::Arc,
1615
task::{Context, Poll},
1716
};
1817
use std::hint::black_box;
@@ -114,14 +113,14 @@ fn bench_response_encoder(c: &mut Criterion) {
114113

115114
fn bench_http_connection(c: &mut Criterion) {
116115
let request = REQUEST.as_bytes();
117-
let handler = Arc::new(make_handler(test_handler));
116+
let handler = make_handler(test_handler);
118117

119118
c.bench_function("process_simple_request", |b| {
120119
b.iter(|| {
121120
let mock_io = MockIO::new(request.to_vec());
122121
let (reader, writer) = (mock_io.clone(), mock_io);
123122
let connection = HttpConnection::new(reader, writer);
124-
block_on(connection.process(handler.clone())).unwrap();
123+
block_on(connection.process(&handler)).unwrap();
125124
});
126125
});
127126
}

crates/http/examples/server.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ async fn main() {
4242
tokio::spawn(async move {
4343
let (reader, writer) = tcp_stream.into_split();
4444
let connection = HttpConnection::new(reader, writer);
45-
match connection.process(handler).await {
45+
match connection.process(handler.as_ref()).await {
4646
Ok(_) => {
4747
info!("finished process, connection shutdown");
4848
}

crates/http/src/connection/http_connection.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use bytes::Bytes;
22
use std::error::Error;
33
use std::fmt::{Debug, Display};
4-
use std::sync::Arc;
54

65
use futures::StreamExt;
76
use http::header::EXPECT;
@@ -54,7 +53,7 @@ where
5453
}
5554
}
5655

57-
pub async fn process<H>(mut self, mut handler: Arc<H>) -> Result<(), HttpError>
56+
pub async fn process<H>(mut self, handler: &H) -> Result<(), HttpError>
5857
where
5958
H: Handler,
6059
H::RespBody: Body<Data = Bytes> + Unpin,
@@ -63,7 +62,7 @@ where
6362
loop {
6463
match self.framed_read.next().await {
6564
Some(Ok(Message::Header((header, payload_size)))) => {
66-
self.do_process(header, payload_size, &mut handler).await?;
65+
self.do_process(header, payload_size, handler).await?;
6766
}
6867

6968
Some(Ok(Message::Payload(PayloadItem::Eof))) => continue,
@@ -93,7 +92,7 @@ where
9392
}
9493
}
9594

96-
async fn do_process<H>(&mut self, header: RequestHeader, payload_size: PayloadSize, handler: &mut Arc<H>) -> Result<(), HttpError>
95+
async fn do_process<H>(&mut self, header: RequestHeader, payload_size: PayloadSize, handler: &H) -> Result<(), HttpError>
9796
where
9897
H: Handler,
9998
H::RespBody: Body<Data = Bytes> + Unpin,

crates/http/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
//! tokio::spawn(async move {
6565
//! let (reader, writer) = tcp_stream.into_split();
6666
//! let connection = HttpConnection::new(reader, writer);
67-
//! match connection.process(handler).await {
67+
//! match connection.process(handler.as_ref()).await {
6868
//! Ok(_) => {
6969
//! info!("finished process, connection shutdown");
7070
//! }

crates/web/src/server.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ impl Server {
169169
tcp_stream.set_nodelay(true).unwrap();
170170
let (reader, writer) = tcp_stream.into_split();
171171
let connection = HttpConnection::new(reader, writer);
172-
match connection.process(handler).await {
172+
match connection.process(handler.as_ref()).await {
173173
Ok(_) => {
174174
info!("finished process, connection shutdown");
175175
}

0 commit comments

Comments
 (0)