Skip to content

Commit

Permalink
Add fibonacci backoff on reconnect
Browse files Browse the repository at this point in the history
  • Loading branch information
boxdot committed Dec 19, 2023
1 parent d6ba990 commit 1a79905
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 2 deletions.
2 changes: 1 addition & 1 deletion benches/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use gurk::app::App;
use gurk::config::{Config, User};
use gurk::signal::test::SignalManagerMock;
use gurk::storage::{ForgetfulStorage, MemCache};
use presage::prelude::Content;
use presage::libsignal_service::content::Content;
use tracing::info;

fn test_app() -> App {
Expand Down
36 changes: 36 additions & 0 deletions src/backoff.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
use std::time::Duration;

const FIBONACCI_TIMEOUTS: [Duration; 9] = [
Duration::from_secs(1),
Duration::from_secs(2),
Duration::from_secs(3),
Duration::from_secs(5),
Duration::from_secs(8),
Duration::from_secs(13),
Duration::from_secs(21),
Duration::from_secs(34),
Duration::from_secs(55),
];

#[derive(Debug, Default)]
pub struct Backoff {
count: usize,
}

impl Backoff {
pub fn new() -> Self {
Default::default()
}

pub fn get(&mut self) -> Duration {
let timeout = FIBONACCI_TIMEOUTS[self.count];
if self.count + 1 < FIBONACCI_TIMEOUTS.len() {
self.count += 1;
}
timeout
}

pub fn reset(&mut self) {
self.count = 0;
}
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Signal Messenger client for terminal

pub mod app;
pub mod backoff;
mod channels;
pub mod config;
pub mod cursor;
Expand Down
8 changes: 7 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use crossterm::{
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
};
use gurk::app::App;
use gurk::backoff::Backoff;
use gurk::storage::{sync_from_signal, JsonStorage, MemCache, SqliteStorage, Storage};
use gurk::{config, signal, ui};
use presage::libsignal_service::content::Content;
Expand Down Expand Up @@ -155,6 +156,7 @@ async fn run_single_threaded(relink: bool) -> anyhow::Result<()> {

let inner_tx = tx.clone();
tokio::task::spawn_local(async move {
let mut backoff = Backoff::new();
loop {
let mut messages = if !is_online().await {
tokio::time::sleep(std::time::Duration::from_secs(10)).await;
Expand Down Expand Up @@ -195,12 +197,16 @@ async fn run_single_threaded(relink: bool) -> anyhow::Result<()> {
}

while let Some(message) = messages.next().await {
backoff.reset();
inner_tx
.send(Event::Message(message))
.await
.expect("logic error: events channel closed")
}
error!("messages channel disconnected. trying to reconnect.")

let after = backoff.get();
error!(?after, "messages channel disconnected. trying to reconnect");
tokio::time::sleep(after).await;
}
});

Expand Down

0 comments on commit 1a79905

Please sign in to comment.