summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorJanito Vaqueiro Ferreira Filho <janito@mullvad.net>2018-04-19 14:41:42 -0300
committerJanito Vaqueiro Ferreira Filho <janito@mullvad.net>2018-04-23 06:48:46 -0300
commit395ec6a724a03e3f94450ea57bb926b7dfac883c (patch)
treecb34051e3f9d2d8b4b49b80f05a321051e8f703f
parent9774b7ec97740ce5441f510903d8984fe1fd6318 (diff)
downloadmullvadvpn-395ec6a724a03e3f94450ea57bb926b7dfac883c.tar.xz
mullvadvpn-395ec6a724a03e3f94450ea57bb926b7dfac883c.zip
Reduce timeout for RPCs to master
-rw-r--r--mullvad-rpc/src/lib.rs21
1 files changed, 15 insertions, 6 deletions
diff --git a/mullvad-rpc/src/lib.rs b/mullvad-rpc/src/lib.rs
index 82f26b9f28..9cdfc69112 100644
--- a/mullvad-rpc/src/lib.rs
+++ b/mullvad-rpc/src/lib.rs
@@ -26,7 +26,7 @@ extern crate mullvad_types;
use chrono::offset::Utc;
use chrono::DateTime;
use jsonrpc_client_http::header::Host;
-use jsonrpc_client_http::HttpTransport;
+use jsonrpc_client_http::{HttpTransport, HttpTransportBuilder};
use tokio_core::reactor::Handle;
pub use jsonrpc_client_core::{Error, ErrorKind};
@@ -39,6 +39,7 @@ use mullvad_types::version;
use std::collections::HashMap;
use std::net::IpAddr;
use std::path::Path;
+use std::time::Duration;
pub mod event_loop;
pub mod rest;
@@ -50,6 +51,7 @@ mod https_client_with_sni;
use https_client_with_sni::HttpsClientWithSni;
static MASTER_API_HOST: &str = "api.mullvad.net";
+static MASTER_RPC_TIMEOUT: Duration = Duration::from_secs(5);
/// A type that helps with the creation of RPC connections.
@@ -80,8 +82,7 @@ impl MullvadRpcFactory {
/// Spawns a tokio core on a new thread and returns a `HttpHandle` running on that core.
pub fn new_connection(&mut self) -> Result<HttpHandle, HttpError> {
- let client = HttpsClientWithSni::new(MASTER_API_HOST.to_owned());
- self.setup_connection(HttpTransport::with_client(client)?)
+ self.setup_connection(HttpTransportBuilder::standalone)
}
/// Create and returns a `HttpHandle` running on the given core handle.
@@ -89,11 +90,19 @@ impl MullvadRpcFactory {
&mut self,
handle: &Handle,
) -> Result<HttpHandle, HttpError> {
- let client = HttpsClientWithSni::new(MASTER_API_HOST.to_owned());
- self.setup_connection(HttpTransport::with_client_shared(client, handle)?)
+ self.setup_connection(move |transport| transport.shared(handle))
}
- fn setup_connection(&mut self, transport: HttpTransport) -> Result<HttpHandle, HttpError> {
+ fn setup_connection<F>(&mut self, create_transport: F) -> Result<HttpHandle, HttpError>
+ where
+ F: FnOnce(HttpTransportBuilder<HttpsClientWithSni>)
+ -> jsonrpc_client_http::Result<HttpTransport>,
+ {
+ let client = HttpsClientWithSni::new(MASTER_API_HOST.to_owned());
+ let transport_builder =
+ HttpTransportBuilder::with_client(client).timeout(MASTER_RPC_TIMEOUT);
+
+ let transport = create_transport(transport_builder)?;
let mut handle = transport.handle(&self.api_uri())?;
handle.set_header(Host::new(MASTER_API_HOST, None));