-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Client feature proposal: set_local_address #1498
Comments
Hey, thanks for the detailed write-up! It really helps me to understand what the end goal is. In this case, it seems like a fine feature for an advanced configuration to allow binding to a local address before connecting to a remote. Some jumbled thoughts:
|
Thanks @seanmonstar. Yes, making it an option on the connector makes lots of sense, so it looks like this: let bind_addr: Option<IpAddr> = Some("127.0.0.1".parse().unwrap());
let mut connector = HttpConnector::new(4);
connector.set_local_address(bind_addr);
let client = Client::builder().build(connector); Aha, I see now that you've upgraded away from However there is one remaining problem - where previously I had to allocate, now I think there's a case that isn't expressible at all. |
Yes, calling |
Add `set_local_address` to the `HttpConnector`. This configures the client to bind the socket to a local address of the host before it connects to the destination. This is useful on hosts which have multiple network interfaces, to ensure the request is issued over a specific interface. Closes hyperium#1498
Add `set_local_address` to the `HttpConnector`. This configures the client to bind the socket to a local address of the host before it connects to the destination. This is useful on hosts which have multiple network interfaces, to ensure the request is issued over a specific interface. Closes hyperium#1498
Add `set_local_address` to the `HttpConnector`. This configures the client to bind the socket to a local address of the host before it connects to the destination. This is useful on hosts which have multiple network interfaces, to ensure the request is issued over a specific interface. Closes hyperium#1498
Add `set_local_address` to the `HttpConnector`. This configures the client to bind the socket to a local address of the host before it connects to the destination. This is useful on hosts which have multiple network interfaces, to ensure the request is issued over a specific interface. Closes hyperium#1498
Add `set_local_address` to the `HttpConnector`. This configures the client to bind the socket to a local address of the host before it connects to the destination. This is useful on hosts which have multiple network interfaces, to ensure the request is issued over a specific interface. Closes hyperium#1498
Add `set_local_address` to the `HttpConnector`. This configures the client to bind the socket to a local address of the host before it connects to the destination. This is useful on hosts which have multiple network interfaces, to ensure the request is issued over a specific interface. Closes hyperium#1498
Hi,
I am using the Hyper client on a host which has multiple network interfaces. I would like to issue an HTTP request over a specific interface. The syntax should be something like this:
Below I propose:
Would you be happy to receive a PR along these lines, and do you think the approach of enhancing mio and tokio-core is best, or should we simply change hyper?
Details follow.
Uncontroversially we need the following plumbing:
client/mod.rs
we addlocal_address: Option<IpAddr>
toConfig
, default it toNone
, addset_local_address
, and makebuild
set it onHttpConnector
if present.local_address: Option<IpAddr>
to the fields ofHttpConnector
and provide a setter.Lazy
andResolving
states ofHttpConnecting
gain an extra tuple fieldOption<IpAddr>
, andConnectingTcp
gains alocal_address
field. The same will also be required in the hyper-tls connector.The interesting part is what we replace
self.current = tokio::net::TcpStream::connect(&addr, handle);
with inConnectingTcp
.Doing it all purely in Hyper looks like this (replacing a few lines of
impl ConnectingTcp
around https://github.com/hyperium/hyper/blob/master/src/client/connect.rs#L426):This direct approach is unpleasant because it violates the architectural layering and duplicates code. In particular, this code is taken almost verbatim from
mio::net::TcpStream::connect()
– and as you can see there, it should really include a special Windows workaround here.It also loses the zero-allocation property: Tokio’s
TcpStream::connect_stream
can only return aBox<Future>
, so we have to change ConnectingTcp to use this boxed future rather than the bare TokioTcpStreamNew
(which we cannot construct outside Tokio).How could we preserve the architectural layering?
In
mio::net::TcpStream
we should add aconnect_with_local_address
method besideconnect
, to encapsulate the Windows and socket knowledge. We should then expose that in a newtokio::net::TcpStream::connect_with_local_address
method. Then this method is available to be used Hyper’sConnectingTcp
:I think this a cleaner approach architecturally, and it is also able to preserve zero-allocation. But it does involve making changes to three projects. Which is the best approach?
The text was updated successfully, but these errors were encountered: