This repository has been archived on 2020-05-25. You can view files and clone it, but cannot push or open issues or pull requests.
tdlib-fork/td/telegram/net/NetQueryDelayer.cpp
levlam 2c4678a1e3 Send NetQuery to callback when closing.
GitOrigin-RevId: 6e564fd9401ae4352471d2938058d02e54c73f05
2018-04-19 20:21:26 +03:00

122 lines
3.8 KiB
C++

//
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2018
//
// 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"
#include "td/utils/format.h"
#include "td/utils/logging.h"
#include "td/utils/misc.h"
#include "td/utils/Slice.h"
#include "td/utils/Status.h"
namespace td {
void NetQueryDelayer::delay(NetQueryPtr query) {
query->debug("try delay");
query->is_ready();
CHECK(query->is_error());
auto code = query->error().code();
double timeout = 0;
if (code < 0 || code == 500) {
// skip
} else if (code == 420) {
auto msg = query->error().message();
auto prefix = Slice("FLOOD_WAIT_");
if (msg.substr(0, prefix.size()) == prefix) {
timeout = to_integer<int>(msg.substr(prefix.size()));
if (timeout < 0) {
timeout = 0;
}
if (timeout > 24 * 60 * 60) {
timeout = 24 * 60 * 60;
}
}
} else {
G()->net_query_dispatcher().dispatch(std::move(query));
return;
}
if (timeout == 0) {
timeout = query->next_timeout;
if (timeout < 60) {
query->next_timeout *= 2;
}
} else {
query->next_timeout = 1;
}
query->total_timeout += timeout;
query->last_timeout = timeout;
auto error = query->error().move_as_error();
query->resend();
// Fix for infinity flood control
if (!query->need_resend_on_503 && code == -503) {
query->set_error(Status::Error(502, "Bad Gateway"));
query->debug("DcManager: send to DcManager");
G()->net_query_dispatcher().dispatch(std::move(query));
return;
}
if (query->total_timeout > query->total_timeout_limit) {
// TODO: support timeouts in DcAuth and GetConfig
LOG(WARNING) << "Failed: " << query << " " << tag("timeout", timeout) << tag("total_timeout", query->total_timeout)
<< " because of " << error << " from " << query->source_;
// NB: code must differ from tdapi FLOOD_WAIT code
query->set_error(
Status::Error(429, PSLICE() << "Too Many Requests: retry after " << static_cast<int32>(timeout + 0.999)));
query->debug("DcManager: send to DcManager");
G()->net_query_dispatcher().dispatch(std::move(query));
return;
}
LOG(WARNING) << "Delay: " << query << " " << tag("timeout", timeout) << tag("total_timeout", query->total_timeout)
<< " 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) {
LOG(INFO) << "raw_event";
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) {
query_slot.query_->set_error(Status::Error(500, "Internal Server Error: closing"));
G()->net_query_dispatcher().dispatch(std::move(query_slot.query_));
});
}
} // namespace td