From 4797298706b94691dba599f7c74112901840a083 Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Fri, 8 Jun 2018 16:10:47 -0700 Subject: [PATCH] Allow to use custom resolver for ClientConnector --- CHANGES.md | 2 ++ Cargo.toml | 2 +- src/application.rs | 4 ++-- src/client/connector.rs | 20 +++++++++++++++----- 4 files changed, 20 insertions(+), 8 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 6905d6c80..cfd5fc14a 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -10,6 +10,8 @@ * Improved failure interoperability with downcasting #285 +* Allow to use custom resolver for `ClientConnector` + ### Deprecated diff --git a/Cargo.toml b/Cargo.toml index cafb962e4..90145935c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -50,7 +50,7 @@ flate2-rust = ["flate2/rust_backend"] features = ["tls", "alpn", "session", "brotli", "flate2-c"] [dependencies] -actix = "^0.5.5" +actix = "^0.5.8" base64 = "0.9" bitflags = "1.0" diff --git a/src/application.rs b/src/application.rs index e29de6c20..106e5226f 100644 --- a/src/application.rs +++ b/src/application.rs @@ -674,7 +674,7 @@ where /// struct State2; /// /// fn main() { - /// //#### # thread::spawn(|| { + /// # thread::spawn(|| { /// server::new(|| { /// vec![ /// App::with_state(State1) @@ -689,7 +689,7 @@ where /// }).bind("127.0.0.1:8080") /// .unwrap() /// .run() - /// //#### # }); + /// # }); /// } /// ``` pub fn boxed(mut self) -> Box { diff --git a/src/client/connector.rs b/src/client/connector.rs index 6389b8972..bf9a0fefb 100644 --- a/src/client/connector.rs +++ b/src/client/connector.rs @@ -9,8 +9,8 @@ use actix::actors::{Connect as ResolveConnect, Connector, ConnectorError}; use actix::fut::WrapFuture; use actix::registry::ArbiterService; use actix::{ - fut, Actor, ActorFuture, ActorResponse, Arbiter, AsyncContext, Context, - ContextFutureSpawner, Handler, Message, Recipient, Supervised, Syn, + fut, Actor, ActorFuture, ActorResponse, Addr, Arbiter, AsyncContext, Context, + ContextFutureSpawner, Handler, Message, Recipient, Supervised, Syn, Unsync, }; use futures::task::{current as current_task, Task}; @@ -181,6 +181,7 @@ pub struct ClientConnector { pool: Rc, pool_modified: Rc>, + resolver: Addr, conn_lifetime: Duration, conn_keep_alive: Duration, limit: usize, @@ -225,6 +226,7 @@ impl Default for ClientConnector { pool: Rc::new(Pool::new(Rc::clone(&_modified))), pool_modified: _modified, connector: builder.build().unwrap(), + resolver: Connector::from_registry(), conn_lifetime: Duration::from_secs(75), conn_keep_alive: Duration::from_secs(15), limit: 100, @@ -245,6 +247,7 @@ impl Default for ClientConnector { subscriber: None, pool: Rc::new(Pool::new(Rc::clone(&_modified))), pool_modified: _modified, + resolver: Connector::from_registry(), conn_lifetime: Duration::from_secs(75), conn_keep_alive: Duration::from_secs(15), limit: 100, @@ -277,9 +280,9 @@ impl ClientConnector { /// # use std::io::Write; /// extern crate openssl; /// 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() { /// let sys = System::new("test"); @@ -312,6 +315,7 @@ impl ClientConnector { subscriber: None, pool: Rc::new(Pool::new(Rc::clone(&modified))), pool_modified: modified, + resolver: Connector::from_registry(), conn_lifetime: Duration::from_secs(75), conn_keep_alive: Duration::from_secs(15), limit: 100, @@ -371,6 +375,12 @@ impl ClientConnector { self } + /// Use custom resolver actor + pub fn resolver(mut self, addr: Addr) -> Self { + self.resolver = addr; + self + } + fn acquire(&mut self, key: &Key) -> Acquire { // check limits if self.limit > 0 { @@ -705,7 +715,7 @@ impl Handler for ClientConnector { { ActorResponse::async( - Connector::from_registry() + self.resolver .send( ResolveConnect::host_and_port(&conn.0.host, port) .timeout(conn_timeout),