diff options
| author | acdenisSK <[email protected]> | 2017-10-24 18:10:10 +0200 |
|---|---|---|
| committer | acdenisSK <[email protected]> | 2017-10-24 18:10:10 +0200 |
| commit | ef60c3cd5b93d61ff8200f5f6871b449bf7dccb5 (patch) | |
| tree | ff9ec9e09dc363c05c6b582380a120ca47290a9f /src/gateway | |
| parent | Remove `on_` prefix to EventHandler tymethods (diff) | |
| parent | Fall back to `str::parse` if `parse_username` fails (diff) | |
| download | serenity-ef60c3cd5b93d61ff8200f5f6871b449bf7dccb5.tar.xz serenity-ef60c3cd5b93d61ff8200f5f6871b449bf7dccb5.zip | |
Merge v0.4.2
Diffstat (limited to 'src/gateway')
| -rw-r--r-- | src/gateway/shard.rs | 26 |
1 files changed, 23 insertions, 3 deletions
diff --git a/src/gateway/shard.rs b/src/gateway/shard.rs index d7ffb59..3343adc 100644 --- a/src/gateway/shard.rs +++ b/src/gateway/shard.rs @@ -6,6 +6,7 @@ use std::io::Write; use std::net::Shutdown; use std::sync::Arc; use std::time::{Duration as StdDuration, Instant}; +use std::thread; use super::{ConnectionStage, GatewayError}; use websocket::client::Url; use websocket::message::{CloseData, OwnedMessage}; @@ -141,7 +142,7 @@ impl Shard { token: Arc<Mutex<String>>, shard_info: [u64; 2]) -> Result<Shard> { - let client = connect(&*ws_url.lock())?; + let client = connecting(&*ws_url.lock().unwrap()); let current_presence = (None, OnlineStatus::Online); let heartbeat_instants = (None, None); @@ -166,8 +167,8 @@ impl Shard { seq, stage, token, - session_id, shard_info, + session_id, ws_url, manager: VoiceManager::new(tx, user.id), manager_rx: rx, @@ -1131,5 +1132,24 @@ fn set_client_timeout(client: &mut WsClient) -> Result<()> { fn build_gateway_url(base: &str) -> Result<Url> { Url::parse(&format!("{}?v={}", base, constants::GATEWAY_VERSION)) - .map_err(|_| Error::Gateway(GatewayError::BuildingUrl)) + .map_err(|why| { + warn!("Error building gateway URL with base `{}`: {:?}", base, why); + + Error::Gateway(GatewayError::BuildingUrl) + }) +} + +/// Tries to connect and upon failure, retries. +fn connecting(uri: &str) -> WsClient { + let waiting_time = 30; + + loop { + match connect(&uri) { + Ok(client) => return client, + Err(why) => { + warn!("Connecting failed: {:?}\n Will retry in {} seconds.", why, waiting_time); + thread::sleep(StdDuration::from_secs(waiting_time)); + }, + }; + } } |