Add Client::get_retry_after_time helper.

This commit is contained in:
levlam 2022-12-11 23:26:23 +03:00
parent a497eb5fcd
commit 66b5d8aa07
2 changed files with 16 additions and 6 deletions

View File

@ -42,16 +42,24 @@ using td::JsonValue;
using td_api::make_object;
using td_api::move_object_as;
void Client::fail_query_with_error(PromisedQueryPtr query, int32 error_code, Slice error_message,
Slice default_message) {
if (error_code == 429) {
int Client::get_retry_after_time(Slice error_message) {
Slice prefix = "Too Many Requests: retry after ";
if (begins_with(error_message, prefix)) {
auto r_retry_after = td::to_integer_safe<int>(error_message.substr(prefix.size()));
if (r_retry_after.is_ok() && r_retry_after.ok() > 0) {
return query->set_retry_after_error(r_retry_after.ok());
return r_retry_after.ok();
}
}
return 0;
}
void Client::fail_query_with_error(PromisedQueryPtr query, int32 error_code, Slice error_message,
Slice default_message) {
if (error_code == 429) {
auto retry_after_time = get_retry_after_time(error_message);
if (retry_after_time > 0) {
return query->set_retry_after_error(retry_after_time);
}
LOG(ERROR) << "Wrong error message: " << error_message << " from " << *query;
return fail_query(500, error_message, std::move(query));
}

View File

@ -619,6 +619,8 @@ class Client final : public WebhookActor::Callback {
void fail_query_conflict(Slice message, PromisedQueryPtr &&query);
static int get_retry_after_time(Slice error_message);
static void fail_query_with_error(PromisedQueryPtr query, int32 error_code, Slice error_message,
Slice default_message = Slice());