-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Add ClientBuilder::local_address option to bind to a local IP address #451
Add ClientBuilder::local_address option to bind to a local IP address #451
Conversation
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.
Awesome start! Left comments inline.
CHANGELOG.md
Outdated
@@ -5,6 +5,7 @@ | |||
- Add `ClientBuilder::h2_prior_knowledge()` option to force HTTP2. | |||
- Add `Response::content_length()` to get the content-length of a response. | |||
- Enable ALPN h2 with the rustls-tls backend. | |||
- Add `ClientBuilder::local_address` option to bind to a local IP address |
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.
Don't worry about changelog entries, I fill this in when tagging a release :)
src/async_impl/client.rs
Outdated
@@ -325,6 +330,12 @@ impl ClientBuilder { | |||
pub fn dns_threads(self, _threads: usize) -> ClientBuilder { | |||
self | |||
} | |||
|
|||
/// Bind to a local IP Address | |||
pub fn local_address(mut self, addr: Option<IpAddr>) -> ClientBuilder { |
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.
Consider an argument of addr: impl Into<Option<IpAddr>>
. This way, in the common case, it reads more fluently, like builder.local_address(ip)
instead of builder.local_address(Some(ip))
.
src/client.rs
Outdated
/// .local_address(Some(local_addr.into())) | ||
/// .build().unwrap(); | ||
/// ``` | ||
pub fn local_address(self, addr: Option<IpAddr>) -> ClientBuilder { |
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.
Same thing here about impl Into<Option<IpAddr>>
.
src/client.rs
Outdated
/// | ||
/// ``` | ||
/// use std::net::Ipv4Addr; | ||
/// let local_addr = Ipv4Addr::new(12, 4, 1, 8); |
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.
I believe this works too: IpAddr::from([12, 4, 1, 8])
src/connect.rs
Outdated
|
||
pub(crate) fn local_address(&self, _addr: Option<IpAddr>) { | ||
match self.inner { | ||
#[cfg(not(feature = "tls"))] |
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.
In order to support even when TLS is enabled, we'll probably need to pass the option during construction instead.
…nnector construction
let tls = try_!(tls.build()); | ||
|
||
let mut http = http_connector()?; | ||
http.set_local_address(local_addr.into()); |
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.
@seanmonstar Why is this the only place I had to use into()? The error was something along the lines of expecting std::option::Option, got type parameter
when not using into()
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.
Actually looks like some of the builds failed with that same error. I will try adding into()
to the rest.
cbdf107
to
e5fc9db
Compare
Ready for another look whenever you are available! |
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.
Woot! Thanks for putting this together!
(And excuse the wait, I was offline all last week.)
Resolves #414