From 116830718906e1d079ecc60996b94ffb3403f9f7 Mon Sep 17 00:00:00 2001 From: Igor Katson Date: Sat, 16 Nov 2024 11:11:56 +0000 Subject: [PATCH] change RwLock> to ArcSwapOption --- Cargo.lock | 7 +++++++ crates/librqbit/Cargo.toml | 1 + crates/librqbit/src/limits.rs | 10 +++++----- 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 037062c..92ef1c9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -129,6 +129,12 @@ version = "1.0.89" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "86fdf8605db99b54d3cd748a44c6d04df638eb5dafb219b135d0149bd0db01f6" +[[package]] +name = "arc-swap" +version = "1.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "69f7f8c3906b62b754cd5326047894316021dcfe5a194c8ea52bdd94934a3457" + [[package]] name = "assert_cfg" version = "0.1.0" @@ -2604,6 +2610,7 @@ name = "librqbit" version = "7.1.0-beta.1" dependencies = [ "anyhow", + "arc-swap", "async-backtrace", "async-stream", "async-trait", diff --git a/crates/librqbit/Cargo.toml b/crates/librqbit/Cargo.toml index 852b10f..66a5a7c 100644 --- a/crates/librqbit/Cargo.toml +++ b/crates/librqbit/Cargo.toml @@ -110,6 +110,7 @@ async-trait = "0.1.81" async-backtrace = { version = "0.2", optional = true } notify = { version = "6.1.1", optional = true } walkdir = "2.5.0" +arc-swap = "1.7.1" [build-dependencies] anyhow = "1" diff --git a/crates/librqbit/src/limits.rs b/crates/librqbit/src/limits.rs index 935a5ec..3259894 100644 --- a/crates/librqbit/src/limits.rs +++ b/crates/librqbit/src/limits.rs @@ -1,6 +1,6 @@ +use arc_swap::ArcSwapOption; use governor::DefaultDirectRateLimiter as RateLimiter; use governor::Quota; -use parking_lot::RwLock; use serde::Deserialize; use serde::Serialize; use std::num::NonZero; @@ -13,7 +13,7 @@ pub struct LimitsConfig { pub download_bps: Option>, } -struct Limit(RwLock>>); +struct Limit(ArcSwapOption); impl Limit { fn new_inner(bps: Option>) -> Option> { @@ -22,11 +22,11 @@ impl Limit { } fn new(bps: Option>) -> Self { - Self(RwLock::new(Self::new_inner(bps))) + Self(ArcSwapOption::new(Self::new_inner(bps))) } async fn acquire(&self, size: NonZero) -> anyhow::Result<()> { - let lim = self.0.read().clone(); + let lim = self.0.load().clone(); if let Some(rl) = lim.as_ref() { rl.until_n_ready(size).await?; } @@ -35,7 +35,7 @@ impl Limit { fn set(&self, limit: Option>) { let new = Self::new_inner(limit); - *self.0.write() = new; + self.0.swap(new); } }