Skip to content

tokio-postgres: buffer sockets to avoid excessive syscalls #777

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions postgres-native-tls/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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?;

Expand All @@ -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
Expand Down
5 changes: 3 additions & 2 deletions postgres-openssl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down Expand Up @@ -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)?;
Expand Down Expand Up @@ -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
Expand Down