diff --git a/telegram-bot-api/Client.cpp b/telegram-bot-api/Client.cpp index 054e73a..432d6ea 100644 --- a/telegram-bot-api/Client.cpp +++ b/telegram-bot-api/Client.cpp @@ -187,9 +187,9 @@ Client::Client(td::ActorShared<> parent, const td::string &bot_token, bool is_te } Client::~Client() { - td::Scheduler::instance()->destroy_on_scheduler(get_file_gc_scheduler_id(), messages_, users_, groups_, supergroups_, - chats_, reply_message_ids_, yet_unsent_reply_message_ids_, - sticker_set_names_); + td::Scheduler::instance()->destroy_on_scheduler(SharedData::get_file_gc_scheduler_id(), messages_, users_, groups_, + supergroups_, chats_, reply_message_ids_, + yet_unsent_reply_message_ids_, sticker_set_names_); } bool Client::init_methods() { @@ -4833,7 +4833,7 @@ void Client::on_update(object_ptr result) { deleted_messages.push_back(std::move(deleted_message)); } } - td::Scheduler::instance()->destroy_on_scheduler(get_file_gc_scheduler_id(), deleted_messages); + td::Scheduler::instance()->destroy_on_scheduler(SharedData::get_file_gc_scheduler_id(), deleted_messages); break; } case td_api::updateFile::ID: { @@ -5168,7 +5168,7 @@ void Client::on_closed() { if (logging_out_) { parameters_->shared_data_->webhook_db_->erase(bot_token_with_dc_); - td::Scheduler::instance()->run_on_scheduler(get_file_gc_scheduler_id(), + td::Scheduler::instance()->run_on_scheduler(SharedData::get_file_gc_scheduler_id(), [actor_id = actor_id(this), dir = dir_](td::Unit) { CHECK(dir.size() >= 24); CHECK(dir.back() == TD_DIR_SLASH); @@ -5194,16 +5194,6 @@ void Client::timeout_expired() { stop(); } -td::int32 Client::get_database_scheduler_id() { - // the same scheduler as for database in Td - return 1; -} - -td::int32 Client::get_file_gc_scheduler_id() { - // the same scheduler as for file GC in Td - return 2; -} - void Client::clear_tqueue() { CHECK(webhook_id_.empty()); auto &tqueue = parameters_->shared_data_->tqueue_; @@ -9034,7 +9024,7 @@ void Client::webhook_error(Status status) { void Client::webhook_closed(Status status) { if (has_webhook_certificate_) { - td::Scheduler::instance()->run_on_scheduler(get_database_scheduler_id(), + td::Scheduler::instance()->run_on_scheduler(SharedData::get_database_scheduler_id(), [actor_id = actor_id(this), path = get_webhook_certificate_path(), status = std::move(status)](td::Unit) mutable { LOG(INFO) << "Unlink certificate " << path; @@ -9147,8 +9137,9 @@ void Client::do_set_webhook(PromisedQueryPtr query, bool was_deleted) { CHECK(!webhook_set_query_); active_webhook_set_query_ = std::move(query); td::Scheduler::instance()->run_on_scheduler( - get_database_scheduler_id(), [actor_id = actor_id(this), from_path = cert_file_ptr->temp_file_name, - to_path = get_webhook_certificate_path(), size](td::Unit) mutable { + SharedData::get_database_scheduler_id(), + [actor_id = actor_id(this), from_path = cert_file_ptr->temp_file_name, + to_path = get_webhook_certificate_path(), size](td::Unit) mutable { LOG(INFO) << "Copy certificate to " << to_path; auto status = td::copy_file(from_path, to_path, size); send_closure(actor_id, &Client::on_webhook_certificate_copied, std::move(status)); diff --git a/telegram-bot-api/Client.h b/telegram-bot-api/Client.h index 6cf8612..226cbba 100644 --- a/telegram-bot-api/Client.h +++ b/telegram-bot-api/Client.h @@ -320,10 +320,6 @@ class Client final : public WebhookActor::Callback { void on_closed(); void finish_closing(); - static int32 get_database_scheduler_id(); - - static int32 get_file_gc_scheduler_id(); - void clear_tqueue(); bool allow_update_before_authorization(const td_api::Object *update) const; diff --git a/telegram-bot-api/ClientManager.cpp b/telegram-bot-api/ClientManager.cpp index 01273d3..602ab12 100644 --- a/telegram-bot-api/ClientManager.cpp +++ b/telegram-bot-api/ClientManager.cpp @@ -236,7 +236,7 @@ void ClientManager::get_stats(td::Promise promise, } sb << "buffer_memory\t" << td::format::as_size(td::BufferAllocator::get_buffer_mem()) << '\n'; - sb << "active_webhook_connections\t" << WebhookActor::get_total_connections_count() << '\n'; + sb << "active_webhook_connections\t" << WebhookActor::get_total_connection_count() << '\n'; sb << "active_requests\t" << parameters_->shared_data_->query_count_.load(std::memory_order_relaxed) << '\n'; sb << "active_network_queries\t" << td::get_pending_network_query_count(*parameters_->net_query_stats_) << '\n'; auto stats = stat_.as_vector(now); diff --git a/telegram-bot-api/ClientParameters.h b/telegram-bot-api/ClientParameters.h index b4d8f93..fab4c3a 100644 --- a/telegram-bot-api/ClientParameters.h +++ b/telegram-bot-api/ClientParameters.h @@ -52,6 +52,16 @@ struct SharedData { } return static_cast(result); } + + static td::int32 get_database_scheduler_id() { + // the same scheduler as for database in Td + return 1; + } + + static td::int32 get_file_gc_scheduler_id() { + // the same scheduler as for file GC in Td + return 2; + } }; struct ClientParameters { diff --git a/telegram-bot-api/WebhookActor.cpp b/telegram-bot-api/WebhookActor.cpp index 89f30a5..0df912a 100644 --- a/telegram-bot-api/WebhookActor.cpp +++ b/telegram-bot-api/WebhookActor.cpp @@ -35,7 +35,7 @@ namespace telegram_bot_api { static int VERBOSITY_NAME(webhook) = VERBOSITY_NAME(DEBUG); -std::atomic WebhookActor::total_connections_count_{0}; +std::atomic WebhookActor::total_connection_count_{0}; WebhookActor::WebhookActor(td::ActorShared callback, td::int64 tqueue_id, td::HttpUrl url, td::string cert_path, td::int32 max_connections, bool from_db_flag, @@ -73,6 +73,11 @@ WebhookActor::WebhookActor(td::ActorShared callback, td::int64 tqueue_ << ", max_connections = " << max_connections_; } +WebhookActor::~WebhookActor() { + td::Scheduler::instance()->destroy_on_scheduler(SharedData::get_file_gc_scheduler_id(), update_map_, queue_updates_, + queues_); +} + void WebhookActor::relax_wakeup_at(double wakeup_at, const char *source) { if (wakeup_at_ == 0 || wakeup_at < wakeup_at_) { VLOG(webhook) << "Wake up in " << wakeup_at - td::Time::now() << " from " << source; @@ -232,7 +237,7 @@ td::Status WebhookActor::create_connection(td::BufferedFd fd) { conn->event_id_ = {}; conn->id_ = id; ready_connections_.put(conn->to_list_node()); - total_connections_count_.fetch_add(1, std::memory_order_relaxed); + total_connection_count_.fetch_add(1, std::memory_order_relaxed); if (!was_checked_) { was_checked_ = true; @@ -657,7 +662,7 @@ void WebhookActor::handle(td::unique_ptr response) { if (need_close || close_connection) { VLOG(webhook) << "Close connection " << connection_id; connections_.erase(connection_ptr->id_); - total_connections_count_.fetch_sub(1, std::memory_order_relaxed); + total_connection_count_.fetch_sub(1, std::memory_order_relaxed); } else { ready_connections_.put(connection_ptr->to_list_node()); } @@ -725,7 +730,7 @@ void WebhookActor::close() { } void WebhookActor::tear_down() { - total_connections_count_.fetch_sub(connections_.size(), std::memory_order_relaxed); + total_connection_count_.fetch_sub(connections_.size(), std::memory_order_relaxed); } void WebhookActor::on_webhook_verified() { diff --git a/telegram-bot-api/WebhookActor.h b/telegram-bot-api/WebhookActor.h index 2f68fbe..869d064 100644 --- a/telegram-bot-api/WebhookActor.h +++ b/telegram-bot-api/WebhookActor.h @@ -55,13 +55,18 @@ class WebhookActor final : public td::HttpOutboundConnection::Callback { WebhookActor(td::ActorShared callback, td::int64 tqueue_id, td::HttpUrl url, td::string cert_path, td::int32 max_connections, bool from_db_flag, td::string cached_ip_address, bool fix_ip_address, td::string secret_token, std::shared_ptr parameters); + WebhookActor(const WebhookActor &) = delete; + WebhookActor &operator=(const WebhookActor &) = delete; + WebhookActor(WebhookActor &&) = delete; + WebhookActor &operator=(WebhookActor &&) = delete; + ~WebhookActor(); void update(); void close(); - static td::int64 get_total_connections_count() { - return total_connections_count_; + static td::int64 get_total_connection_count() { + return total_connection_count_; } private: @@ -70,7 +75,7 @@ class WebhookActor final : public td::HttpOutboundConnection::Callback { static constexpr int WEBHOOK_MAX_RESEND_TIMEOUT = 60; static constexpr int WEBHOOK_DROP_TIMEOUT = 60 * 60 * 23; - static std::atomic total_connections_count_; + static std::atomic total_connection_count_; td::ActorShared callback_; td::int64 tqueue_id_;