2018-12-31 20:04:05 +01:00
|
|
|
//
|
2022-12-31 22:28:08 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2023
|
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/NetQueryCreator.h"
|
|
|
|
|
2020-03-16 02:09:17 +01:00
|
|
|
#include "td/telegram/AuthManager.h"
|
|
|
|
#include "td/telegram/Global.h"
|
|
|
|
#include "td/telegram/Td.h"
|
2020-03-15 23:01:14 +01:00
|
|
|
#include "td/telegram/telegram_api.h"
|
|
|
|
|
2023-07-26 11:29:36 +02:00
|
|
|
#include "td/actor/actor.h"
|
|
|
|
|
2021-09-10 16:32:39 +02:00
|
|
|
#include "td/utils/buffer.h"
|
2018-08-06 18:04:21 +02:00
|
|
|
#include "td/utils/format.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/Gzip.h"
|
2018-07-17 13:27:24 +02:00
|
|
|
#include "td/utils/logging.h"
|
2020-03-15 23:01:14 +01:00
|
|
|
#include "td/utils/Storer.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
namespace td {
|
2018-04-24 18:21:47 +02:00
|
|
|
|
2021-10-06 17:47:32 +02:00
|
|
|
NetQueryCreator::NetQueryCreator(std::shared_ptr<NetQueryStats> net_query_stats)
|
2023-07-26 13:46:31 +02:00
|
|
|
: net_query_stats_(std::move(net_query_stats))
|
|
|
|
, current_scheduler_id_(Scheduler::instance() == nullptr ? -2 : Scheduler::instance()->sched_id()) {
|
2021-10-06 17:47:32 +02:00
|
|
|
object_pool_.set_check_empty(true);
|
|
|
|
}
|
|
|
|
|
2022-02-05 21:28:43 +01:00
|
|
|
NetQueryPtr NetQueryCreator::create(const telegram_api::Function &function, vector<ChainId> chain_ids, DcId dc_id,
|
2022-02-01 17:34:14 +01:00
|
|
|
NetQuery::Type type) {
|
2022-02-03 20:32:47 +01:00
|
|
|
return create(UniqueId::next(), function, std::move(chain_ids), dc_id, type, NetQuery::AuthFlag::On);
|
2020-03-15 22:17:11 +01:00
|
|
|
}
|
|
|
|
|
2022-02-05 21:28:43 +01:00
|
|
|
NetQueryPtr NetQueryCreator::create(uint64 id, const telegram_api::Function &function, vector<ChainId> &&chain_ids,
|
2022-02-01 17:34:14 +01:00
|
|
|
DcId dc_id, NetQuery::Type type, NetQuery::AuthFlag auth_flag) {
|
2021-05-16 23:12:52 +02:00
|
|
|
LOG(INFO) << "Create query " << to_string(function);
|
2020-03-15 23:01:14 +01:00
|
|
|
auto storer = DefaultStorer<telegram_api::Function>(function);
|
2018-12-31 20:04:05 +01:00
|
|
|
BufferSlice slice(storer.size());
|
2023-02-02 15:40:47 +01:00
|
|
|
auto real_size = storer.store(slice.as_mutable_slice().ubegin());
|
2019-02-12 17:17:20 +01:00
|
|
|
LOG_CHECK(real_size == slice.size()) << real_size << " " << slice.size() << " "
|
2023-02-21 09:21:07 +01:00
|
|
|
<< format::as_hex_dump<4>(slice.as_slice());
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2023-05-10 01:06:24 +02:00
|
|
|
size_t min_gzipped_size = 128;
|
2020-08-04 01:24:12 +02:00
|
|
|
int32 tl_constructor = function.get_id();
|
2023-05-10 01:06:24 +02:00
|
|
|
int32 total_timeout_limit = 60;
|
|
|
|
|
2023-07-26 13:46:31 +02:00
|
|
|
if (Scheduler::instance() != nullptr && current_scheduler_id_ == Scheduler::instance()->sched_id() &&
|
|
|
|
!G()->close_flag()) {
|
2023-05-10 01:06:24 +02:00
|
|
|
auto td = G()->td();
|
|
|
|
if (!td.empty()) {
|
|
|
|
auto auth_manager = td.get_actor_unsafe()->auth_manager_.get();
|
|
|
|
if (auth_manager != nullptr && auth_manager->is_bot()) {
|
|
|
|
total_timeout_limit = 8;
|
|
|
|
min_gzipped_size = 1024;
|
|
|
|
}
|
|
|
|
if ((auth_manager == nullptr || !auth_manager->was_authorized()) && auth_flag == NetQuery::AuthFlag::On &&
|
|
|
|
tl_constructor != telegram_api::auth_exportAuthorization::ID &&
|
|
|
|
tl_constructor != telegram_api::auth_bindTempAuthKey::ID) {
|
|
|
|
LOG(ERROR) << "Send query before authorization: " << to_string(function);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-03-15 00:49:10 +01:00
|
|
|
|
2023-05-10 01:06:24 +02:00
|
|
|
auto gzip_flag = slice.size() < min_gzipped_size ? NetQuery::GzipFlag::Off : NetQuery::GzipFlag::On;
|
2020-03-15 00:49:10 +01:00
|
|
|
if (slice.size() >= 16384) {
|
|
|
|
// test compression ratio for the middle part
|
|
|
|
// if it is less than 0.9, then try to compress the whole request
|
|
|
|
size_t TESTED_SIZE = 1024;
|
|
|
|
BufferSlice compressed_part = gzencode(slice.as_slice().substr((slice.size() - TESTED_SIZE) / 2, TESTED_SIZE), 0.9);
|
|
|
|
if (compressed_part.empty()) {
|
|
|
|
gzip_flag = NetQuery::GzipFlag::Off;
|
|
|
|
}
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
if (gzip_flag == NetQuery::GzipFlag::On) {
|
2020-03-14 23:56:48 +01:00
|
|
|
BufferSlice compressed = gzencode(slice.as_slice(), 0.9);
|
2018-12-31 20:04:05 +01:00
|
|
|
if (compressed.empty()) {
|
|
|
|
gzip_flag = NetQuery::GzipFlag::Off;
|
|
|
|
} else {
|
|
|
|
slice = std::move(compressed);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-30 17:13:35 +02:00
|
|
|
auto query = object_pool_.create(id, std::move(slice), dc_id, type, auth_flag, gzip_flag, tl_constructor,
|
|
|
|
total_timeout_limit, net_query_stats_.get(), std::move(chain_ids));
|
2018-12-31 20:04:05 +01:00
|
|
|
query->set_cancellation_token(query.generation());
|
|
|
|
return query;
|
|
|
|
}
|
2018-04-24 18:21:47 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
} // namespace td
|