1
0
mirror of https://github.com/d47081/qBittorrent.git synced 2025-01-22 20:44:15 +00:00

Avoid blocking call when changing libtorrent session settings

We don't really need to get currently used settings pack in order to apply changes to session settings. It is enough to apply settings pack that contains only updated settings.

PR #17989.
This commit is contained in:
Vladimir Golovnev 2022-11-09 08:02:34 +03:00 committed by GitHub
parent 2e4431f0b8
commit 6c9c40fd7c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 42 additions and 49 deletions

View File

@ -107,10 +107,10 @@ void PortForwarderImpl::removePorts(const QString &profile)
void PortForwarderImpl::start() void PortForwarderImpl::start()
{ {
lt::settings_pack settingsPack = m_provider->get_settings(); lt::settings_pack settingsPack;
settingsPack.set_bool(lt::settings_pack::enable_upnp, true); settingsPack.set_bool(lt::settings_pack::enable_upnp, true);
settingsPack.set_bool(lt::settings_pack::enable_natpmp, true); settingsPack.set_bool(lt::settings_pack::enable_natpmp, true);
m_provider->apply_settings(settingsPack); m_provider->apply_settings(std::move(settingsPack));
for (auto profileIter = m_portProfiles.begin(); profileIter != m_portProfiles.end(); ++profileIter) for (auto profileIter = m_portProfiles.begin(); profileIter != m_portProfiles.end(); ++profileIter)
{ {
@ -129,10 +129,10 @@ void PortForwarderImpl::start()
void PortForwarderImpl::stop() void PortForwarderImpl::stop()
{ {
lt::settings_pack settingsPack = m_provider->get_settings(); lt::settings_pack settingsPack;
settingsPack.set_bool(lt::settings_pack::enable_upnp, false); settingsPack.set_bool(lt::settings_pack::enable_upnp, false);
settingsPack.set_bool(lt::settings_pack::enable_natpmp, false); settingsPack.set_bool(lt::settings_pack::enable_natpmp, false);
m_provider->apply_settings(settingsPack); m_provider->apply_settings(std::move(settingsPack));
// don't clear m_portProfiles so a later `start()` call can restore the port forwardings // don't clear m_portProfiles so a later `start()` call can restore the port forwardings
for (auto profileIter = m_portProfiles.begin(); profileIter != m_portProfiles.end(); ++profileIter) for (auto profileIter = m_portProfiles.begin(); profileIter != m_portProfiles.end(); ++profileIter)

View File

@ -1066,25 +1066,25 @@ void SessionImpl::adjustLimits()
{ {
if (isQueueingSystemEnabled()) if (isQueueingSystemEnabled())
{ {
lt::settings_pack settingsPack = m_nativeSession->get_settings(); lt::settings_pack settingsPack;
adjustLimits(settingsPack); // Internally increase the queue limits to ensure that the magnet is started
m_nativeSession->apply_settings(settingsPack); settingsPack.set_int(lt::settings_pack::active_downloads, adjustLimit(maxActiveDownloads()));
settingsPack.set_int(lt::settings_pack::active_limit, adjustLimit(maxActiveTorrents()));
m_nativeSession->apply_settings(std::move(settingsPack));
} }
} }
void SessionImpl::applyBandwidthLimits() void SessionImpl::applyBandwidthLimits()
{ {
lt::settings_pack settingsPack = m_nativeSession->get_settings(); lt::settings_pack settingsPack;
applyBandwidthLimits(settingsPack); settingsPack.set_int(lt::settings_pack::download_rate_limit, downloadSpeedLimit());
m_nativeSession->apply_settings(settingsPack); settingsPack.set_int(lt::settings_pack::upload_rate_limit, uploadSpeedLimit());
m_nativeSession->apply_settings(std::move(settingsPack));
} }
void SessionImpl::configure() void SessionImpl::configure()
{ {
lt::settings_pack settingsPack = m_nativeSession->get_settings(); m_nativeSession->apply_settings(loadLTSettings());
loadLTSettings(settingsPack);
m_nativeSession->apply_settings(settingsPack);
configureComponents(); configureComponents();
m_deferredConfigureScheduled = false; m_deferredConfigureScheduled = false;
@ -1430,10 +1430,11 @@ void SessionImpl::endStartup(ResumeSessionContext *context)
void SessionImpl::initializeNativeSession() void SessionImpl::initializeNativeSession()
{ {
const std::string peerId = lt::generate_fingerprint(PEER_ID, QBT_VERSION_MAJOR, QBT_VERSION_MINOR, QBT_VERSION_BUGFIX, QBT_VERSION_BUILD); lt::settings_pack pack = loadLTSettings();
lt::settings_pack pack; const std::string peerId = lt::generate_fingerprint(PEER_ID, QBT_VERSION_MAJOR, QBT_VERSION_MINOR, QBT_VERSION_BUGFIX, QBT_VERSION_BUILD);
pack.set_str(lt::settings_pack::peer_fingerprint, peerId); pack.set_str(lt::settings_pack::peer_fingerprint, peerId);
pack.set_bool(lt::settings_pack::listen_system_port_fallback, false); pack.set_bool(lt::settings_pack::listen_system_port_fallback, false);
pack.set_str(lt::settings_pack::user_agent, USER_AGENT.toStdString()); pack.set_str(lt::settings_pack::user_agent, USER_AGENT.toStdString());
pack.set_bool(lt::settings_pack::use_dht_as_fallback, false); pack.set_bool(lt::settings_pack::use_dht_as_fallback, false);
@ -1450,8 +1451,7 @@ void SessionImpl::initializeNativeSession()
pack.set_bool(lt::settings_pack::enable_set_file_valid_data, true); pack.set_bool(lt::settings_pack::enable_set_file_valid_data, true);
#endif #endif
loadLTSettings(pack); lt::session_params sessionParams {std::move(pack), {}};
lt::session_params sessionParams {pack, {}};
#ifdef QBT_USES_LIBTORRENT2 #ifdef QBT_USES_LIBTORRENT2
switch (diskIOType()) switch (diskIOType())
{ {
@ -1509,28 +1509,14 @@ void SessionImpl::processBannedIPs(lt::ip_filter &filter)
} }
} }
void SessionImpl::adjustLimits(lt::settings_pack &settingsPack) const int SessionImpl::adjustLimit(const int limit) const
{ {
// Internally increase the queue limits to ensure that the magnet is started if (limit <= -1)
const auto adjustLimit = [this](const int limit) -> int return limit;
{ // check for overflow: (limit + m_extraLimit) < std::numeric_limits<int>::max()
if (limit <= -1) return (m_extraLimit < (std::numeric_limits<int>::max() - limit))
return limit; ? (limit + m_extraLimit)
// check for overflow: (limit + m_extraLimit) < std::numeric_limits<int>::max() : std::numeric_limits<int>::max();
return (m_extraLimit < (std::numeric_limits<int>::max() - limit))
? (limit + m_extraLimit)
: std::numeric_limits<int>::max();
};
settingsPack.set_int(lt::settings_pack::active_downloads, adjustLimit(maxActiveDownloads()));
settingsPack.set_int(lt::settings_pack::active_limit, adjustLimit(maxActiveTorrents()));
}
void SessionImpl::applyBandwidthLimits(lt::settings_pack &settingsPack) const
{
const bool altSpeedLimitEnabled = isAltGlobalSpeedLimitEnabled();
settingsPack.set_int(lt::settings_pack::download_rate_limit, altSpeedLimitEnabled ? altGlobalDownloadSpeedLimit() : globalDownloadSpeedLimit());
settingsPack.set_int(lt::settings_pack::upload_rate_limit, altSpeedLimitEnabled ? altGlobalUploadSpeedLimit() : globalUploadSpeedLimit());
} }
void SessionImpl::initMetrics() void SessionImpl::initMetrics()
@ -1575,8 +1561,10 @@ void SessionImpl::initMetrics()
m_metricIndices.disk.diskJobTime = findMetricIndex("disk.disk_job_time"); m_metricIndices.disk.diskJobTime = findMetricIndex("disk.disk_job_time");
} }
void SessionImpl::loadLTSettings(lt::settings_pack &settingsPack) lt::settings_pack SessionImpl::loadLTSettings() const
{ {
lt::settings_pack settingsPack;
const lt::alert_category_t alertMask = lt::alert::error_notification const lt::alert_category_t alertMask = lt::alert::error_notification
| lt::alert::file_progress_notification | lt::alert::file_progress_notification
| lt::alert::ip_block_notification | lt::alert::ip_block_notification
@ -1594,8 +1582,10 @@ void SessionImpl::loadLTSettings(lt::settings_pack &settingsPack)
// It will not take affect until the listen_interfaces settings is updated // It will not take affect until the listen_interfaces settings is updated
settingsPack.set_int(lt::settings_pack::listen_queue_size, socketBacklogSize()); settingsPack.set_int(lt::settings_pack::listen_queue_size, socketBacklogSize());
configureNetworkInterfaces(settingsPack); applyNetworkInterfacesSettings(settingsPack);
applyBandwidthLimits(settingsPack);
settingsPack.set_int(lt::settings_pack::download_rate_limit, downloadSpeedLimit());
settingsPack.set_int(lt::settings_pack::upload_rate_limit, uploadSpeedLimit());
// The most secure, rc4 only so that all streams are encrypted // The most secure, rc4 only so that all streams are encrypted
settingsPack.set_int(lt::settings_pack::allowed_enc_level, lt::settings_pack::pe_rc4); settingsPack.set_int(lt::settings_pack::allowed_enc_level, lt::settings_pack::pe_rc4);
@ -1730,7 +1720,9 @@ void SessionImpl::loadLTSettings(lt::settings_pack &settingsPack)
// Queueing System // Queueing System
if (isQueueingSystemEnabled()) if (isQueueingSystemEnabled())
{ {
adjustLimits(settingsPack); // Internally increase the queue limits to ensure that the magnet is started
settingsPack.set_int(lt::settings_pack::active_downloads, adjustLimit(maxActiveDownloads()));
settingsPack.set_int(lt::settings_pack::active_limit, adjustLimit(maxActiveTorrents()));
settingsPack.set_int(lt::settings_pack::active_seeds, maxActiveUploads()); settingsPack.set_int(lt::settings_pack::active_seeds, maxActiveUploads());
settingsPack.set_bool(lt::settings_pack::dont_count_slow_torrents, ignoreSlowTorrentsForQueueing()); settingsPack.set_bool(lt::settings_pack::dont_count_slow_torrents, ignoreSlowTorrentsForQueueing());
@ -1846,9 +1838,11 @@ void SessionImpl::loadLTSettings(lt::settings_pack &settingsPack)
settingsPack.set_int(lt::settings_pack::seed_choking_algorithm, lt::settings_pack::anti_leech); settingsPack.set_int(lt::settings_pack::seed_choking_algorithm, lt::settings_pack::anti_leech);
break; break;
} }
return settingsPack;
} }
void SessionImpl::configureNetworkInterfaces(lt::settings_pack &settingsPack) void SessionImpl::applyNetworkInterfacesSettings(lt::settings_pack &settingsPack) const
{ {
if (m_listenInterfaceConfigured) if (m_listenInterfaceConfigured)
return; return;

View File

@ -474,11 +474,10 @@ namespace BitTorrent
Q_INVOKABLE void configure(); Q_INVOKABLE void configure();
void configureComponents(); void configureComponents();
void initializeNativeSession(); void initializeNativeSession();
void loadLTSettings(lt::settings_pack &settingsPack); lt::settings_pack loadLTSettings() const;
void configureNetworkInterfaces(lt::settings_pack &settingsPack); void applyNetworkInterfacesSettings(lt::settings_pack &settingsPack) const;
void configurePeerClasses(); void configurePeerClasses();
void adjustLimits(lt::settings_pack &settingsPack) const; int adjustLimit(int limit) const;
void applyBandwidthLimits(lt::settings_pack &settingsPack) const;
void initMetrics(); void initMetrics();
void adjustLimits(); void adjustLimits();
void applyBandwidthLimits(); void applyBandwidthLimits();
@ -553,7 +552,7 @@ namespace BitTorrent
bool m_deferredConfigureScheduled = false; bool m_deferredConfigureScheduled = false;
bool m_IPFilteringConfigured = false; bool m_IPFilteringConfigured = false;
bool m_listenInterfaceConfigured = false; mutable bool m_listenInterfaceConfigured = false;
CachedSettingValue<bool> m_isDHTEnabled; CachedSettingValue<bool> m_isDHTEnabled;
CachedSettingValue<bool> m_isLSDEnabled; CachedSettingValue<bool> m_isLSDEnabled;