-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
feat: add TCP keepalive for MySQL and PostgresSQL. #3559
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -3,6 +3,7 @@ use std::io; | |||||
use std::path::Path; | ||||||
use std::pin::Pin; | ||||||
use std::task::{Context, Poll}; | ||||||
use std::time::Duration; | ||||||
|
||||||
use bytes::BufMut; | ||||||
use futures_core::ready; | ||||||
|
@@ -182,10 +183,18 @@ impl<S: Socket + ?Sized> Socket for Box<S> { | |||||
} | ||||||
} | ||||||
|
||||||
#[derive(Debug, Clone)] | ||||||
pub struct TcpKeepalive { | ||||||
pub time: Duration, | ||||||
pub interval: Duration, | ||||||
pub retries: u32, | ||||||
} | ||||||
|
||||||
pub async fn connect_tcp<Ws: WithSocket>( | ||||||
host: &str, | ||||||
port: u16, | ||||||
with_socket: Ws, | ||||||
keepalive: &Option<TcpKeepalive>, | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Would this not be a better API?
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I didn't notice that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I didn't notice the API design preference in Rust. Thank you for pointing this out for me! |
||||||
) -> crate::Result<Ws::Output> { | ||||||
// IPv6 addresses in URLs will be wrapped in brackets and the `url` crate doesn't trim those. | ||||||
let host = host.trim_matches(&['[', ']'][..]); | ||||||
|
@@ -197,6 +206,16 @@ pub async fn connect_tcp<Ws: WithSocket>( | |||||
let stream = TcpStream::connect((host, port)).await?; | ||||||
stream.set_nodelay(true)?; | ||||||
|
||||||
// set tcp keepalive | ||||||
if let Some(keepalive) = keepalive { | ||||||
let keepalive = socket2::TcpKeepalive::new() | ||||||
.with_interval(keepalive.interval) | ||||||
.with_retries(keepalive.retries) | ||||||
.with_time(keepalive.time); | ||||||
let sock_ref = socket2::SockRef::from(&stream); | ||||||
sock_ref.set_tcp_keepalive(&keepalive)?; | ||||||
} | ||||||
|
||||||
return Ok(with_socket.with_socket(stream)); | ||||||
} | ||||||
|
||||||
|
@@ -216,9 +235,24 @@ pub async fn connect_tcp<Ws: WithSocket>( | |||||
s.get_ref().set_nodelay(true)?; | ||||||
Ok(s) | ||||||
}); | ||||||
match stream { | ||||||
Ok(stream) => return Ok(with_socket.with_socket(stream)), | ||||||
Err(e) => last_err = Some(e), | ||||||
let stream = match stream { | ||||||
Ok(stream) => stream, | ||||||
Err(e) => { | ||||||
last_err = Some(e); | ||||||
continue; | ||||||
} | ||||||
}; | ||||||
// set tcp keepalive | ||||||
if let Some(keepalive) = keepalive { | ||||||
let keepalive = socket2::TcpKeepalive::new() | ||||||
.with_interval(keepalive.interval) | ||||||
.with_retries(keepalive.retries) | ||||||
.with_time(keepalive.time); | ||||||
let sock_ref = socket2::SockRef::from(&stream); | ||||||
match sock_ref.set_tcp_keepalive(&keepalive) { | ||||||
Ok(_) => return Ok(with_socket.with_socket(stream)), | ||||||
Err(e) => last_err = Some(e), | ||||||
} | ||||||
} | ||||||
} | ||||||
|
||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://docs.rs/socket2/latest/socket2/struct.TcpKeepalive.html
I think we should use the builder pattern too and copy the platform support of
socket2
given that this is explicitly done in their case.Also adding documentation via docstrings is really helpful => copying their docs is likely fine.
Likely we should make thisCopy
too.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for giving me a review!
That's reasonable, or we might have to maintain the consistency between
TcpKeepalive
defined insocket2
and insqlx_core
.Should I re-export
TcpKeepalive
definition insocket2
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Current code is a build failour on the following platforms (assuming one tries to use the code):
Please use the same platform
cfg
scoping as the new dependency.