1
0
Fork 0
mirror of https://github.com/actix/actix-web.git synced 2024-12-18 14:16:47 +00:00

Allow to use custom resolver for ClientConnector

This commit is contained in:
Nikolay Kim 2018-06-08 16:10:47 -07:00
parent 5eaf4cbefd
commit 4797298706
4 changed files with 20 additions and 8 deletions

View file

@ -10,6 +10,8 @@
* Improved failure interoperability with downcasting #285 * Improved failure interoperability with downcasting #285
* Allow to use custom resolver for `ClientConnector`
### Deprecated ### Deprecated

View file

@ -50,7 +50,7 @@ flate2-rust = ["flate2/rust_backend"]
features = ["tls", "alpn", "session", "brotli", "flate2-c"] features = ["tls", "alpn", "session", "brotli", "flate2-c"]
[dependencies] [dependencies]
actix = "^0.5.5" actix = "^0.5.8"
base64 = "0.9" base64 = "0.9"
bitflags = "1.0" bitflags = "1.0"

View file

@ -674,7 +674,7 @@ where
/// struct State2; /// struct State2;
/// ///
/// fn main() { /// fn main() {
/// //#### # thread::spawn(|| { /// # thread::spawn(|| {
/// server::new(|| { /// server::new(|| {
/// vec![ /// vec![
/// App::with_state(State1) /// App::with_state(State1)
@ -689,7 +689,7 @@ where
/// }).bind("127.0.0.1:8080") /// }).bind("127.0.0.1:8080")
/// .unwrap() /// .unwrap()
/// .run() /// .run()
/// //#### # }); /// # });
/// } /// }
/// ``` /// ```
pub fn boxed(mut self) -> Box<HttpHandler> { pub fn boxed(mut self) -> Box<HttpHandler> {

View file

@ -9,8 +9,8 @@ use actix::actors::{Connect as ResolveConnect, Connector, ConnectorError};
use actix::fut::WrapFuture; use actix::fut::WrapFuture;
use actix::registry::ArbiterService; use actix::registry::ArbiterService;
use actix::{ use actix::{
fut, Actor, ActorFuture, ActorResponse, Arbiter, AsyncContext, Context, fut, Actor, ActorFuture, ActorResponse, Addr, Arbiter, AsyncContext, Context,
ContextFutureSpawner, Handler, Message, Recipient, Supervised, Syn, ContextFutureSpawner, Handler, Message, Recipient, Supervised, Syn, Unsync,
}; };
use futures::task::{current as current_task, Task}; use futures::task::{current as current_task, Task};
@ -181,6 +181,7 @@ pub struct ClientConnector {
pool: Rc<Pool>, pool: Rc<Pool>,
pool_modified: Rc<Cell<bool>>, pool_modified: Rc<Cell<bool>>,
resolver: Addr<Unsync, Connector>,
conn_lifetime: Duration, conn_lifetime: Duration,
conn_keep_alive: Duration, conn_keep_alive: Duration,
limit: usize, limit: usize,
@ -225,6 +226,7 @@ impl Default for ClientConnector {
pool: Rc::new(Pool::new(Rc::clone(&_modified))), pool: Rc::new(Pool::new(Rc::clone(&_modified))),
pool_modified: _modified, pool_modified: _modified,
connector: builder.build().unwrap(), connector: builder.build().unwrap(),
resolver: Connector::from_registry(),
conn_lifetime: Duration::from_secs(75), conn_lifetime: Duration::from_secs(75),
conn_keep_alive: Duration::from_secs(15), conn_keep_alive: Duration::from_secs(15),
limit: 100, limit: 100,
@ -245,6 +247,7 @@ impl Default for ClientConnector {
subscriber: None, subscriber: None,
pool: Rc::new(Pool::new(Rc::clone(&_modified))), pool: Rc::new(Pool::new(Rc::clone(&_modified))),
pool_modified: _modified, pool_modified: _modified,
resolver: Connector::from_registry(),
conn_lifetime: Duration::from_secs(75), conn_lifetime: Duration::from_secs(75),
conn_keep_alive: Duration::from_secs(15), conn_keep_alive: Duration::from_secs(15),
limit: 100, limit: 100,
@ -277,9 +280,9 @@ impl ClientConnector {
/// # use std::io::Write; /// # use std::io::Write;
/// extern crate openssl; /// extern crate openssl;
/// use actix::prelude::*; /// use actix::prelude::*;
/// use actix_web::client::{Connect, ClientConnector}; /// use actix_web::client::{ClientConnector, Connect};
/// ///
/// use openssl::ssl::{SslMethod, SslConnector}; /// use openssl::ssl::{SslConnector, SslMethod};
/// ///
/// fn main() { /// fn main() {
/// let sys = System::new("test"); /// let sys = System::new("test");
@ -312,6 +315,7 @@ impl ClientConnector {
subscriber: None, subscriber: None,
pool: Rc::new(Pool::new(Rc::clone(&modified))), pool: Rc::new(Pool::new(Rc::clone(&modified))),
pool_modified: modified, pool_modified: modified,
resolver: Connector::from_registry(),
conn_lifetime: Duration::from_secs(75), conn_lifetime: Duration::from_secs(75),
conn_keep_alive: Duration::from_secs(15), conn_keep_alive: Duration::from_secs(15),
limit: 100, limit: 100,
@ -371,6 +375,12 @@ impl ClientConnector {
self self
} }
/// Use custom resolver actor
pub fn resolver(mut self, addr: Addr<Unsync, Connector>) -> Self {
self.resolver = addr;
self
}
fn acquire(&mut self, key: &Key) -> Acquire { fn acquire(&mut self, key: &Key) -> Acquire {
// check limits // check limits
if self.limit > 0 { if self.limit > 0 {
@ -705,7 +715,7 @@ impl Handler<Connect> for ClientConnector {
{ {
ActorResponse::async( ActorResponse::async(
Connector::from_registry() self.resolver
.send( .send(
ResolveConnect::host_and_port(&conn.0.host, port) ResolveConnect::host_and_port(&conn.0.host, port)
.timeout(conn_timeout), .timeout(conn_timeout),