Skip to content

Commit 7508bd8

Browse files
committed
refactor(client): restore handshake to by-ref
1 parent 41f4173 commit 7508bd8

File tree

2 files changed

+39
-37
lines changed

2 files changed

+39
-37
lines changed

src/client/conn.rs

Lines changed: 39 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -506,46 +506,50 @@ impl Builder {
506506
}
507507

508508
/// Constructs a connection with the configured options and IO.
509-
pub async fn handshake<T, B>(self, io: T) -> crate::Result<(SendRequest<B>, Connection<T, B>)>
509+
pub fn handshake<T, B>(&self, io: T) -> impl Future<Output = crate::Result<(SendRequest<B>, Connection<T, B>)>>
510510
where
511511
T: AsyncRead + AsyncWrite + Unpin + Send + 'static,
512512
B: Payload + 'static,
513513
B::Data: Unpin,
514514
{
515-
trace!("client handshake HTTP/{}", if self.http2 { 2 } else { 1 });
516-
517-
let (tx, rx) = dispatch::channel();
518-
let either = if !self.http2 {
519-
let mut conn = proto::Conn::new(io);
520-
if !self.h1_writev {
521-
conn.set_write_strategy_flatten();
522-
}
523-
if self.h1_title_case_headers {
524-
conn.set_title_case_headers();
525-
}
526-
if let Some(sz) = self.h1_read_buf_exact_size {
527-
conn.set_read_buf_exact_size(sz);
528-
}
529-
if let Some(max) = self.h1_max_buf_size {
530-
conn.set_max_buf_size(max);
531-
}
532-
let cd = proto::h1::dispatch::Client::new(rx);
533-
let dispatch = proto::h1::Dispatcher::new(cd, conn);
534-
Either::Left(dispatch)
535-
} else {
536-
let h2 = proto::h2::client::handshake(io, rx, &self.h2_builder, self.exec.clone())
537-
.await?;
538-
Either::Right(h2)
539-
};
540-
541-
Ok((
542-
SendRequest {
543-
dispatch: tx,
544-
},
545-
Connection {
546-
inner: Some(either),
547-
},
548-
))
515+
let opts = self.clone();
516+
517+
async move {
518+
trace!("client handshake HTTP/{}", if opts.http2 { 2 } else { 1 });
519+
520+
let (tx, rx) = dispatch::channel();
521+
let either = if !opts.http2 {
522+
let mut conn = proto::Conn::new(io);
523+
if !opts.h1_writev {
524+
conn.set_write_strategy_flatten();
525+
}
526+
if opts.h1_title_case_headers {
527+
conn.set_title_case_headers();
528+
}
529+
if let Some(sz) = opts.h1_read_buf_exact_size {
530+
conn.set_read_buf_exact_size(sz);
531+
}
532+
if let Some(max) = opts.h1_max_buf_size {
533+
conn.set_max_buf_size(max);
534+
}
535+
let cd = proto::h1::dispatch::Client::new(rx);
536+
let dispatch = proto::h1::Dispatcher::new(cd, conn);
537+
Either::Left(dispatch)
538+
} else {
539+
let h2 = proto::h2::client::handshake(io, rx, &opts.h2_builder, opts.exec.clone())
540+
.await?;
541+
Either::Right(h2)
542+
};
543+
544+
Ok((
545+
SendRequest {
546+
dispatch: tx,
547+
},
548+
Connection {
549+
inner: Some(either),
550+
},
551+
))
552+
}
549553
}
550554
}
551555

src/client/mod.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -514,8 +514,6 @@ where C: Connect + Sync + 'static,
514514
let is_h2 = is_ver_h2 || connected.alpn == Alpn::H2;
515515
Either::Left(Box::pin(conn_builder
516516
.http2_only(is_h2)
517-
// TODO: convert client::conn::Builder to be by-value?
518-
.clone()
519517
.handshake(io)
520518
.and_then(move |(tx, conn)| {
521519
trace!("handshake complete, spawning background dispatcher task");

0 commit comments

Comments
 (0)