Add "disable_network_statistics" option.

This commit is contained in:
levlam 2023-03-13 17:26:23 +03:00
parent 2baff56b7a
commit 32d0433c26
5 changed files with 54 additions and 37 deletions

View File

@ -653,6 +653,9 @@ void OptionManager::set_option(const string &name, td_api::object_ptr<td_api::Op
if (!is_bot && set_boolean_option("disable_top_chats")) {
return;
}
if (set_boolean_option("disable_network_statistics")) {
return;
}
if (set_boolean_option("disable_persistent_network_statistics")) {
return;
}

View File

@ -3573,6 +3573,28 @@ void Td::init(Result<TdDb::OpenedDatabase> r_opened_database) {
}
});
if (!option_manager_->get_option_boolean("disable_network_statistics")) {
net_stats_manager_ = create_actor<NetStatsManager>("NetStatsManager", create_reference());
// How else could I let two actor know about each other, without quite complex async logic?
auto net_stats_manager_ptr = net_stats_manager_.get_actor_unsafe();
net_stats_manager_ptr->init();
G()->connection_creator().get_actor_unsafe()->set_net_stats_callback(
net_stats_manager_ptr->get_common_stats_callback(), net_stats_manager_ptr->get_media_stats_callback());
G()->set_net_stats_file_callbacks(net_stats_manager_ptr->get_file_stats_callbacks());
}
complete_pending_preauthentication_requests([](int32 id) {
switch (id) {
case td_api::getNetworkStatistics::ID:
case td_api::addNetworkStatistics::ID:
case td_api::resetNetworkStatistics::ID:
return true;
default:
return false;
}
});
if (events.since_last_open >= 3600) {
auto old_since_last_open = option_manager_->get_option_integer("since_last_open");
if (events.since_last_open > old_since_last_open) {
@ -3725,38 +3747,12 @@ void Td::init_options_and_network() {
option_manager_ = make_unique<OptionManager>(this);
G()->set_option_manager(option_manager_.get());
init_connection_creator();
VLOG(td_init) << "Create TempAuthKeyWatchdog";
auto temp_auth_key_watchdog = create_actor<TempAuthKeyWatchdog>("TempAuthKeyWatchdog", create_reference());
G()->set_temp_auth_key_watchdog(std::move(temp_auth_key_watchdog));
VLOG(td_init) << "Create ConfigManager";
config_manager_ = create_actor<ConfigManager>("ConfigManager", create_reference());
G()->set_config_manager(config_manager_.get());
}
void Td::init_connection_creator() {
VLOG(td_init) << "Create ConnectionCreator";
auto connection_creator = create_actor<ConnectionCreator>("ConnectionCreator", create_reference());
auto net_stats_manager = create_actor<NetStatsManager>("NetStatsManager", create_reference());
// How else could I let two actor know about each other, without quite complex async logic?
auto net_stats_manager_ptr = net_stats_manager.get_actor_unsafe();
net_stats_manager_ptr->init();
connection_creator.get_actor_unsafe()->set_net_stats_callback(net_stats_manager_ptr->get_common_stats_callback(),
net_stats_manager_ptr->get_media_stats_callback());
G()->set_net_stats_file_callbacks(net_stats_manager_ptr->get_file_stats_callbacks());
G()->set_connection_creator(std::move(connection_creator));
net_stats_manager_ = std::move(net_stats_manager);
G()->set_connection_creator(create_actor<ConnectionCreator>("ConnectionCreator", create_reference()));
complete_pending_preauthentication_requests([](int32 id) {
switch (id) {
case td_api::setNetworkType::ID:
case td_api::getNetworkStatistics::ID:
case td_api::addNetworkStatistics::ID:
case td_api::resetNetworkStatistics::ID:
case td_api::addProxy::ID:
case td_api::editProxy::ID:
case td_api::enableProxy::ID:
@ -3769,6 +3765,13 @@ void Td::init_connection_creator() {
return false;
}
});
VLOG(td_init) << "Create TempAuthKeyWatchdog";
G()->set_temp_auth_key_watchdog(create_actor<TempAuthKeyWatchdog>("TempAuthKeyWatchdog", create_reference()));
VLOG(td_init) << "Create ConfigManager";
config_manager_ = create_actor<ConfigManager>("ConfigManager", create_reference());
G()->set_config_manager(config_manager_.get());
}
void Td::init_file_manager() {
@ -4843,6 +4846,9 @@ void Td::on_request(uint64 id, td_api::optimizeStorage &request) {
}
void Td::on_request(uint64 id, td_api::getNetworkStatistics &request) {
if (net_stats_manager_.empty()) {
return send_error_raw(id, 400, "Network statistics is disabled");
}
if (!request.only_current_ && G()->get_option_boolean("disable_persistent_network_statistics")) {
return send_error_raw(id, 400, "Persistent network statistics is disabled");
}
@ -4859,6 +4865,9 @@ void Td::on_request(uint64 id, td_api::getNetworkStatistics &request) {
}
void Td::on_request(uint64 id, td_api::resetNetworkStatistics &request) {
if (net_stats_manager_.empty()) {
return send_error_raw(id, 400, "Network statistics is disabled");
}
CREATE_OK_REQUEST_PROMISE();
send_closure(net_stats_manager_, &NetStatsManager::reset_network_stats);
promise.set_value(Unit());
@ -4868,6 +4877,9 @@ void Td::on_request(uint64 id, td_api::addNetworkStatistics &request) {
if (request.entry_ == nullptr) {
return send_error_raw(id, 400, "Network statistics entry must be non-empty");
}
if (net_stats_manager_.empty()) {
return send_error_raw(id, 400, "Network statistics is disabled");
}
NetworkStatsEntry entry;
switch (request.entry_->get_id()) {

View File

@ -1596,8 +1596,6 @@ class Td final : public Actor {
void init_options_and_network();
void init_connection_creator();
void init_file_manager();
void init_managers();

View File

@ -59,10 +59,14 @@ class StatsCallback final : public mtproto::RawConnection::StatsCallback {
}
void on_read(uint64 bytes) final {
net_stats_callback_->on_read(bytes);
if (net_stats_callback_ != nullptr) {
net_stats_callback_->on_read(bytes);
}
}
void on_write(uint64 bytes) final {
net_stats_callback_->on_write(bytes);
if (net_stats_callback_ != nullptr) {
net_stats_callback_->on_write(bytes);
}
}
void on_pong() final {

View File

@ -62,17 +62,17 @@ NetQuery::NetQuery(State state, uint64 id, BufferSlice &&query, BufferSlice &&an
}
void NetQuery::on_net_write(size_t size) {
if (file_type_ == -1) {
return;
const auto &callbacks = G()->get_net_stats_file_callbacks();
if (static_cast<size_t>(file_type_) < callbacks.size()) {
callbacks[file_type_]->on_write(size);
}
G()->get_net_stats_file_callbacks().at(file_type_)->on_write(size);
}
void NetQuery::on_net_read(size_t size) {
if (file_type_ == -1) {
return;
const auto &callbacks = G()->get_net_stats_file_callbacks();
if (static_cast<size_t>(file_type_) < callbacks.size()) {
callbacks[file_type_]->on_read(size);
}
G()->get_net_stats_file_callbacks().at(file_type_)->on_read(size);
}
int32 NetQuery::tl_magic(const BufferSlice &buffer_slice) {