-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: send periodic pings for keep alives (#103)
#93 & #94 added a notion of keep alives to graphql-ws-client. But the implementation caused the connection to drop whenever no messages were received for the specified period. This isn't how I'd usually expect a keep alive to work though - I'd usually expect it to send pings during inactive periods, and only drop the connection if a few of those pings were not replied to. This PR implements that behaviour instead. It's a slightly breaking change over the earlier implementation, but that was not yet released so that seems fine.
- Loading branch information
Showing
4 changed files
with
122 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
use std::{future::pending, time::Duration}; | ||
|
||
use futures::Stream; | ||
|
||
use crate::ConnectionCommand; | ||
|
||
#[derive(Clone)] | ||
pub(super) struct KeepAliveSettings { | ||
/// How often to send a keep alive ping | ||
pub(super) interval: Option<Duration>, | ||
|
||
/// How many pings can be sent without receiving a reply before | ||
/// the connection is considered dropped | ||
pub(super) retries: usize, | ||
} | ||
|
||
impl Default for KeepAliveSettings { | ||
fn default() -> Self { | ||
Self { | ||
interval: None, | ||
retries: 3, | ||
} | ||
} | ||
} | ||
|
||
enum KeepAliveState { | ||
Running, | ||
StartedKeepAlive, | ||
TimingOut { failure_count: usize }, | ||
} | ||
|
||
impl KeepAliveSettings { | ||
pub(super) fn run(&self) -> impl Stream<Item = ConnectionCommand> + 'static { | ||
let settings = self.clone(); | ||
|
||
futures::stream::unfold(KeepAliveState::Running, move |mut state| async move { | ||
match settings.interval { | ||
Some(duration) => futures_timer::Delay::new(duration).await, | ||
None => pending::<()>().await, | ||
} | ||
|
||
match state { | ||
KeepAliveState::Running => { | ||
state = KeepAliveState::StartedKeepAlive; | ||
} | ||
KeepAliveState::StartedKeepAlive => { | ||
state = KeepAliveState::TimingOut { failure_count: 0 }; | ||
} | ||
KeepAliveState::TimingOut { failure_count } => { | ||
state = KeepAliveState::TimingOut { | ||
failure_count: failure_count + 1, | ||
}; | ||
} | ||
} | ||
|
||
if state.failure_count() > settings.retries { | ||
// returning None aborts | ||
return None; | ||
} | ||
|
||
Some((ConnectionCommand::Ping, state)) | ||
}) | ||
} | ||
} | ||
|
||
impl KeepAliveState { | ||
pub fn failure_count(&self) -> usize { | ||
match self { | ||
KeepAliveState::Running | KeepAliveState::StartedKeepAlive => 0, | ||
KeepAliveState::TimingOut { failure_count } => *failure_count, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters