2018-12-31 20:04:05 +01:00
|
|
|
//
|
2022-01-01 01:35:39 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2022
|
2018-12-31 20:04:05 +01:00
|
|
|
//
|
|
|
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
//
|
|
|
|
#include "td/telegram/net/NetQueryDelayer.h"
|
|
|
|
|
|
|
|
#include "td/telegram/Global.h"
|
|
|
|
#include "td/telegram/net/NetQueryDispatcher.h"
|
|
|
|
|
2019-02-12 22:26:36 +01:00
|
|
|
#include "td/utils/common.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/format.h"
|
|
|
|
#include "td/utils/logging.h"
|
|
|
|
#include "td/utils/misc.h"
|
|
|
|
#include "td/utils/Slice.h"
|
2021-05-17 14:21:11 +02:00
|
|
|
#include "td/utils/SliceBuilder.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/Status.h"
|
|
|
|
|
|
|
|
namespace td {
|
2018-04-19 19:21:26 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
void NetQueryDelayer::delay(NetQueryPtr query) {
|
2018-10-07 22:40:58 +02:00
|
|
|
query->debug("trying to delay");
|
2018-12-31 20:04:05 +01:00
|
|
|
query->is_ready();
|
|
|
|
CHECK(query->is_error());
|
|
|
|
auto code = query->error().code();
|
2022-02-04 15:05:35 +01:00
|
|
|
int32 timeout = 0;
|
2018-12-26 19:01:46 +01:00
|
|
|
if (code < 0) {
|
2018-12-31 20:04:05 +01:00
|
|
|
// skip
|
2018-12-26 19:01:46 +01:00
|
|
|
} else if (code == 500) {
|
2021-10-23 20:17:00 +02:00
|
|
|
auto error_message = query->error().message();
|
|
|
|
if (error_message == "WORKER_BUSY_TOO_LONG_RETRY") {
|
2018-12-26 19:01:46 +01:00
|
|
|
timeout = 1; // it is dangerous to resend query without timeout, so use 1
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
} else if (code == 420) {
|
2021-10-23 20:17:00 +02:00
|
|
|
auto error_message = query->error().message();
|
2019-11-12 21:05:38 +01:00
|
|
|
for (auto prefix :
|
|
|
|
{Slice("FLOOD_WAIT_"), Slice("SLOWMODE_WAIT_"), Slice("2FA_CONFIRM_WAIT_"), Slice("TAKEOUT_INIT_DELAY_")}) {
|
2021-10-23 20:17:00 +02:00
|
|
|
if (begins_with(error_message, prefix)) {
|
|
|
|
timeout = clamp(to_integer<int>(error_message.substr(prefix.size())), 1, 14 * 24 * 60 * 60);
|
2018-06-27 20:26:52 +02:00
|
|
|
break;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
G()->net_query_dispatcher().dispatch(std::move(query));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (timeout == 0) {
|
2020-06-10 01:44:54 +02:00
|
|
|
timeout = query->next_timeout_;
|
2018-12-31 20:04:05 +01:00
|
|
|
if (timeout < 60) {
|
2020-06-10 01:44:54 +02:00
|
|
|
query->next_timeout_ *= 2;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
} else {
|
2020-06-10 01:44:54 +02:00
|
|
|
query->next_timeout_ = 1;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2020-06-10 01:44:54 +02:00
|
|
|
query->total_timeout_ += timeout;
|
|
|
|
query->last_timeout_ = timeout;
|
2022-08-02 18:35:35 +02:00
|
|
|
LOG(INFO) << "Set total_timeout to " << query->total_timeout_ << " for " << query->id();
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
auto error = query->error().move_as_error();
|
|
|
|
query->resend();
|
|
|
|
|
|
|
|
// Fix for infinity flood control
|
2020-06-10 01:44:54 +02:00
|
|
|
if (!query->need_resend_on_503_ && code == -503) {
|
2018-12-31 20:04:05 +01:00
|
|
|
query->set_error(Status::Error(502, "Bad Gateway"));
|
|
|
|
query->debug("DcManager: send to DcManager");
|
|
|
|
G()->net_query_dispatcher().dispatch(std::move(query));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-06-10 01:44:54 +02:00
|
|
|
if (query->total_timeout_ > query->total_timeout_limit_) {
|
2018-12-31 20:04:05 +01:00
|
|
|
// TODO: support timeouts in DcAuth and GetConfig
|
2020-06-10 01:44:54 +02:00
|
|
|
LOG(WARNING) << "Failed: " << query << " " << tag("timeout", timeout) << tag("total_timeout", query->total_timeout_)
|
2018-12-31 20:04:05 +01:00
|
|
|
<< " because of " << error << " from " << query->source_;
|
|
|
|
// NB: code must differ from tdapi FLOOD_WAIT code
|
2022-02-04 15:05:35 +01:00
|
|
|
query->set_error(Status::Error(429, PSLICE() << "Too Many Requests: retry after " << timeout));
|
2018-12-31 20:04:05 +01:00
|
|
|
query->debug("DcManager: send to DcManager");
|
|
|
|
G()->net_query_dispatcher().dispatch(std::move(query));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-06-10 01:44:54 +02:00
|
|
|
LOG(WARNING) << "Delay: " << query << " " << tag("timeout", timeout) << tag("total_timeout", query->total_timeout_)
|
2018-12-31 20:04:05 +01:00
|
|
|
<< " because of " << error << " from " << query->source_;
|
|
|
|
query->debug(PSTRING() << "delay for " << format::as_time(timeout));
|
|
|
|
auto id = container_.create(QuerySlot());
|
|
|
|
auto *query_slot = container_.get(id);
|
|
|
|
query_slot->query_ = std::move(query);
|
|
|
|
query_slot->timeout_.set_event(EventCreator::yield(actor_shared(this, id)));
|
|
|
|
query_slot->timeout_.set_timeout_in(timeout);
|
|
|
|
}
|
|
|
|
|
|
|
|
void NetQueryDelayer::wakeup() {
|
|
|
|
auto link_token = get_link_token();
|
|
|
|
if (link_token) {
|
|
|
|
on_slot_event(link_token);
|
|
|
|
}
|
|
|
|
loop();
|
|
|
|
}
|
|
|
|
|
|
|
|
void NetQueryDelayer::on_slot_event(uint64 id) {
|
|
|
|
auto *slot = container_.get(id);
|
|
|
|
if (slot == nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto query = std::move(slot->query_);
|
|
|
|
if (!query->invoke_after().empty()) {
|
|
|
|
// Fail query after timeout expired if it is a part of an invokeAfter chain.
|
|
|
|
// It is not necessary but helps to avoid server problems, when previous query was lost.
|
|
|
|
query->set_error_resend_invoke_after();
|
|
|
|
}
|
|
|
|
slot->timeout_.close();
|
|
|
|
container_.erase(id);
|
|
|
|
G()->net_query_dispatcher().dispatch(std::move(query));
|
|
|
|
}
|
|
|
|
|
|
|
|
void NetQueryDelayer::tear_down() {
|
|
|
|
container_.for_each([](auto id, auto &query_slot) {
|
2021-10-07 15:36:21 +02:00
|
|
|
query_slot.query_->set_error(Global::request_aborted_error());
|
2018-12-31 20:04:05 +01:00
|
|
|
G()->net_query_dispatcher().dispatch(std::move(query_slot.query_));
|
|
|
|
});
|
|
|
|
}
|
2018-04-19 19:21:26 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
} // namespace td
|