1
0
mirror of https://github.com/NLnetLabs/rtrtr.git synced 2024-05-11 05:55:07 +00:00

Switch Arc<Mutex<Arc<_>>> to ArcSwap<_>.

This commit is contained in:
Martin Hoffmann
2020-11-05 11:45:32 +01:00
parent d5517af148
commit 33c7498f47
4 changed files with 22 additions and 20 deletions

View File

@ -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"

View File

@ -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<Arc<String>>,
path: Option<Arc<str>>,
}
impl<'a, T: AsRef<Path>> 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<T> Marked<T> {
/// 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)) => {

View File

@ -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<Mutex<Arc<Vec<RegisteredSource>>>>,
sources: ArcSwap<Vec<RegisteredSource>>,
}
impl Collection {
pub fn register(&self, name: Arc<str>, source: Weak<dyn Source>) {
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)
}
}

View File

@ -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<Mutex<Arc<SourceData>>>,
data: ArcSwap<SourceData>,
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
}
}