mirror of
https://github.com/fafhrd91/actix-web
synced 2025-06-25 06:39:22 +02:00
add local_address bind for client builder (#2024)
This commit is contained in:
@ -1,3 +1,4 @@
|
||||
use std::net::IpAddr;
|
||||
use std::time::Duration;
|
||||
|
||||
const DEFAULT_H2_CONN_WINDOW: u32 = 1024 * 1024 * 2; // 2MB
|
||||
@ -13,6 +14,7 @@ pub(crate) struct ConnectorConfig {
|
||||
pub(crate) limit: usize,
|
||||
pub(crate) conn_window_size: u32,
|
||||
pub(crate) stream_window_size: u32,
|
||||
pub(crate) local_address: Option<IpAddr>,
|
||||
}
|
||||
|
||||
impl Default for ConnectorConfig {
|
||||
@ -25,6 +27,7 @@ impl Default for ConnectorConfig {
|
||||
limit: 100,
|
||||
conn_window_size: DEFAULT_H2_CONN_WINDOW,
|
||||
stream_window_size: DEFAULT_H2_STREAM_WINDOW,
|
||||
local_address: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,9 +1,12 @@
|
||||
use std::fmt;
|
||||
use std::future::Future;
|
||||
use std::marker::PhantomData;
|
||||
use std::pin::Pin;
|
||||
use std::task::{Context, Poll};
|
||||
use std::time::Duration;
|
||||
use std::{
|
||||
fmt,
|
||||
future::Future,
|
||||
marker::PhantomData,
|
||||
net::IpAddr,
|
||||
pin::Pin,
|
||||
task::{Context, Poll},
|
||||
time::Duration,
|
||||
};
|
||||
|
||||
use actix_codec::{AsyncRead, AsyncWrite};
|
||||
use actix_rt::net::TcpStream;
|
||||
@ -240,6 +243,12 @@ where
|
||||
self
|
||||
}
|
||||
|
||||
/// Set local IP Address the connector would use for establishing connection.
|
||||
pub fn local_address(mut self, addr: IpAddr) -> Self {
|
||||
self.config.local_address = Some(addr);
|
||||
self
|
||||
}
|
||||
|
||||
/// Finish configuration process and create connector service.
|
||||
/// The Connector builder always concludes by calling `finish()` last in
|
||||
/// its combinator chain.
|
||||
@ -247,10 +256,19 @@ where
|
||||
self,
|
||||
) -> impl Service<Connect, Response = impl Connection, Error = ConnectError> + Clone
|
||||
{
|
||||
let local_address = self.config.local_address;
|
||||
let timeout = self.config.timeout;
|
||||
|
||||
let tcp_service = TimeoutService::new(
|
||||
self.config.timeout,
|
||||
apply_fn(self.connector.clone(), |msg: Connect, srv| {
|
||||
srv.call(TcpConnect::new(msg.uri).set_addr(msg.addr))
|
||||
timeout,
|
||||
apply_fn(self.connector.clone(), move |msg: Connect, srv| {
|
||||
let mut req = TcpConnect::new(msg.uri).set_addr(msg.addr);
|
||||
|
||||
if let Some(local_addr) = local_address {
|
||||
req = req.set_local_addr(local_addr);
|
||||
}
|
||||
|
||||
srv.call(req)
|
||||
})
|
||||
.map_err(ConnectError::from)
|
||||
.map(|stream| (stream.into_parts().0, Protocol::Http1)),
|
||||
@ -294,10 +312,16 @@ where
|
||||
use actix_tls::connect::ssl::rustls::{RustlsConnector, Session};
|
||||
|
||||
let ssl_service = TimeoutService::new(
|
||||
self.config.timeout,
|
||||
timeout,
|
||||
pipeline(
|
||||
apply_fn(self.connector.clone(), |msg: Connect, srv| {
|
||||
srv.call(TcpConnect::new(msg.uri).set_addr(msg.addr))
|
||||
apply_fn(self.connector.clone(), move |msg: Connect, srv| {
|
||||
let mut req = TcpConnect::new(msg.uri).set_addr(msg.addr);
|
||||
|
||||
if let Some(local_addr) = local_address {
|
||||
req = req.set_local_addr(local_addr);
|
||||
}
|
||||
|
||||
srv.call(req)
|
||||
})
|
||||
.map_err(ConnectError::from),
|
||||
)
|
||||
|
Reference in New Issue
Block a user