Skip to content

Commit

Permalink
Reboot shard on broken pipe
Browse files Browse the repository at this point in the history
If the receiver or sender breaks the pipe for one reason or another,
shutdown both. Afterwards, close down the keepalive and perform a
reboot of the shard.
  • Loading branch information
Austin Hellyer committed Jan 20, 2017
1 parent 38db32e commit 76f9095
Show file tree
Hide file tree
Showing 3 changed files with 211 additions and 108 deletions.
1 change: 1 addition & 0 deletions src/client/error.rs
Expand Up @@ -122,6 +122,7 @@ pub enum Error {
///
/// [`Context::edit_role`]: struct.Context.html#method.edit_role
RecordNotFound,
ShardBootFailure,
/// When the shard being retrieved from within the Client could not be
/// found after being inserted into the Client's internal vector of
/// [`Shard`]s.
Expand Down
19 changes: 16 additions & 3 deletions src/client/gateway/prep.rs
@@ -1,6 +1,5 @@
use serde_json::builder::ObjectBuilder;
use serde_json::Value;
use std::net::Shutdown;
use std::sync::mpsc::{
Receiver as MpscReceiver,
Sender as MpscSender,
Expand Down Expand Up @@ -96,6 +95,7 @@ pub fn keepalive(interval: u64,
let mut next_tick = time::get_time() + base_interval;

let mut last_sequence = 0;
let mut last_successful = false;

'outer: loop {
thread::sleep(StdDuration::from_millis(100));
Expand Down Expand Up @@ -137,12 +137,25 @@ pub fn keepalive(interval: u64,

*heartbeat_sent.lock().unwrap() = now;
},
Err(why) => warn!("Error sending keepalive: {:?}", why),
Err(why) => {
warn!("Error sending keepalive: {:?}", why);

if last_successful {
debug!("If next keepalive fails, closing");
} else {
break;
}

last_successful = false;
},
}
}
}

debug!("Closing keepalive");

let _ = sender.get_mut().shutdown(Shutdown::Both);
match sender.shutdown_all() {
Ok(_) => debug!("Successfully shutdown sender/receiver"),
Err(why) => warn!("Failed to shutdown sender/receiver: {:?}", why),
}
}

0 comments on commit 76f9095

Please sign in to comment.