Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This is achieved through two different means: - The ability to disable buffering inside `Response` objects with the `Response::with_buffering` method. Enabling this will force the transfer encoding to be `TransferEncoding::Chunked` and will configure the chunks encoder to flush to its underlying writer on every write. - To get "instantaneous" write, disabling buffering in the chunks encoder is not enough, as the underlying writer returned when calling `Server::recv()` (`ClientConnection.sink`) is in fact a `BufWriter` wrapping the "real" output. The `writer_buffering` parameter in `ServerConfig.advanced` can alter the server behavior to omit the BufWriter when writing to the TcpStream. The cost of that abstraction is that `ClientConnection.sink` now boxes the writer to be able to choose between `BufWriter<RefinedTcpStream>` and `RefinedTcpStream` dynamically, which means there is now one additional pointer deference. However, this pointer is then stored in an Arc<Mutex<>>, and locking/unlocking the mutex is probbaly more expensive that deferencing the pointer. This will probably decrease performance significantly when sending big files, which is why these two subfeatures are disabled by default, and must be opted-in (by calling the `with_buffering` method for the first, and by instanciating the server with the `with_writer_buffering_mode` method for the second).
- Loading branch information
1 parent
33b0f26
commit 4968af3
Showing
8 changed files
with
268 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
use std::io::{BufWriter, Result as IoResult, Write}; | ||
|
||
pub enum MaybeBufferedWriter<W: Write> { | ||
Buffered(BufWriter<W>), | ||
Unbuffered(W), | ||
} | ||
|
||
impl<W: Write> Write for MaybeBufferedWriter<W> { | ||
fn write(&mut self, buf: &[u8]) -> IoResult<usize> { | ||
match self { | ||
MaybeBufferedWriter::Buffered(w) => w.write(buf), | ||
MaybeBufferedWriter::Unbuffered(w) => w.write(buf), | ||
} | ||
} | ||
|
||
fn write_all(&mut self, buf: &[u8]) -> IoResult<()> { | ||
match self { | ||
MaybeBufferedWriter::Buffered(w) => w.write_all(buf), | ||
MaybeBufferedWriter::Unbuffered(w) => w.write_all(buf), | ||
} | ||
} | ||
|
||
fn flush(&mut self) -> IoResult<()> { | ||
match self { | ||
MaybeBufferedWriter::Buffered(w) => w.flush(), | ||
MaybeBufferedWriter::Unbuffered(w) => w.flush(), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.