Skip to content

Commit

Permalink
Change ChunkedWriterStrategy to minimize connection writes
Browse files Browse the repository at this point in the history
  • Loading branch information
rmja committed May 17, 2024
1 parent f3bd3cb commit 87353be
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ where
HttpConnection::Plain(c) => {
let mut writer = ChunkedBodyWriter::new(c);
body.write(&mut writer).await?;
writer.write_empty_chunk().await.map_err(|e| e.kind())?;
writer.write_termination().await.map_err(|e| e.kind())?;
}
HttpConnection::PlainBuffered(buffered_conn) => {
// Flush the buffered connection so that we can bypass it and rent its buffer
Expand All @@ -335,7 +335,7 @@ where
HttpConnection::Tls(c) => {
let mut writer = ChunkedBodyWriter::new(c);
body.write(&mut writer).await?;
writer.write_empty_chunk().await.map_err(|e| e.kind())?;
writer.write_termination().await.map_err(|e| e.kind())?;
}
#[cfg(all(not(feature = "embedded-tls"), not(feature = "esp-mbedtls")))]
HttpConnection::Tls(_) => unreachable!(),
Expand Down
25 changes: 17 additions & 8 deletions src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use core::fmt::Write as _;
use core::mem::size_of;
use embedded_io::{Error as _, ErrorType};
use embedded_io_async::Write;
use heapless::String;
use heapless::{String, Vec};

/// A read only HTTP request type
pub struct Request<'req, B>
Expand Down Expand Up @@ -365,8 +365,8 @@ where
Self(conn, 0)
}

pub async fn write_empty_chunk(&mut self) -> Result<(), C::Error> {
self.0.write_all(b"0\r\n\r\n").await
pub async fn write_termination(&mut self) -> Result<(), C::Error> {
self.0.write_all(b"\r\n0\r\n\r\n").await
}
}

Expand Down Expand Up @@ -404,15 +404,23 @@ where
hex::encode_to_slice(len.to_be_bytes(), &mut hex).unwrap();
let leading_zeros = hex.iter().position(|x| *x != b'0').unwrap_or_default();
let (_, hex) = hex.split_at(leading_zeros);
self.0.write_all(hex).await.map_err(to_errorkind)?;
self.0.write_all(b"\r\n").await.map_err(to_errorkind)?;

// Write chunk
let mut header = Vec::<u8, { 2 + 8 + 2 }>::new();
if self.1 > 0 {
// Write newline terminating ongoing chunk
// We do it here instead of after writing the chunk
// to minimize the number of writes to the underlying connection
header.extend_from_slice(b"\r\n").unwrap();
}
header.extend_from_slice(hex).unwrap();
header.extend_from_slice(b"\r\n").unwrap();

self.0.write_all(&header).await.map_err(to_errorkind)?;

// Write chunk payload
self.0.write_all(buf).await.map_err(to_errorkind)?;
self.1 += len;

// Write newline
self.0.write_all(b"\r\n").await.map_err(to_errorkind)?;
Ok(())
}

Expand All @@ -424,6 +432,7 @@ where
#[cfg(test)]
mod tests {
use super::*;
use std::vec::Vec;

#[tokio::test]
async fn basic_auth() {
Expand Down

0 comments on commit 87353be

Please sign in to comment.