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