Skip to content

Commit

Permalink
Merge version 6.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
giuseppeM99 committed May 1, 2022
2 parents f2e7e45 + 0dfcb61 commit edcb852
Show file tree
Hide file tree
Showing 12 changed files with 949 additions and 431 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ if (POLICY CMP0065)
cmake_policy(SET CMP0065 NEW)
endif()

project(TelegramBotApi VERSION 5.7 LANGUAGES CXX)
project(TelegramBotApi VERSION 6.0.1 LANGUAGES CXX)

if (POLICY CMP0069)
option(TELEGRAM_BOT_API_ENABLE_LTO "Use \"ON\" to enable Link Time Optimization.")
Expand Down
2 changes: 1 addition & 1 deletion td
Submodule td updated 277 files
1,182 changes: 822 additions & 360 deletions telegram-bot-api/Client.cpp

Large diffs are not rendered by default.

106 changes: 73 additions & 33 deletions telegram-bot-api/Client.h

Large diffs are not rendered by default.

9 changes: 7 additions & 2 deletions telegram-bot-api/ClientManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,13 @@ void ClientManager::send(PromisedQueryPtr query) {
return fail_query(401, "Unauthorized: invalid token specified", std::move(query));
}
auto r_user_id = td::to_integer_safe<td::int64>(query->token().substr(0, token.find(':')));
if (r_user_id.is_error() || r_user_id.ok() < 0 || !token_range_(r_user_id.ok())) {
if (r_user_id.is_error() || !token_range_(r_user_id.ok())) {
return fail_query(421, "Misdirected Request: unallowed token specified", std::move(query));
}
auto user_id = r_user_id.ok();
if (user_id <= 0 || user_id >= (static_cast<td::int64>(1) << 54)) {
return fail_query(401, "Unauthorized: invalid token specified", std::move(query));
}

if (query->is_test_dc()) {
token += "/test";
Expand Down Expand Up @@ -532,7 +536,7 @@ PromisedQueryPtr ClientManager::get_webhook_restore_query(td::Slice token, bool
args.emplace_back(add_string("url"), add_string(parser.read_all()));

const auto method = add_string("setwebhook");
auto query = std::make_unique<Query>(std::move(containers), token, is_user, is_test_dc, method, std::move(args),
auto query = td::make_unique<Query>(std::move(containers), token, is_user, is_test_dc, method, std::move(args),
td::vector<std::pair<td::MutableSlice, td::MutableSlice>>(),
td::vector<td::HttpFile>(), std::move(shared_data), td::IPAddress(), true);
return PromisedQueryPtr(query.release(), PromiseDeleter(td::PromiseActor<td::unique_ptr<Query>>()));
Expand All @@ -542,6 +546,7 @@ void ClientManager::raw_event(const td::Event::Raw &event) {
auto id = get_link_token();
auto *info = clients_.get(id);
CHECK(info != nullptr);
CHECK(info->tqueue_id_ != 0);
auto &value = active_client_count_[info->tqueue_id_];
if (event.ptr != nullptr) {
value++;
Expand Down
8 changes: 4 additions & 4 deletions telegram-bot-api/ClientManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
#include "td/utils/buffer.h"
#include "td/utils/common.h"
#include "td/utils/Container.h"
#include "td/utils/FlatHashMap.h"
#include "td/utils/FloodControlFast.h"
#include "td/utils/Slice.h"

#include <memory>
#include <unordered_map>
#include <utility>

namespace telegram_bot_api {
Expand Down Expand Up @@ -64,9 +64,9 @@ class ClientManager final : public td::Actor {
std::shared_ptr<const ClientParameters> parameters_;
TokenRange token_range_;

std::unordered_map<td::string, td::uint64> token_to_id_;
std::unordered_map<td::string, td::FloodControlFast> flood_controls_;
std::unordered_map<td::int64, td::uint64> active_client_count_;
td::FlatHashMap<td::string, td::uint64> token_to_id_;
td::FlatHashMap<td::string, td::FloodControlFast> flood_controls_;
td::FlatHashMap<td::int64, td::uint64> active_client_count_;

bool close_flag_ = false;
td::vector<td::Promise<td::Unit>> close_promises_;
Expand Down
2 changes: 1 addition & 1 deletion telegram-bot-api/HttpConnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ void HttpConnection::handle(td::unique_ptr<td::HttpQuery> http_query,
}


auto query = std::make_unique<Query>(std::move(http_query->container_), token, is_user, is_test_dc, method,
auto query = td::make_unique<Query>(std::move(http_query->container_), token, is_user, is_test_dc, method,
std::move(http_query->args_), std::move(http_query->headers_),
std::move(http_query->files_), shared_data_, http_query->peer_address_, false);

Expand Down
6 changes: 3 additions & 3 deletions telegram-bot-api/Query.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

namespace telegram_bot_api {

std::unordered_map<td::string, std::unique_ptr<td::VirtuallyJsonable>> empty_parameters;
td::FlatHashMap<td::string, td::unique_ptr<td::VirtuallyJsonable>> empty_parameters;

Query::Query(td::vector<td::BufferSlice> &&container, td::Slice token, bool is_user, bool is_test_dc, td::MutableSlice method,
td::vector<std::pair<td::MutableSlice, td::MutableSlice>> &&args,
Expand Down Expand Up @@ -97,8 +97,8 @@ void Query::set_error(int http_status_code, td::BufferSlice result) {
void Query::set_retry_after_error(int retry_after) {
retry_after_ = retry_after;

std::unordered_map<td::string, std::unique_ptr<td::VirtuallyJsonable>> parameters;
parameters.emplace("retry_after", std::make_unique<td::VirtuallyJsonableLong>(retry_after));
td::FlatHashMap<td::string, td::unique_ptr<td::VirtuallyJsonable>> parameters;
parameters.emplace("retry_after", td::make_unique<td::VirtuallyJsonableLong>(retry_after));
set_error(429, td::json_encode<td::BufferSlice>(
JsonQueryError(429, PSLICE() << "Too Many Requests: retry after " << retry_after, parameters)));
}
Expand Down
14 changes: 7 additions & 7 deletions telegram-bot-api/Query.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

#include "td/utils/buffer.h"
#include "td/utils/common.h"
#include "td/utils/FlatHashMap.h"
#include "td/utils/JsonBuilder.h"
#include "td/utils/List.h"
#include "td/utils/port/IPAddress.h"
Expand All @@ -23,7 +24,6 @@

#include <algorithm>
#include <memory>
#include <unordered_map>
#include <utility>

namespace telegram_bot_api {
Expand Down Expand Up @@ -168,11 +168,11 @@ td::StringBuilder &operator<<(td::StringBuilder &sb, const Query &query);

// fix for outdated C++14 libraries
// https://stackoverflow.com/questions/26947704/implicit-conversion-failure-from-initializer-list
extern std::unordered_map<td::string, std::unique_ptr<td::VirtuallyJsonable>> empty_parameters;
extern td::FlatHashMap<td::string, td::unique_ptr<td::VirtuallyJsonable>> empty_parameters;

class JsonParameters final : public td::Jsonable {
public:
explicit JsonParameters(const std::unordered_map<td::string, std::unique_ptr<td::VirtuallyJsonable>> &parameters)
explicit JsonParameters(const td::FlatHashMap<td::string, td::unique_ptr<td::VirtuallyJsonable>> &parameters)
: parameters_(parameters) {
}
void store(td::JsonValueScope *scope) const {
Expand All @@ -184,7 +184,7 @@ class JsonParameters final : public td::Jsonable {
}

private:
const std::unordered_map<td::string, std::unique_ptr<td::VirtuallyJsonable>> &parameters_;
const td::FlatHashMap<td::string, td::unique_ptr<td::VirtuallyJsonable>> &parameters_;
};

template <class T>
Expand All @@ -210,7 +210,7 @@ class JsonQueryError final : public td::Jsonable {
public:
JsonQueryError(
int error_code, td::Slice description,
const std::unordered_map<td::string, std::unique_ptr<td::VirtuallyJsonable>> &parameters = empty_parameters)
const td::FlatHashMap<td::string, td::unique_ptr<td::VirtuallyJsonable>> &parameters = empty_parameters)
: error_code_(error_code), description_(description), parameters_(parameters) {
}
void store(td::JsonValueScope *scope) const {
Expand All @@ -226,7 +226,7 @@ class JsonQueryError final : public td::Jsonable {
private:
int error_code_;
td::Slice description_;
const std::unordered_map<td::string, std::unique_ptr<td::VirtuallyJsonable>> &parameters_;
const td::FlatHashMap<td::string, td::unique_ptr<td::VirtuallyJsonable>> &parameters_;
};

class PromiseDeleter {
Expand Down Expand Up @@ -265,7 +265,7 @@ void answer_query(const Jsonable &result, PromisedQueryPtr query, td::Slice desc

inline void fail_query(
int http_status_code, td::Slice description, PromisedQueryPtr query,
const std::unordered_map<td::string, std::unique_ptr<td::VirtuallyJsonable>> &parameters = empty_parameters) {
const td::FlatHashMap<td::string, td::unique_ptr<td::VirtuallyJsonable>> &parameters = empty_parameters) {
query->set_error(http_status_code,
td::json_encode<td::BufferSlice>(JsonQueryError(http_status_code, description, parameters)));
query.reset(); // send query into promise explicitly
Expand Down
37 changes: 24 additions & 13 deletions telegram-bot-api/WebhookActor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -379,11 +379,14 @@ void WebhookActor::load_updates() {

for (auto &update : updates) {
VLOG(webhook) << "Load update " << update.id;
if (update_map_.find(update.id) != update_map_.end()) {
LOG(ERROR) << "Receive duplicated event " << update.id << " from tqueue";
CHECK(update.id.is_valid());
auto &dest_ptr = update_map_[update.id];
if (dest_ptr != nullptr) {
LOG(ERROR) << "Receive duplicated event " << update.id << " from TQueue";
continue;
}
auto &dest = update_map_[update.id];
dest_ptr = td::make_unique<Update>();
auto &dest = *dest_ptr;
dest.id_ = update.id;
dest.json_ = update.data.str();
dest.delay_ = 1;
Expand Down Expand Up @@ -437,7 +440,7 @@ void WebhookActor::load_updates() {
void WebhookActor::drop_event(td::TQueue::EventId event_id) {
auto it = update_map_.find(event_id);
CHECK(it != update_map_.end());
auto queue_id = it->second.queue_id_;
auto queue_id = it->second->queue_id_;
update_map_.erase(it);

auto queue_updates_it = queue_updates_.find(queue_id);
Expand All @@ -448,8 +451,10 @@ void WebhookActor::drop_event(td::TQueue::EventId event_id) {
if (queue_updates_it->second.event_ids.empty()) {
queue_updates_.erase(queue_updates_it);
} else {
auto &update = update_map_[queue_updates_it->second.event_ids.front()];
queues_.emplace(update.wakeup_at_, update.queue_id_);
auto update_id = queue_updates_it->second.event_ids.front();
CHECK(update_id.is_valid());
auto &update = update_map_[update_id];
queues_.emplace(update->wakeup_at_, update->queue_id_);
}

parameters_->shared_data_->tqueue_->forget(tqueue_id_, event_id);
Expand All @@ -462,7 +467,7 @@ void WebhookActor::on_update_ok(td::TQueue::EventId event_id) {
auto it = update_map_.find(event_id);
CHECK(it != update_map_.end());

VLOG(webhook) << "Receive ok for update " << event_id << " in " << (last_success_time_ - it->second.last_send_time_)
VLOG(webhook) << "Receive ok for update " << event_id << " in " << (last_success_time_ - it->second->last_send_time_)
<< " seconds";

drop_event(event_id);
Expand All @@ -474,21 +479,22 @@ void WebhookActor::on_update_error(td::TQueue::EventId event_id, td::Slice error

auto it = update_map_.find(event_id);
CHECK(it != update_map_.end());
CHECK(it->second != nullptr);
auto &update = *it->second;

const int MAX_RETRY_AFTER = 3600;
retry_after = td::clamp(retry_after, 0, MAX_RETRY_AFTER);
int next_delay = it->second.delay_;
int next_delay = update.delay_;
int next_effective_delay = retry_after;
if (retry_after == 0 && it->second.fail_count_ > 0) {
if (retry_after == 0 && update.fail_count_ > 0) {
next_delay = td::min(WEBHOOK_MAX_RESEND_TIMEOUT, next_delay * 2);
next_effective_delay = next_delay;
}
if (parameters_->shared_data_->get_unix_time(now) + next_effective_delay > it->second.expires_at_) {
if (parameters_->shared_data_->get_unix_time(now) + next_effective_delay > update.expires_at_) {
LOG(WARNING) << "Drop update " << event_id << ": " << error;
drop_event(event_id);
return;
}
auto &update = it->second;
update.delay_ = next_delay;
update.wakeup_at_ = now + next_effective_delay;
update.fail_count_++;
Expand All @@ -514,10 +520,15 @@ td::Status WebhookActor::send_update() {
}

auto queue_id = it->id;
CHECK(queue_id != 0);
queues_.erase(it);
auto event_id = queue_updates_[queue_id].event_ids.front();
CHECK(event_id.is_valid());

auto &update = update_map_[event_id];
auto update_map_it = update_map_.find(event_id);
CHECK(update_map_it != update_map_.end());
CHECK(update_map_it->second != nullptr);
auto &update = *update_map_it->second;
update.last_send_time_ = now;

auto body = td::json_encode<td::BufferSlice>(JsonUpdate(update.id_.value(), update.json_));
Expand Down Expand Up @@ -586,7 +597,7 @@ void WebhookActor::handle(td::unique_ptr<td::HttpQuery> response) {
if (!method.empty() && method != "deletewebhook" && method != "setwebhook" && method != "close" &&
method != "logout" && !td::begins_with(method, "get")) {
VLOG(webhook) << "Receive request " << method << " in response to webhook";
auto query = std::make_unique<Query>(std::move(response->container_), td::MutableSlice(), false, false,
auto query = td::make_unique<Query>(std::move(response->container_), td::MutableSlice(), false, false,
td::MutableSlice(), std::move(response->args_),
std::move(response->headers_), std::move(response->files_),
parameters_->shared_data_, response->peer_address_, false);
Expand Down
6 changes: 3 additions & 3 deletions telegram-bot-api/WebhookActor.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "td/utils/BufferedFd.h"
#include "td/utils/common.h"
#include "td/utils/Container.h"
#include "td/utils/FlatHashMap.h"
#include "td/utils/FloodControlFast.h"
#include "td/utils/HttpUrl.h"
#include "td/utils/JsonBuilder.h"
Expand All @@ -35,7 +36,6 @@
#include <memory>
#include <set>
#include <tuple>
#include <unordered_map>

namespace telegram_bot_api {

Expand Down Expand Up @@ -127,8 +127,8 @@ class WebhookActor final : public td::HttpOutboundConnection::Callback {
return std::hash<td::int32>()(event_id.value());
}
};
std::unordered_map<td::TQueue::EventId, Update, EventIdHash> update_map_;
std::unordered_map<td::int64, QueueUpdates> queue_updates_;
td::FlatHashMap<td::TQueue::EventId, td::unique_ptr<Update>, EventIdHash> update_map_;
td::FlatHashMap<td::int64, QueueUpdates> queue_updates_;
std::set<Queue> queues_;
td::int64 unique_queue_id_ = static_cast<td::int64>(1) << 60;

Expand Down
6 changes: 3 additions & 3 deletions telegram-bot-api/telegram-bot-api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ int main(int argc, char *argv[]) {
auto start_time = td::Time::now();
auto shared_data = std::make_shared<SharedData>();
auto parameters = std::make_unique<ClientParameters>();
parameters->version_ = "5.7";
parameters->version_ = "6.0.1";
parameters->shared_data_ = shared_data;
parameters->start_time_ = start_time;
auto net_query_stats = td::create_net_query_stats();
Expand Down Expand Up @@ -230,11 +230,11 @@ int main(int argc, char *argv[]) {
}
return 0;
}(std::getenv("TELEGRAM_API_ID"));
parameters->api_hash_ = [](auto x) -> std::string {
parameters->api_hash_ = [](auto x) -> td::string {
if (x) {
return x;
}
return std::string();
return td::string();
}(std::getenv("TELEGRAM_API_HASH"));

options.set_usage(td::Slice(argv[0]), "--api-id=<arg> --api-hash=<arg> [--local] [OPTION]...");
Expand Down

0 comments on commit edcb852

Please sign in to comment.