use std::collections::HashMap;
use std::sync::Arc;
use crate::rate_limiter::RateLimiter;
use crate::util::rwlock_ext::RwLockRecover;
use super::runtime_update::{RuntimeOptionChanges, apply_runtime_option_value};
impl super::super::RequestGroup {
pub(crate) fn bt_max_peers_limit(&self) -> Arc<std::sync::atomic::AtomicUsize> {
Arc::clone(&self.bt_max_peers_limit)
}
pub fn set_rate_limiter(&self, limiter: RateLimiter) {
*self.rate_limiter.recover_mut() = Some(limiter);
}
pub fn set_pending_options(
&self,
changes: std::collections::HashMap<String, serde_json::Value>,
) {
if let Ok(mut pending) = self.pending_options.write() {
pending.extend(changes);
}
}
pub fn apply_pending_options(&mut self) {
let changes = self
.pending_options
.write()
.map(|mut pending| std::mem::take(&mut *pending))
.unwrap_or_default();
for (key, value) in changes {
if let Err(error) = self.try_update_option(&key, value) {
tracing::warn!(%key, %error, "Deferred runtime option was rejected");
}
}
}
pub fn pending_options(&self) -> std::collections::HashMap<String, serde_json::Value> {
self.pending_options
.read()
.map(|pending| pending.clone())
.unwrap_or_default()
}
pub fn runtime_options(&self) -> std::collections::HashMap<String, serde_json::Value> {
self.runtime_options
.read()
.map(|options| options.clone())
.unwrap_or_default()
}
pub(crate) fn classify_runtime_options(
&self,
changes: HashMap<String, serde_json::Value>,
) -> Result<RuntimeOptionChanges, String> {
let is_running = self.status().is_running();
let mut classified = RuntimeOptionChanges::default();
for (key, value) in changes {
match crate::config::is_option_changeable(&key, is_running) {
crate::config::ChangeableKind::Immediate => {
if Self::validate_option_update(&key, &value)? {
classified.immediate.insert(key, value);
}
}
crate::config::ChangeableKind::Pending => {
if Self::validate_option_update(&key, &value)? {
classified.pending.insert(key, value);
}
}
crate::config::ChangeableKind::NotChangeable => {}
}
}
Ok(classified)
}
pub(crate) fn apply_runtime_options(
&mut self,
changes: HashMap<String, serde_json::Value>,
) -> Result<(), String> {
for (key, value) in &changes {
if !Self::validate_option_update(key, value)? {
return Err(format!("Option '{}' cannot be changed at runtime", key));
}
}
for (key, value) in changes {
if !self.try_update_option(&key, value)? {
return Err(format!("Option '{}' cannot be changed at runtime", key));
}
}
Ok(())
}
pub fn validate_option_update(key: &str, value: &serde_json::Value) -> Result<bool, String> {
let registry = crate::config::OptionRegistry::new();
if registry.get(key).is_some() {
registry
.parse_rpc_value(key, value)
.map_err(|error| format!("Option '{}': {}", key, error))?;
}
let mut options = super::super::DownloadOptions::default();
apply_runtime_option_value(&mut options, key, value)
}
pub fn try_update_option(
&mut self,
key: &str,
value: serde_json::Value,
) -> Result<bool, String> {
if !Self::validate_option_update(key, &value)? {
return Ok(false);
}
let opts = Arc::make_mut(&mut self.options);
let applied = apply_runtime_option_value(opts, key, &value)?;
if key == "bt-max-peers" {
self.bt_max_peers_limit
.store(opts.bt_max_peers, std::sync::atomic::Ordering::Release);
}
if key == "enable-peer-exchange" {
self.bt_peer_exchange_enabled.store(
opts.enable_peer_exchange,
std::sync::atomic::Ordering::Release,
);
}
match key {
"max-download-limit" => {
if let Some(ref limiter) = *self.rate_limiter.recover() {
limiter.set_download_rate(opts.max_download_limit);
}
}
"max-upload-limit" => {
if let Some(ref limiter) = *self.rate_limiter.recover() {
limiter.set_upload_rate(opts.max_upload_limit);
}
}
"split" => {
tracing::warn!(
new_split = opts.split,
"split changed but will take effect on download restart/retry, \
not mid-download (current segments unchanged)"
);
}
_ => {}
}
if applied {
if let Ok(mut runtime_options) = self.runtime_options.write() {
runtime_options.insert(key.to_string(), value);
}
Ok(true)
} else {
Ok(false)
}
}
}