Minor improvements.

This commit is contained in:
levlam 2022-01-25 18:18:44 +03:00
parent 93ccd2a145
commit 208fd70d58
14 changed files with 236 additions and 235 deletions

File diff suppressed because it is too large Load Diff

View File

@ -11,6 +11,7 @@
#include "telegram-bot-api/WebhookActor.h" #include "telegram-bot-api/WebhookActor.h"
#include "td/telegram/ClientActor.h" #include "td/telegram/ClientActor.h"
#include "td/telegram/td_api.h"
#include "td/actor/actor.h" #include "td/actor/actor.h"
#include "td/actor/PromiseFuture.h" #include "td/actor/PromiseFuture.h"
@ -35,12 +36,12 @@ struct ClientParameters;
namespace td_api = td::td_api; namespace td_api = td::td_api;
class Client : public WebhookActor::Callback { class Client final : public WebhookActor::Callback {
public: public:
Client(td::ActorShared<> parent, const td::string &bot_token, bool is_test_dc, td::int64 tqueue_id, Client(td::ActorShared<> parent, const td::string &bot_token, bool is_test_dc, td::int64 tqueue_id,
std::shared_ptr<const ClientParameters> parameters, td::ActorId<BotStatActor> stat_actor); std::shared_ptr<const ClientParameters> parameters, td::ActorId<BotStatActor> stat_actor);
void send(PromisedQueryPtr query) override; void send(PromisedQueryPtr query) final;
void close(); void close();
@ -519,11 +520,11 @@ class Client : public WebhookActor::Callback {
Status process_get_webhook_info_query(PromisedQueryPtr &query); Status process_get_webhook_info_query(PromisedQueryPtr &query);
Status process_get_file_query(PromisedQueryPtr &query); Status process_get_file_query(PromisedQueryPtr &query);
void webhook_verified(td::string cached_ip_address) override; void webhook_verified(td::string cached_ip_address) final;
void webhook_success() override; void webhook_success() final;
void webhook_error(Status status) override; void webhook_error(Status status) final;
void webhook_closed(Status status) override; void webhook_closed(Status status) final;
void hangup_shared() override; void hangup_shared() final;
int32 get_webhook_max_connections(const Query *query) const; int32 get_webhook_max_connections(const Query *query) const;
static bool get_webhook_fix_ip_address(const Query *query); static bool get_webhook_fix_ip_address(const Query *query);
void do_set_webhook(PromisedQueryPtr query, bool was_deleted); void do_set_webhook(PromisedQueryPtr query, bool was_deleted);
@ -562,13 +563,13 @@ class Client : public WebhookActor::Callback {
void long_poll_wakeup(bool force_flag); void long_poll_wakeup(bool force_flag);
void start_up() override; void start_up() final;
void raw_event(const td::Event::Raw &event) override; void raw_event(const td::Event::Raw &event) final;
void loop() override; void loop() final;
void timeout_expired() override; void timeout_expired() final;
struct UserInfo { struct UserInfo {
enum class Type { Regular, Deleted, Bot, Unknown }; enum class Type { Regular, Deleted, Bot, Unknown };

View File

@ -11,6 +11,7 @@
#include "telegram-bot-api/WebhookActor.h" #include "telegram-bot-api/WebhookActor.h"
#include "td/telegram/ClientActor.h" #include "td/telegram/ClientActor.h"
#include "td/telegram/td_api.h"
#include "td/db/binlog/Binlog.h" #include "td/db/binlog/Binlog.h"
#include "td/db/binlog/ConcurrentBinlog.h" #include "td/db/binlog/ConcurrentBinlog.h"
@ -104,7 +105,7 @@ void ClientManager::send(PromisedQueryPtr query) {
flood_control.add_limit(60, 20); // 20 in a minute flood_control.add_limit(60, 20); // 20 in a minute
flood_control.add_limit(60 * 60, 600); // 600 in an hour flood_control.add_limit(60 * 60, 600); // 600 in an hour
} }
td::uint32 now = static_cast<td::uint32>(td::Time::now()); auto now = static_cast<td::uint32>(td::Time::now());
td::uint32 wakeup_at = flood_control.get_wakeup_at(); td::uint32 wakeup_at = flood_control.get_wakeup_at();
if (wakeup_at > now) { if (wakeup_at > now) {
LOG(INFO) << "Failed to create Client from IP address " << ip_address; LOG(INFO) << "Failed to create Client from IP address " << ip_address;

View File

@ -73,9 +73,9 @@ class ClientManager final : public td::Actor {
static PromisedQueryPtr get_webhook_restore_query(td::Slice token, td::Slice webhook_info, static PromisedQueryPtr get_webhook_restore_query(td::Slice token, td::Slice webhook_info,
std::shared_ptr<SharedData> shared_data); std::shared_ptr<SharedData> shared_data);
void start_up() override; void start_up() final;
void raw_event(const td::Event::Raw &event) override; void raw_event(const td::Event::Raw &event) final;
void hangup_shared() override; void hangup_shared() final;
void close_db(); void close_db();
void finish_close(); void finish_close();
}; };

View File

@ -6,13 +6,13 @@
// //
#pragma once #pragma once
#include "td/actor/actor.h"
#include "td/db/KeyValueSyncInterface.h" #include "td/db/KeyValueSyncInterface.h"
#include "td/db/TQueue.h" #include "td/db/TQueue.h"
#include "td/net/GetHostByNameActor.h" #include "td/net/GetHostByNameActor.h"
#include "td/actor/actor.h"
#include "td/utils/common.h" #include "td/utils/common.h"
#include "td/utils/List.h" #include "td/utils/List.h"
#include "td/utils/port/IPAddress.h" #include "td/utils/port/IPAddress.h"

View File

@ -9,12 +9,12 @@
#include "telegram-bot-api/ClientManager.h" #include "telegram-bot-api/ClientManager.h"
#include "telegram-bot-api/Query.h" #include "telegram-bot-api/Query.h"
#include "td/actor/actor.h"
#include "td/actor/PromiseFuture.h"
#include "td/net/HttpInboundConnection.h" #include "td/net/HttpInboundConnection.h"
#include "td/net/HttpQuery.h" #include "td/net/HttpQuery.h"
#include "td/actor/actor.h"
#include "td/actor/PromiseFuture.h"
#include "td/utils/buffer.h" #include "td/utils/buffer.h"
#include "td/utils/Slice.h" #include "td/utils/Slice.h"
@ -24,15 +24,15 @@ namespace telegram_bot_api {
struct SharedData; struct SharedData;
class HttpConnection : public td::HttpInboundConnection::Callback { class HttpConnection final : public td::HttpInboundConnection::Callback {
public: public:
explicit HttpConnection(td::ActorId<ClientManager> client_manager, std::shared_ptr<SharedData> shared_data) explicit HttpConnection(td::ActorId<ClientManager> client_manager, std::shared_ptr<SharedData> shared_data)
: client_manager_(client_manager), shared_data_(std::move(shared_data)) { : client_manager_(client_manager), shared_data_(std::move(shared_data)) {
} }
void handle(td::unique_ptr<td::HttpQuery> http_query, td::ActorOwn<td::HttpInboundConnection> connection) override; void handle(td::unique_ptr<td::HttpQuery> http_query, td::ActorOwn<td::HttpInboundConnection> connection) final;
void wakeup() override; void wakeup() final;
private: private:
td::FutureActor<td::unique_ptr<Query>> result_; td::FutureActor<td::unique_ptr<Query>> result_;
@ -40,7 +40,7 @@ class HttpConnection : public td::HttpInboundConnection::Callback {
td::ActorOwn<td::HttpInboundConnection> connection_; td::ActorOwn<td::HttpInboundConnection> connection_;
std::shared_ptr<SharedData> shared_data_; std::shared_ptr<SharedData> shared_data_;
void hangup() override { void hangup() final {
connection_.release(); connection_.release();
stop(); stop();
} }

View File

@ -6,11 +6,11 @@
// //
#pragma once #pragma once
#include "td/actor/actor.h"
#include "td/net/HttpInboundConnection.h" #include "td/net/HttpInboundConnection.h"
#include "td/net/TcpListener.h" #include "td/net/TcpListener.h"
#include "td/actor/actor.h"
#include "td/utils/BufferedFd.h" #include "td/utils/BufferedFd.h"
#include "td/utils/FloodControlFast.h" #include "td/utils/FloodControlFast.h"
#include "td/utils/format.h" #include "td/utils/format.h"
@ -23,7 +23,7 @@
namespace telegram_bot_api { namespace telegram_bot_api {
class HttpServer : public td::TcpListener::Callback { class HttpServer final : public td::TcpListener::Callback {
public: public:
HttpServer(td::string ip_address, int port, HttpServer(td::string ip_address, int port,
std::function<td::ActorOwn<td::HttpInboundConnection::Callback>()> creator) std::function<td::ActorOwn<td::HttpInboundConnection::Callback>()> creator)
@ -39,7 +39,7 @@ class HttpServer : public td::TcpListener::Callback {
td::ActorOwn<td::TcpListener> listener_; td::ActorOwn<td::TcpListener> listener_;
td::FloodControlFast flood_control_; td::FloodControlFast flood_control_;
void start_up() override { void start_up() final {
auto now = td::Time::now(); auto now = td::Time::now();
auto wakeup_at = flood_control_.get_wakeup_at(); auto wakeup_at = flood_control_.get_wakeup_at();
if (wakeup_at > now) { if (wakeup_at > now) {
@ -53,13 +53,13 @@ class HttpServer : public td::TcpListener::Callback {
actor_shared(this, 1), ip_address_); actor_shared(this, 1), ip_address_);
} }
void hangup_shared() override { void hangup_shared() final {
LOG(ERROR) << "TCP listener was closed"; LOG(ERROR) << "TCP listener was closed";
listener_.release(); listener_.release();
yield(); yield();
} }
void accept(td::SocketFd fd) override { void accept(td::SocketFd fd) final {
auto scheduler_count = td::Scheduler::instance()->sched_count(); auto scheduler_count = td::Scheduler::instance()->sched_count();
auto scheduler_id = scheduler_count - 1; auto scheduler_id = scheduler_count - 1;
if (scheduler_id > 0) { if (scheduler_id > 0) {
@ -70,7 +70,7 @@ class HttpServer : public td::TcpListener::Callback {
.release(); .release();
} }
void loop() override { void loop() final {
if (listener_.empty()) { if (listener_.empty()) {
start_up(); start_up();
} }

View File

@ -8,30 +8,30 @@
#include "telegram-bot-api/ClientManager.h" #include "telegram-bot-api/ClientManager.h"
#include "td/actor/actor.h"
#include "td/actor/PromiseFuture.h"
#include "td/net/HttpInboundConnection.h" #include "td/net/HttpInboundConnection.h"
#include "td/net/HttpQuery.h" #include "td/net/HttpQuery.h"
#include "td/actor/actor.h"
#include "td/actor/PromiseFuture.h"
#include "td/utils/buffer.h" #include "td/utils/buffer.h"
namespace telegram_bot_api { namespace telegram_bot_api {
class HttpStatConnection : public td::HttpInboundConnection::Callback { class HttpStatConnection final : public td::HttpInboundConnection::Callback {
public: public:
explicit HttpStatConnection(td::ActorId<ClientManager> client_manager) : client_manager_(client_manager) { explicit HttpStatConnection(td::ActorId<ClientManager> client_manager) : client_manager_(client_manager) {
} }
void handle(td::unique_ptr<td::HttpQuery> http_query, td::ActorOwn<td::HttpInboundConnection> connection) override; void handle(td::unique_ptr<td::HttpQuery> http_query, td::ActorOwn<td::HttpInboundConnection> connection) final;
void wakeup() override; void wakeup() final;
private: private:
td::FutureActor<td::BufferSlice> result_; td::FutureActor<td::BufferSlice> result_;
td::ActorId<ClientManager> client_manager_; td::ActorId<ClientManager> client_manager_;
td::ActorOwn<td::HttpInboundConnection> connection_; td::ActorOwn<td::HttpInboundConnection> connection_;
void hangup() override { void hangup() final {
connection_.release(); connection_.release();
stop(); stop();
} }

View File

@ -8,11 +8,11 @@
#include "telegram-bot-api/ClientParameters.h" #include "telegram-bot-api/ClientParameters.h"
#include "td/net/HttpFile.h"
#include "td/actor/actor.h" #include "td/actor/actor.h"
#include "td/actor/PromiseFuture.h" #include "td/actor/PromiseFuture.h"
#include "td/net/HttpFile.h"
#include "td/utils/buffer.h" #include "td/utils/buffer.h"
#include "td/utils/common.h" #include "td/utils/common.h"
#include "td/utils/JsonBuilder.h" #include "td/utils/JsonBuilder.h"
@ -30,7 +30,7 @@ namespace telegram_bot_api {
class BotStatActor; class BotStatActor;
class Query : public td::ListNode { class Query final : public td::ListNode {
public: public:
enum class State : td::int8 { Query, OK, Error }; enum class State : td::int8 { Query, OK, Error };
@ -166,7 +166,7 @@ td::StringBuilder &operator<<(td::StringBuilder &sb, const Query &query);
// https://stackoverflow.com/questions/26947704/implicit-conversion-failure-from-initializer-list // 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 std::unordered_map<td::string, std::unique_ptr<td::VirtuallyJsonable>> empty_parameters;
class JsonParameters : public td::Jsonable { class JsonParameters final : public td::Jsonable {
public: public:
explicit JsonParameters(const std::unordered_map<td::string, std::unique_ptr<td::VirtuallyJsonable>> &parameters) explicit JsonParameters(const std::unordered_map<td::string, std::unique_ptr<td::VirtuallyJsonable>> &parameters)
: parameters_(parameters) { : parameters_(parameters) {
@ -184,7 +184,7 @@ class JsonParameters : public td::Jsonable {
}; };
template <class T> template <class T>
class JsonQueryOk : public td::Jsonable { class JsonQueryOk final : public td::Jsonable {
public: public:
JsonQueryOk(const T &result, td::Slice description) : result_(result), description_(description) { JsonQueryOk(const T &result, td::Slice description) : result_(result), description_(description) {
} }
@ -202,7 +202,7 @@ class JsonQueryOk : public td::Jsonable {
td::Slice description_; td::Slice description_;
}; };
class JsonQueryError : public td::Jsonable { class JsonQueryError final : public td::Jsonable {
public: public:
JsonQueryError( JsonQueryError(
int error_code, td::Slice description, int error_code, td::Slice description,

View File

@ -156,7 +156,7 @@ class BotStatActor final : public td::Actor {
BotStatActor(const BotStatActor &) = delete; BotStatActor(const BotStatActor &) = delete;
BotStatActor &operator=(const BotStatActor &other) = delete; BotStatActor &operator=(const BotStatActor &other) = delete;
BotStatActor(BotStatActor &&) = default; BotStatActor(BotStatActor &&) = default;
BotStatActor &operator=(BotStatActor &&other) { BotStatActor &operator=(BotStatActor &&other) noexcept {
if (!empty()) { if (!empty()) {
do_stop(); do_stop();
} }
@ -165,7 +165,7 @@ class BotStatActor final : public td::Actor {
parent_ = other.parent_; parent_ = other.parent_;
return *this; return *this;
} }
~BotStatActor() override = default; ~BotStatActor() final = default;
template <class EventT> template <class EventT>
void add_event(const EventT &event, double now) { void add_event(const EventT &event, double now) {

View File

@ -152,11 +152,11 @@ td::Status WebhookActor::create_connection() {
} }
VLOG(webhook) << "Create connection through proxy " << parameters_->webhook_proxy_ip_address_; VLOG(webhook) << "Create connection through proxy " << parameters_->webhook_proxy_ip_address_;
class Callback : public td::TransparentProxy::Callback { class Callback final : public td::TransparentProxy::Callback {
public: public:
Callback(td::ActorId<WebhookActor> actor, td::int64 id) : actor_(actor), id_(id) { Callback(td::ActorId<WebhookActor> actor, td::int64 id) : actor_(actor), id_(id) {
} }
void set_result(td::Result<td::BufferedFd<td::SocketFd>> result) override { void set_result(td::Result<td::BufferedFd<td::SocketFd>> result) final {
send_closure(std::move(actor_), &WebhookActor::on_socket_ready_async, std::move(result), id_); send_closure(std::move(actor_), &WebhookActor::on_socket_ready_async, std::move(result), id_);
CHECK(actor_.empty()); CHECK(actor_.empty());
} }
@ -169,7 +169,7 @@ td::Status WebhookActor::create_connection() {
send_closure(std::move(actor_), &WebhookActor::on_socket_ready_async, td::Status::Error("Canceled"), id_); send_closure(std::move(actor_), &WebhookActor::on_socket_ready_async, td::Status::Error("Canceled"), id_);
} }
} }
void on_connected() override { void on_connected() final {
// nothing to do // nothing to do
} }

View File

@ -41,7 +41,7 @@ namespace telegram_bot_api {
struct ClientParameters; struct ClientParameters;
class WebhookActor : public td::HttpOutboundConnection::Callback { class WebhookActor final : public td::HttpOutboundConnection::Callback {
public: public:
class Callback : public td::Actor { class Callback : public td::Actor {
public: public:
@ -139,7 +139,7 @@ class WebhookActor : public td::HttpOutboundConnection::Callback {
double next_ip_address_resolve_time_ = 0; double next_ip_address_resolve_time_ = 0;
td::FutureActor<td::IPAddress> future_ip_address_; td::FutureActor<td::IPAddress> future_ip_address_;
class Connection : public td::ListNode { class Connection final : public td::ListNode {
public: public:
Connection() = default; Connection() = default;
Connection(const Connection &) = delete; Connection(const Connection &) = delete;
@ -190,26 +190,26 @@ class WebhookActor : public td::HttpOutboundConnection::Callback {
td::Status send_update() TD_WARN_UNUSED_RESULT; td::Status send_update() TD_WARN_UNUSED_RESULT;
void send_updates(); void send_updates();
void loop() override; void loop() final;
void handle(td::unique_ptr<td::HttpQuery> response) override; void handle(td::unique_ptr<td::HttpQuery> response) final;
void hangup_shared() override; void hangup_shared() final;
void hangup() override; void hangup() final;
void tear_down() override; void tear_down() final;
void start_up() override; void start_up() final;
bool check_ip_address(const td::IPAddress &addr) const; bool check_ip_address(const td::IPAddress &addr) const;
void on_error(td::Status status); void on_error(td::Status status);
void on_connection_error(td::Status error) override; void on_connection_error(td::Status error) final;
void on_webhook_error(td::Slice error); void on_webhook_error(td::Slice error);
void on_webhook_verified(); void on_webhook_verified();
}; };
class JsonUpdate : public td::Jsonable { class JsonUpdate final : public td::Jsonable {
public: public:
JsonUpdate(td::int32 id, td::Slice update) : id_(id), update_(update) { JsonUpdate(td::int32 id, td::Slice update) : id_(id), update_(update) {
} }

View File

@ -310,16 +310,16 @@ int main(int argc, char *argv[]) {
options.add_checked_option('c', "max-connections", "maximum number of open file descriptors", options.add_checked_option('c', "max-connections", "maximum number of open file descriptors",
td::OptionParser::parse_integer(max_connections)); td::OptionParser::parse_integer(max_connections));
options.add_checked_option( options.add_checked_option('\0', "proxy",
'\0', "proxy", PSLICE() << "HTTP proxy server for outgoing webhook requests in the format http://host:port", "HTTP proxy server for outgoing webhook requests in the format http://host:port",
[&](td::Slice address) { [&](td::Slice address) {
if (td::begins_with(address, "http://")) { if (td::begins_with(address, "http://")) {
address.remove_prefix(7); address.remove_prefix(7);
} else if (td::begins_with(address, "https://")) { } else if (td::begins_with(address, "https://")) {
address.remove_prefix(8); address.remove_prefix(8);
} }
return parameters->webhook_proxy_ip_address_.init_host_port(address.str()); return parameters->webhook_proxy_ip_address_.init_host_port(address.str());
}); });
options.add_check([&] { options.add_check([&] {
if (parameters->api_id_ <= 0 || parameters->api_hash_.empty()) { if (parameters->api_id_ <= 0 || parameters->api_hash_.empty()) {
return td::Status::Error("You must provide valid api-id and api-hash obtained at https://my.telegram.org"); return td::Status::Error("You must provide valid api-id and api-hash obtained at https://my.telegram.org");