Skip to content

Commit

Permalink
Merge pull request #1553 from famedly/nico/handle-send-event-ratelimits
Browse files Browse the repository at this point in the history
fix: Wait for rate limit to pass while sending messages
  • Loading branch information
nico-famedly authored Sep 22, 2023
2 parents fa73ab3 + 97bf772 commit 8b6bc12
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions lib/src/room.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1100,17 +1100,28 @@ class Room {
txid: messageID,
);
} catch (e, s) {
if (e is MatrixException || DateTime.now().isAfter(timeoutDate)) {
if (e is MatrixException &&
e.retryAfterMs != null &&
!DateTime.now()
.add(Duration(milliseconds: e.retryAfterMs!))
.isAfter(timeoutDate)) {
Logs().w(
'Ratelimited while sending message, waiting for ${e.retryAfterMs}ms');
await Future.delayed(Duration(milliseconds: e.retryAfterMs!));
} else if (e is MatrixException ||
DateTime.now().isAfter(timeoutDate)) {
Logs().w('Problem while sending message', e, s);
syncUpdate.rooms!.join!.values.first.timeline!.events!.first
.unsigned![messageSendingStatusKey] = EventStatus.error.intValue;
await _handleFakeSync(syncUpdate);
completer.complete();
_sendingQueue.remove(completer);
return null;
} else {
Logs()
.w('Problem while sending message: $e Try again in 1 seconds...');
await Future.delayed(Duration(seconds: 1));
}
Logs().w('Problem while sending message: $e Try again in 1 seconds...');
await Future.delayed(Duration(seconds: 1));
}
}
syncUpdate.rooms!.join!.values.first.timeline!.events!.first
Expand Down

0 comments on commit 8b6bc12

Please sign in to comment.