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 21d182e
Showing 1 changed file with 32 additions and 6 deletions.
38 changes: 32 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::RumaApiError::ClientApi;
use matrix_sdk::HttpError::Api;

use regex::Regex;

use std::env;
Expand Down Expand Up @@ -128,12 +132,34 @@ 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 { ref kind, .. } = e.body {
if let matrix_sdk::ruma::api::client::error::ErrorKind::LimitExceeded { retry_after_ms } = kind {
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;
}

Check failure on line 154 in src/bot.rs

View workflow job for this annotation

GitHub Actions / clippy

cargo-clippy: this `if let` can be collapsed into the outer `if let`

error: this `if let` can be collapsed into the outer `if let` --> src/bot.rs:145:25 | 145 | / if let matrix_sdk::ruma::api::client::error::ErrorKind::LimitExceeded { retry_after_ms } = kind { 146 | | if let Some(retry_after_ms) = retry_after_ms { 147 | | let sleep_duration = *retry_after_ms + tokio::time::Duration::from_millis(250); 148 | | debug!("Sleeping for {sleep_duration:?} before retrying login…"); ... | 153 | | continue; 154 | | } | |_________________________^ | help: the outer pattern can be modified to include the inner pattern --> src/bot.rs:144:88 | 144 | if let matrix_sdk::ruma::api::client::error::ErrorBody::Standard { ref kind, .. } = e.body { | ^^^^^^^^ replace this binding 145 | if let matrix_sdk::ruma::api::client::error::ErrorKind::LimitExceeded { retry_after_ms } = kind { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ with this pattern, prefixed by `ref kind`: = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#collapsible_match = note: `-D clippy::collapsible-match` implied by `-D warnings` = help: to override `-D warnings` add `#[allow(clippy::collapsible_match)]`
}
panic!("Error: {e:?}");
},
Err(e) => {
panic!("Error: {e:?}");
}
}
};

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

0 comments on commit 21d182e

Please sign in to comment.