Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Retry login after intervals specified by the server #94

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 41 additions & 7 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 Expand Up @@ -859,7 +893,7 @@ impl Bot {
async fn restart_command(&self) {
self.send_message("Restarting hebbot…", BotMsgType::AdminRoomPlainNotice)
.await;
Command::new("/proc/self/exe").exec();
let _ = Command::new("/proc/self/exe").exec();
}

async fn say_command(&self, msg: &str) {
Expand Down