Merge pull request #777 from petrosagg/buffered-io

tokio-postgres: buffer sockets to avoid excessive syscalls
This commit is contained in:
Steven Fackler 2021-05-24 13:38:45 -04:00 committed by GitHub
commit 3390cf33ed
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 6 additions and 4 deletions

View File

@ -51,7 +51,7 @@ use std::future::Future;
use std::io;
use std::pin::Pin;
use std::task::{Context, Poll};
use tokio::io::{AsyncRead, AsyncWrite, ReadBuf};
use tokio::io::{AsyncRead, AsyncWrite, BufReader, ReadBuf};
use tokio_postgres::tls;
#[cfg(feature = "runtime")]
use tokio_postgres::tls::MakeTlsConnect;
@ -115,6 +115,7 @@ where
type Future = Pin<Box<dyn Future<Output = Result<TlsStream<S>, native_tls::Error>> + Send>>;
fn connect(self, stream: S) -> Self::Future {
let stream = BufReader::with_capacity(8192, stream);
let future = async move {
let stream = self.connector.connect(&self.domain, stream).await?;
@ -126,7 +127,7 @@ where
}
/// The stream returned by `TlsConnector`.
pub struct TlsStream<S>(tokio_native_tls::TlsStream<S>);
pub struct TlsStream<S>(tokio_native_tls::TlsStream<BufReader<S>>);
impl<S> AsyncRead for TlsStream<S>
where

View File

@ -57,7 +57,7 @@ use std::pin::Pin;
#[cfg(feature = "runtime")]
use std::sync::Arc;
use std::task::{Context, Poll};
use tokio::io::{AsyncRead, AsyncWrite, ReadBuf};
use tokio::io::{AsyncRead, AsyncWrite, BufReader, ReadBuf};
use tokio_openssl::SslStream;
use tokio_postgres::tls;
#[cfg(feature = "runtime")]
@ -140,6 +140,7 @@ where
type Future = Pin<Box<dyn Future<Output = Result<TlsStream<S>, Self::Error>> + Send>>;
fn connect(self, stream: S) -> Self::Future {
let stream = BufReader::with_capacity(8192, stream);
let future = async move {
let ssl = self.ssl.into_ssl(&self.domain)?;
let mut stream = SslStream::new(ssl, stream)?;
@ -182,7 +183,7 @@ impl Error for ConnectError {
}
/// The stream returned by `TlsConnector`.
pub struct TlsStream<S>(SslStream<S>);
pub struct TlsStream<S>(SslStream<BufReader<S>>);
impl<S> AsyncRead for TlsStream<S>
where