Skip to content

Commit

Permalink
Remove unnecessary Arc
Browse files Browse the repository at this point in the history
  • Loading branch information
link2xt committed Apr 17, 2024
1 parent 58352d5 commit 6ef14e3
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 11 deletions.
8 changes: 4 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use std::path::PathBuf;
use std::sync::Arc;

use anyhow::{Context, Result};
use structopt::StructOpt;
Expand Down Expand Up @@ -41,14 +40,14 @@ async fn main() -> Result<()> {
let opt = Opt::from_args();
let certificate = std::fs::File::open(&opt.certificate_file).context("invalid certificate")?;

let metrics_state = Arc::new(metrics::Metrics::new());
let metrics_state = metrics::Metrics::new();

let state = state::State::new(
&opt.db,
certificate,
&opt.password,
opt.topic.clone(),
metrics_state.clone(),
metrics_state,
opt.interval,
)?;

Expand All @@ -57,7 +56,8 @@ async fn main() -> Result<()> {
let interval = opt.interval;

if let Some(metrics_address) = opt.metrics.clone() {
async_std::task::spawn(async move { metrics::start(metrics_state, metrics_address).await });
let state = state.clone();
async_std::task::spawn(async move { metrics::start(state, metrics_address).await });
}

// Setup mulitple parallel notifiers.
Expand Down
7 changes: 3 additions & 4 deletions src/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
//! independently of the main service.
use std::sync::atomic::AtomicI64;
use std::sync::Arc;

use prometheus_client::encoding::text::encode;
use prometheus_client::metrics::counter::Counter;
Expand All @@ -14,6 +13,8 @@ use prometheus_client::registry::Registry;

use anyhow::Result;

use crate::state::State;

#[derive(Debug, Default)]
pub struct Metrics {
pub registry: Registry,
Expand Down Expand Up @@ -73,8 +74,6 @@ impl Metrics {
}
}

type State = Arc<Metrics>;

pub async fn start(state: State, server: String) -> Result<()> {
let mut app = tide::with_state(state);
app.at("/metrics").get(metrics);
Expand All @@ -84,7 +83,7 @@ pub async fn start(state: State, server: String) -> Result<()> {

async fn metrics(req: tide::Request<State>) -> tide::Result<tide::Response> {
let mut encoded = String::new();
encode(&mut encoded, &req.state().registry).unwrap();
encode(&mut encoded, &req.state().metrics().registry).unwrap();
let response = tide::Response::builder(tide::StatusCode::Ok)
.body(encoded)
.content_type("application/openmetrics-text; version=1.0.0; charset=utf-8")
Expand Down
6 changes: 3 additions & 3 deletions src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub struct InnerState {

topic: Option<String>,

metrics: Arc<Metrics>,
metrics: Metrics,

/// Heartbeat notification interval.
interval: Duration,
Expand All @@ -36,7 +36,7 @@ impl State {
mut certificate: std::fs::File,
password: &str,
topic: Option<String>,
metrics: Arc<Metrics>,
metrics: Metrics,
interval: Duration,
) -> Result<Self> {
let schedule = Schedule::new(db)?;
Expand Down Expand Up @@ -76,7 +76,7 @@ impl State {
}

pub fn metrics(&self) -> &Metrics {
self.inner.metrics.as_ref()
&self.inner.metrics
}

pub fn interval(&self) -> Duration {
Expand Down

0 comments on commit 6ef14e3

Please sign in to comment.