change RwLock<Option<Arc>> to ArcSwapOption
This commit is contained in:
parent
855e7ccaeb
commit
1168307189
3 changed files with 13 additions and 5 deletions
7
Cargo.lock
generated
7
Cargo.lock
generated
|
|
@ -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",
|
||||
|
|
|
|||
|
|
@ -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"
|
||||
|
|
|
|||
|
|
@ -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<NonZero<u32>>,
|
||||
}
|
||||
|
||||
struct Limit(RwLock<Option<Arc<RateLimiter>>>);
|
||||
struct Limit(ArcSwapOption<RateLimiter>);
|
||||
|
||||
impl Limit {
|
||||
fn new_inner(bps: Option<NonZero<u32>>) -> Option<Arc<RateLimiter>> {
|
||||
|
|
@ -22,11 +22,11 @@ impl Limit {
|
|||
}
|
||||
|
||||
fn new(bps: Option<NonZero<u32>>) -> Self {
|
||||
Self(RwLock::new(Self::new_inner(bps)))
|
||||
Self(ArcSwapOption::new(Self::new_inner(bps)))
|
||||
}
|
||||
|
||||
async fn acquire(&self, size: NonZero<u32>) -> 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<NonZero<u32>>) {
|
||||
let new = Self::new_inner(limit);
|
||||
*self.0.write() = new;
|
||||
self.0.swap(new);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue