From e5103d48bd635252d1f992b090d0f9443fca7ef7 Mon Sep 17 00:00:00 2001 From: _ <> Date: Sun, 20 Dec 2020 19:35:32 +0000 Subject: [PATCH] :heavy_plus_sign: update: add ArcSwap to update gauges lock-free --- Cargo.lock | 8 +++++++ crates/ptth_file_server_bin/Cargo.toml | 1 + crates/ptth_file_server_bin/src/main.rs | 23 ++++++++++++++++++- crates/ptth_server/Cargo.toml | 1 + crates/ptth_server/src/file_server/metrics.rs | 2 ++ crates/ptth_server/src/file_server/mod.rs | 3 +++ crates/ptth_server/src/lib.rs | 9 +++++--- 7 files changed, 43 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 659cf6f..55a1d67 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -46,6 +46,12 @@ version = "1.0.35" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2c0df63cb2955042487fad3aefd2c6e3ae7389ac5dc1beb28921de0b69f779d4" +[[package]] +name = "arc-swap" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec5a4539a733493f412c4d0bb962748ea1f90f3dfdba9ff3ee18acbefc3b33f0" + [[package]] name = "arrayref" version = "0.3.6" @@ -1636,6 +1642,7 @@ name = "ptth_file_server" version = "0.1.0" dependencies = [ "anyhow", + "arc-swap", "handlebars", "http", "hyper", @@ -1682,6 +1689,7 @@ dependencies = [ "aho-corasick", "always_equal", "anyhow", + "arc-swap", "base64 0.12.3", "blake3", "chrono", diff --git a/crates/ptth_file_server_bin/Cargo.toml b/crates/ptth_file_server_bin/Cargo.toml index 04b67b4..af3cf68 100644 --- a/crates/ptth_file_server_bin/Cargo.toml +++ b/crates/ptth_file_server_bin/Cargo.toml @@ -9,6 +9,7 @@ license = "AGPL-3.0" [dependencies] anyhow = "1.0.34" +arc-swap = "1.1.0" handlebars = "3.5.1" http = "0.2.1" hyper = "0.13.8" diff --git a/crates/ptth_file_server_bin/src/main.rs b/crates/ptth_file_server_bin/src/main.rs index 0fcd807..32a25cb 100644 --- a/crates/ptth_file_server_bin/src/main.rs +++ b/crates/ptth_file_server_bin/src/main.rs @@ -6,6 +6,7 @@ use std::{ sync::Arc, }; +use arc_swap::ArcSwap; use hyper::{ Body, Request, @@ -98,7 +99,26 @@ async fn main () -> Result <(), anyhow::Error> { config_file.name.unwrap_or_else (|| "PTTH File Server".to_string ()) ); - let gauges = metrics::Gauges::new ().await?; + let metrics_gauge = Arc::new (ArcSwap::from_pointee (None)); + + let gauge_writer = Arc::clone (&metrics_gauge); + + tokio::spawn (async move { + let mut interval = tokio::time::interval (std::time::Duration::from_secs (2)); + loop { + interval.tick ().await; + + let new_gauges = match file_server::metrics::Gauges::new ().await { + Err (e) => { + error! ("Failed to update gauge metrics: {:?}", e); + continue; + }, + Ok (x) => x, + }; + let new_gauges = Arc::new (Some (new_gauges)); + gauge_writer.store (new_gauges); + } + }); let state = Arc::new (State { config: file_server::Config { @@ -106,6 +126,7 @@ async fn main () -> Result <(), anyhow::Error> { }, handlebars, metrics_startup, + metrics_gauge, hidden_path: Some (path), }); diff --git a/crates/ptth_server/Cargo.toml b/crates/ptth_server/Cargo.toml index 967bb15..d03c9c4 100644 --- a/crates/ptth_server/Cargo.toml +++ b/crates/ptth_server/Cargo.toml @@ -10,6 +10,7 @@ license = "AGPL-3.0" aho-corasick = "0.7.14" anyhow = "1.0.34" +arc-swap = "1.1.0" base64 = "0.12.3" blake3 = "0.3.7" chrono = {version = "0.4.19", features = ["serde"]} diff --git a/crates/ptth_server/src/file_server/metrics.rs b/crates/ptth_server/src/file_server/metrics.rs index 657c39e..659f4e3 100644 --- a/crates/ptth_server/src/file_server/metrics.rs +++ b/crates/ptth_server/src/file_server/metrics.rs @@ -35,6 +35,7 @@ pub struct Startup { #[derive (Debug, serde::Serialize)] pub struct Gauges { + pub utc: DateTime , pub rss_mib: u64, } @@ -48,6 +49,7 @@ impl Gauges { let rss_mib = mem.rss ().get:: (); let x = Gauges { + utc: Utc::now (), rss_mib, }; diff --git a/crates/ptth_server/src/file_server/mod.rs b/crates/ptth_server/src/file_server/mod.rs index 5835cc8..8d0934a 100644 --- a/crates/ptth_server/src/file_server/mod.rs +++ b/crates/ptth_server/src/file_server/mod.rs @@ -12,8 +12,10 @@ use std::{ Path, PathBuf, }, + sync::Arc, }; +use arc_swap::ArcSwap; use handlebars::Handlebars; use serde::Serialize; use tokio::{ @@ -57,6 +59,7 @@ pub struct State { pub config: Config, pub handlebars: handlebars::Handlebars <'static>, pub metrics_startup: metrics::Startup, + pub metrics_gauge: Arc >>, pub hidden_path: Option , } diff --git a/crates/ptth_server/src/lib.rs b/crates/ptth_server/src/lib.rs index 0858133..3901dd5 100644 --- a/crates/ptth_server/src/lib.rs +++ b/crates/ptth_server/src/lib.rs @@ -53,7 +53,6 @@ pub fn password_is_bad (mut password: String) -> bool { struct State { file_server: file_server::State, config: Config, - gauges: RwLock , client: Client, } @@ -179,8 +178,11 @@ pub async fn run_server ( ) -> Result <(), ServerError> { - use std::convert::TryInto; + use std::{ + convert::TryInto, + }; + use arc_swap::ArcSwap; use http::status::StatusCode; let asset_root = asset_root.unwrap_or_else (PathBuf::new); @@ -202,6 +204,7 @@ pub async fn run_server ( let handlebars = file_server::load_templates (&asset_root)?; let metrics_startup = file_server::metrics::Startup::new (config_file.name); + let metrics_gauge = Arc::new (ArcSwap::from_pointee (None)); let state = Arc::new (State { file_server: file_server::State { @@ -210,12 +213,12 @@ pub async fn run_server ( }, handlebars, metrics_startup, + metrics_gauge, hidden_path, }, config: Config { relay_url: config_file.relay_url, }, - gauges: RwLock::new (file_server::metrics::Gauges::new ().await?), client, });