diff options
| author | Austin Hellyer <[email protected]> | 2016-11-29 08:02:35 -0800 |
|---|---|---|
| committer | Austin Hellyer <[email protected]> | 2016-11-29 08:02:35 -0800 |
| commit | 018684e3a562ac4f09fb92783d8fd06d679a2ce1 (patch) | |
| tree | e32c1ff9542dc30461dffec1dd2dbc219e0023f8 /src | |
| parent | Retrieve a new URL on reconnect (diff) | |
| download | serenity-018684e3a562ac4f09fb92783d8fd06d679a2ce1.tar.xz serenity-018684e3a562ac4f09fb92783d8fd06d679a2ce1.zip | |
Remove duplicated gateway logic
Diffstat (limited to 'src')
| -rw-r--r-- | src/client/gateway/error.rs | 5 | ||||
| -rw-r--r-- | src/client/gateway/shard.rs | 30 | ||||
| -rw-r--r-- | src/client/mod.rs | 13 |
3 files changed, 11 insertions, 37 deletions
diff --git a/src/client/gateway/error.rs b/src/client/gateway/error.rs index dc94209..3e355f5 100644 --- a/src/client/gateway/error.rs +++ b/src/client/gateway/error.rs @@ -12,6 +12,8 @@ pub enum Error { ExpectedHello, /// Expected a Ready or an InvalidateSession InvalidHandshake, + /// Failed to reconnect after a number of attempts. + ReconnectFailure, } impl Display for Error { @@ -26,6 +28,9 @@ impl Display for Error { Error::InvalidHandshake => { f.write_str("Expected Ready or InvalidateSession") }, + Error::ReconnectFailure => { + f.write_str("Failed to Reconnect") + }, } } } diff --git a/src/client/gateway/shard.rs b/src/client/gateway/shard.rs index f7e1bde..ead7fd0 100644 --- a/src/client/gateway/shard.rs +++ b/src/client/gateway/shard.rs @@ -432,9 +432,8 @@ impl Shard { -> Result<(Event, Receiver<WebSocketStream>)> { debug!("Reconnecting"); - // Take a few attempts at reconnecting; otherwise fall back to - // re-instantiating the connection. - for _ in 0..3 { + // Take a few attempts at reconnecting. + for i in 1u64..11u64 { let gateway_url = try!(rest::get_gateway()).url; let shard = Shard::new(&gateway_url, @@ -450,29 +449,12 @@ impl Shard { return Ok((Event::Ready(ready), receiver_new)); } - thread::sleep(StdDuration::from_secs(1)); + // Exponentially back off. + thread::sleep(StdDuration::from_secs(i.pow(2))); } - // If all else fails: get a new endpoint. - // - // A bit of complexity here: instantiate a temporary instance of a - // Client. This client _does not_ replace the current client(s) that the - // user has. This client will then connect to gateway. This new - // shard will be used to replace _this_ shard. - let (shard, ready, receiver_new) = { - let mut client = Client::login_raw(&self.token.clone(), - self.login_type); - - try!(client.boot_shard(self.shard_info)) - }; - - // Replace this shard with a new one, and shutdown the now-old - // shard. - try!(mem::replace(self, shard).shutdown(&mut receiver)); - - self.session_id = Some(ready.ready.session_id.clone()); - - Ok((Event::Ready(ready), receiver_new)) + // Reconnecting failed; just return an error instead. + Err(Error::Gateway(GatewayError::ReconnectFailure)) } fn resume(&mut self, session_id: String, receiver: &mut Receiver<WebSocketStream>) diff --git a/src/client/mod.rs b/src/client/mod.rs index 21b74d4..7248405 100644 --- a/src/client/mod.rs +++ b/src/client/mod.rs @@ -819,19 +819,6 @@ impl Client { thread::sleep(Duration::from_secs(1)); } } - - // Boot up a new shard. This is used primarily in the scenario of - // re-instantiating a shard in the reconnect logic in another [`Shard`]. - // - // [`Shard`]: gateway/struct.Shard.html - #[doc(hidden)] - pub fn boot_shard(&mut self, - shard_info: Option<[u8; 2]>) - -> Result<(Shard, ReadyEvent, Receiver<WebSocketStream>)> { - let gateway_url = try!(rest::get_gateway()).url; - - Shard::new(&gateway_url, &self.token, shard_info, self.login_type) - } } #[cfg(feature = "cache")] |