Add Global::get_retry_after.

This commit is contained in:
levlam 2022-04-12 16:12:22 +03:00
parent 7a2b3abe18
commit 877452f6ce
3 changed files with 22 additions and 6 deletions

View File

@ -135,6 +135,23 @@ Status Global::init(const TdParameters &parameters, ActorId<Td> td, unique_ptr<T
return Status::OK();
}
int32 Global::get_retry_after(int32 error_code, Slice error_message) {
if (error_code != 429) {
return 0;
}
Slice retry_after_prefix("Too Many Requests: retry after ");
if (!begins_with(error_message, retry_after_prefix)) {
return 0;
}
auto r_retry_after = to_integer_safe<int32>(error_message.substr(retry_after_prefix.size()));
if (r_retry_after.is_ok() && r_retry_after.ok() > 0) {
return r_retry_after.ok();
}
return 0;
}
int32 Global::to_unix_time(double server_time) const {
LOG_CHECK(1.0 <= server_time && server_time <= 2140000000.0)
<< server_time << ' ' << Clocks::system() << ' ' << is_server_time_reliable() << ' '

View File

@ -420,6 +420,8 @@ class Global final : public ActorContext {
return close_flag();
}
static int32 get_retry_after(int32 error_code, Slice error_message);
const std::vector<std::shared_ptr<NetStatsCallback>> &get_net_stats_file_callbacks() {
return net_stats_file_callbacks_;
}

View File

@ -31147,12 +31147,9 @@ void MessagesManager::fail_send_message(FullMessageId full_message_id, int error
message->send_error_code = error_code;
message->send_error_message = error_message;
message->try_resend_at = 0.0;
Slice retry_after_prefix("Too Many Requests: retry after ");
if (error_code == 429 && begins_with(error_message, retry_after_prefix)) {
auto r_retry_after = to_integer_safe<int32>(error_message.substr(retry_after_prefix.size()));
if (r_retry_after.is_ok() && r_retry_after.ok() > 0) {
message->try_resend_at = Time::now() + r_retry_after.ok();
}
auto retry_after = Global::get_retry_after(error_code, error_message);
if (retry_after > 0) {
message->try_resend_at = Time::now() + retry_after;
}
update_failed_to_send_message_content(td_, message->content);