Skip to content

Commit

Permalink
Retry login after intervals specified by the server
Browse files Browse the repository at this point in the history
Sleep for the duration indicated by the server on M_LIMIT_EXCEEDED
responses, or immediately if no time specified.
  • Loading branch information
aperezdc committed Jan 31, 2025
1 parent 738d6e0 commit cd93269
Showing 1 changed file with 40 additions and 6 deletions.
46 changes: 40 additions & 6 deletions src/bot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ use matrix_sdk::ruma::events::AnyTimelineEvent;
use matrix_sdk::ruma::{EventId, OwnedMxcUri, RoomId, ServerName, UserId};
use matrix_sdk::{Client, Room, RoomState};

use matrix_sdk::ruma::api::error::FromHttpResponseError;
use matrix_sdk::HttpError::Api;
use matrix_sdk::RumaApiError::ClientApi;

use regex::Regex;

use std::env;
Expand Down Expand Up @@ -128,12 +132,42 @@ impl Bot {
/// Login
async fn login(client: &Client, user: &str, pwd: &str) {
info!("Logging in…");
let response = client
.matrix_auth()
.login_username(user, pwd)
.initial_device_display_name("hebbot")
.await
.expect("Unable to login");
let response = loop {
let response = client
.matrix_auth()
.login_username(user, pwd)
.initial_device_display_name("hebbot")
.await;
match response {
Ok(r) => break r,
Err(matrix_sdk::Error::Http(Api(FromHttpResponseError::Server(ClientApi(e)))))
if e.status_code == 429 =>
{
if let matrix_sdk::ruma::api::client::error::ErrorBody::Standard {
kind:
matrix_sdk::ruma::api::client::error::ErrorKind::LimitExceeded {
retry_after_ms,
},
..
} = e.body
{
if let Some(retry_after_ms) = retry_after_ms {
let sleep_duration =
retry_after_ms + tokio::time::Duration::from_millis(250);
debug!("Sleeping for {sleep_duration:?} before retrying login…");
tokio::time::sleep(sleep_duration).await;
} else {
debug!("No wait time given, retrying right away");
}
continue;
}
panic!("Error: {e:?}");
}
Err(e) => {
panic!("Error: {e:?}");
}
}
};

info!("Doing the initial sync…");
client
Expand Down

0 comments on commit cd93269

Please sign in to comment.