From 33c7498f47bc53a57452ae1b405cb5dadea661a3 Mon Sep 17 00:00:00 2001 From: Martin Hoffmann Date: Thu, 5 Nov 2020 11:45:32 +0100 Subject: [PATCH] Switch Arc>> to ArcSwap<_>. --- Cargo.toml | 1 + src/config.rs | 6 +++--- src/metrics.rs | 15 +++++++-------- src/targets/rtr.rs | 20 +++++++++++--------- 4 files changed, 22 insertions(+), 20 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index c9acad0..927b209 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,6 +10,7 @@ license = "BSD-3-Clause" readme = "README.md" [dependencies] +arc-swap = "0.4.7" chrono = "0.4.11" clap = "2.33" crossbeam-utils = "0.7.2" diff --git a/src/config.rs b/src/config.rs index eb93652..42e91ae 100644 --- a/src/config.rs +++ b/src/config.rs @@ -84,13 +84,13 @@ pub struct Source { /// The optional path of a config file. /// /// If this in `None`, the source is an interactive session. - path: Option>, + path: Option>, } impl<'a, T: AsRef> From<&'a T> for Source { fn from(path: &'a T) -> Source { Source { - path: Some(Arc::new(format!("{}", path.as_ref().display()))) + path: Some(format!("{}", path.as_ref().display()).into()) } } } @@ -148,7 +148,7 @@ impl Marked { /// Formats the mark for displaying. pub fn format_mark(&self, f: &mut fmt::Formatter) -> fmt::Result { let source = self.source.as_ref().and_then(|source| - source.path.as_ref().map(|path| path.as_str()) + source.path.as_ref() ); match (source, self.pos) { (Some(source), Some(pos)) => { diff --git a/src/metrics.rs b/src/metrics.rs index 48f1ada..c38ccfd 100644 --- a/src/metrics.rs +++ b/src/metrics.rs @@ -1,8 +1,9 @@ //! Metrics. use std::{fmt, iter}; -use std::sync::{Arc, Mutex, Weak}; +use std::sync::{Arc, Weak}; use std::fmt::Write; +use arc_swap::ArcSwap; use clap::{crate_name, crate_version}; @@ -15,14 +16,12 @@ const PROMETHEUS_PREFIX: &str = "rtrtr"; #[derive(Clone, Default)] pub struct Collection { - sources: Arc>>>, + sources: ArcSwap>, } impl Collection { pub fn register(&self, name: Arc, source: Weak) { - let mut sources = self.sources.lock().unwrap(); - - let old_sources = sources.clone(); + let old_sources = self.sources.load(); let mut new_sources = Vec::new(); for item in old_sources.iter() { if item.source.strong_count() > 0 { @@ -32,11 +31,11 @@ impl Collection { new_sources.push( RegisteredSource { name, source } ); - *sources = new_sources.into() + self.sources.store(new_sources.into()) } pub fn assemble(&self, format: OutputFormat) -> String { - let sources = self.sources.lock().unwrap().clone(); + let sources = self.sources.load(); let mut target = Target::new(format); for item in sources.iter() { if let Some(source) = item.source.upgrade() { @@ -50,7 +49,7 @@ impl Collection { impl fmt::Debug for Collection { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - let len = self.sources.lock().unwrap().len(); + let len = self.sources.load().len(); write!(f, "Collection({} sources)", len) } } diff --git a/src/targets/rtr.rs b/src/targets/rtr.rs index 564f045..4a65b79 100644 --- a/src/targets/rtr.rs +++ b/src/targets/rtr.rs @@ -1,9 +1,10 @@ /// RTR servers as a target. use std::cmp; -use std::sync::{Arc, Mutex}; +use std::sync::Arc; use std::net::SocketAddr; use std::net::TcpListener as StdTcpListener; +use arc_swap::ArcSwap; use log::{debug, error}; use serde_derive::Deserialize; use rpki_rtr::payload::Timing; @@ -73,17 +74,18 @@ impl Tcp { } + //------------ Source -------------------------------------------------------- #[derive(Clone, Default)] struct Source { - data: Arc>>, + data: ArcSwap, diff_num: usize, } impl Source { fn update(&self, update: payload::Update) { - let data = self.data.lock().unwrap().clone(); + let data = self.data.load(); let new_data = match data.current.as_ref() { None => { @@ -129,7 +131,7 @@ impl Source { } }; - *self.data.lock().unwrap() = Arc::new(new_data); + self.data.store(new_data.into()); } } @@ -138,15 +140,15 @@ impl VrpSource for Source { type DiffIter = payload::DiffIter; fn ready(&self) -> bool { - self.data.lock().unwrap().current.is_some() + self.data.load().current.is_some() } fn notify(&self) -> State { - self.data.lock().unwrap().state + self.data.load().state } fn full(&self) -> (State, Self::FullIter) { - let this = self.data.lock().unwrap(); + let this = self.data.load(); match this.current.as_ref() { Some(current) => (this.state, current.clone().into()), None => (this.state, Arc::new(payload::Set::default()).into()) @@ -154,7 +156,7 @@ impl VrpSource for Source { } fn diff(&self, state: State) -> Option<(State, Self::DiffIter)> { - let this = self.data.lock().unwrap(); + let this = self.data.load(); if this.current.is_none() || state.session() != this.state.session() { return None } @@ -165,7 +167,7 @@ impl VrpSource for Source { } fn timing(&self) -> Timing { - self.data.lock().unwrap().timing + self.data.load().timing } }