From 9f688af4fbc76e2da48985a7241325c1b5d8d369 Mon Sep 17 00:00:00 2001 From: levlam Date: Tue, 25 Jul 2023 22:32:05 +0300 Subject: [PATCH] Add dedicated threads for TQueue and webhook databases and webhook certificate processing. --- telegram-bot-api/Client.cpp | 4 ++-- telegram-bot-api/ClientManager.cpp | 4 ++-- telegram-bot-api/ClientParameters.h | 17 +++++++++++------ telegram-bot-api/WebhookActor.cpp | 11 ++++++----- 4 files changed, 21 insertions(+), 15 deletions(-) diff --git a/telegram-bot-api/Client.cpp b/telegram-bot-api/Client.cpp index 7597b5e..d136f64 100644 --- a/telegram-bot-api/Client.cpp +++ b/telegram-bot-api/Client.cpp @@ -9874,7 +9874,7 @@ void Client::webhook_error(td::Status status) { void Client::webhook_closed(td::Status status) { if (has_webhook_certificate_) { - td::Scheduler::instance()->run_on_scheduler(SharedData::get_database_scheduler_id(), + td::Scheduler::instance()->run_on_scheduler(SharedData::get_webhook_certificate_scheduler_id(), [actor_id = actor_id(this), path = get_webhook_certificate_path(), status = std::move(status)](td::Unit) mutable { LOG(INFO) << "Unlink certificate " << path; @@ -9987,7 +9987,7 @@ 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( - SharedData::get_database_scheduler_id(), + SharedData::get_webhook_certificate_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; diff --git a/telegram-bot-api/ClientManager.cpp b/telegram-bot-api/ClientManager.cpp index 1bad19d..b94ab75 100644 --- a/telegram-bot-api/ClientManager.cpp +++ b/telegram-bot-api/ClientManager.cpp @@ -331,7 +331,7 @@ void ClientManager::start_up() { } auto concurrent_binlog = - std::make_shared(std::move(binlog), SharedData::get_database_scheduler_id()); + std::make_shared(std::move(binlog), SharedData::get_binlog_scheduler_id()); auto concurrent_tqueue_binlog = td::make_unique>(); concurrent_tqueue_binlog->set_binlog(std::move(concurrent_binlog)); tqueue->set_callback(std::move(concurrent_tqueue_binlog)); @@ -346,7 +346,7 @@ void ClientManager::start_up() { // init webhook_db auto concurrent_webhook_db = td::make_unique>(); auto status = concurrent_webhook_db->init(parameters_->working_directory_ + "webhooks_db.binlog", td::DbKey::empty(), - SharedData::get_database_scheduler_id()); + SharedData::get_binlog_scheduler_id()); LOG_IF(FATAL, status.is_error()) << "Can't open webhooks_db.binlog " << status; parameters_->shared_data_->webhook_db_ = std::move(concurrent_webhook_db); diff --git a/telegram-bot-api/ClientParameters.h b/telegram-bot-api/ClientParameters.h index b233138..b22031d 100644 --- a/telegram-bot-api/ClientParameters.h +++ b/telegram-bot-api/ClientParameters.h @@ -53,11 +53,6 @@ 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; @@ -88,9 +83,19 @@ struct SharedData { return 8; } - static td::int32 get_thread_count() { + static td::int32 get_binlog_scheduler_id() { + // the thread for TQueue and webhook binlogs return 9; } + + static td::int32 get_webhook_certificate_scheduler_id() { + // the thread for webhook certificate processing + return 10; + } + + static td::int32 get_thread_count() { + return 11; + } }; struct ClientParameters { diff --git a/telegram-bot-api/WebhookActor.cpp b/telegram-bot-api/WebhookActor.cpp index 1cd3661..e2870b6 100644 --- a/telegram-bot-api/WebhookActor.cpp +++ b/telegram-bot-api/WebhookActor.cpp @@ -724,11 +724,12 @@ void WebhookActor::start_up() { if (url_.protocol_ != td::HttpUrl::Protocol::Http && !stop_flag_) { // asynchronously create SSL context - td::Scheduler::instance()->run_on_scheduler( - SharedData::get_database_scheduler_id(), [actor_id = actor_id(this), cert_path = cert_path_](td::Unit) mutable { - send_closure(actor_id, &WebhookActor::on_ssl_context_created, - td::SslCtx::create(cert_path, td::SslCtx::VerifyPeer::On)); - }); + td::Scheduler::instance()->run_on_scheduler(SharedData::get_webhook_certificate_scheduler_id(), + [actor_id = actor_id(this), cert_path = cert_path_](td::Unit) mutable { + send_closure( + actor_id, &WebhookActor::on_ssl_context_created, + td::SslCtx::create(cert_path, td::SslCtx::VerifyPeer::On)); + }); } yield();