From c4ac0d0570151381a12de436804bbe31704735e0 Mon Sep 17 00:00:00 2001 From: strct Date: Sat, 10 Oct 2020 15:57:23 +0200 Subject: [PATCH] matrix-sdk: example: retry autojoin --- matrix_sdk/examples/autojoin.rs | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/matrix_sdk/examples/autojoin.rs b/matrix_sdk/examples/autojoin.rs index 2af9a61b..ca757244 100644 --- a/matrix_sdk/examples/autojoin.rs +++ b/matrix_sdk/examples/autojoin.rs @@ -1,4 +1,5 @@ use std::{env, process::exit}; +use tokio::time::{delay_for, Duration}; use matrix_sdk::{ self, @@ -32,11 +33,23 @@ impl EventEmitter for AutoJoinBot { if let SyncRoom::Invited(room) = room { let room = room.read().await; - println!("Autojoining room {}", room.display_name()); - self.client - .join_room_by_id(&room.room_id) - .await - .expect("Can't join room"); + println!("Autojoining room {}", room.room_id); + let mut delay = 2; + while let Err(err) = self.client.join_room_by_id(&room.room_id).await { + // retry autojoin due to synapse sending invites, before the invited user can join + // for more information see https://github.com/matrix-org/synapse/issues/4345 + eprintln!( + "Failed to join room {} ({:?}), retrying in {}s", + room.room_id, err, delay + ); + delay_for(Duration::from_secs(delay)).await; + delay *= 2; + if delay > 3600 { + eprintln!("Can't join room {} ({:?})", room.room_id, err); + break; + } + } + println!("Successfully joined room {}", room.room_id); } } }