Remove lazy_static dependency.

This commit is contained in:
Orne Brocaar 2025-04-16 09:51:49 +01:00
parent 4ce4828a78
commit 75e9106bbb
27 changed files with 204 additions and 224 deletions

2
Cargo.lock generated
View File

@ -866,7 +866,6 @@ dependencies = [
"humantime-serde",
"jsonwebtoken",
"lapin",
"lazy_static",
"lrwn",
"mime_guess",
"oauth2",
@ -946,7 +945,6 @@ dependencies = [
"anyhow",
"async-trait",
"chirpstack_api",
"lazy_static",
"redis",
"serde",
"serde_json",

View File

@ -24,6 +24,5 @@
async-trait = "0.1"
serde = { version = "1.0", features = ["derive"] }
tokio = { version = "1.44", features = ["macros", "rt-multi-thread"] }
lazy_static = "1.5"
serde_json = "1.0"
toml = "0.8"

View File

@ -1,8 +1,6 @@
#[macro_use]
extern crate lazy_static;
use std::io::Cursor;
use std::str::FromStr;
use std::sync::LazyLock;
use anyhow::Result;
use async_trait::async_trait;
@ -13,10 +11,8 @@ use tracing_subscriber::{filter, prelude::*};
use chirpstack_api::{integration as integration_pb, prost::Message};
lazy_static! {
static ref INTEGRATION: RwLock<Option<Box<dyn IntegrationTrait + Sync + Send>>> =
RwLock::new(None);
}
static INTEGRATION: LazyLock<RwLock<Option<Box<dyn IntegrationTrait + Sync + Send>>>> =
LazyLock::new(|| RwLock::new(None));
#[derive(Default, Deserialize, Clone)]
#[serde(default)]

View File

@ -137,7 +137,6 @@
] }
# Misc
lazy_static = "1.5"
uuid = { version = "1.16", features = ["v4", "serde"] }
chrono = "0.4"
async-trait = "0.1"

View File

@ -1,4 +1,5 @@
use std::collections::HashMap;
use std::sync::LazyLock;
use anyhow::Result;
use async_trait::async_trait;
@ -14,10 +15,8 @@ pub mod lora_lr_fhss;
pub mod lr_fhss;
pub mod plugin;
lazy_static! {
static ref ADR_ALGORITHMS: RwLock<HashMap<String, Box<dyn Handler + Sync + Send>>> =
RwLock::new(HashMap::new());
}
static ADR_ALGORITHMS: LazyLock<RwLock<HashMap<String, Box<dyn Handler + Sync + Send>>>> =
LazyLock::new(|| RwLock::new(HashMap::new()));
pub async fn setup() -> Result<()> {
info!("Setting up adr algorithms");

View File

@ -1,3 +1,4 @@
use std::sync::LazyLock;
use std::time::{Duration, Instant};
use std::{
future::Future,
@ -67,33 +68,31 @@ pub mod relay;
pub mod tenant;
pub mod user;
lazy_static! {
static ref GRPC_COUNTER: Family<GrpcLabels, Counter> = {
let counter = Family::<GrpcLabels, Counter>::default();
prometheus::register(
"api_requests_handled",
"Number of API requests handled by service, method and status code",
counter.clone(),
);
counter
};
static ref GRPC_HISTOGRAM: Family<GrpcLabels, Histogram> = {
let histogram = Family::<GrpcLabels, Histogram>::new_with_constructor(|| {
Histogram::new(
[
0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1.0, 2.5, 5.0, 10.0,
]
.into_iter(),
)
});
prometheus::register(
"api_requests_handled_seconds",
"Duration of API requests handled by service, method and status code",
histogram.clone(),
);
histogram
};
}
static GRPC_COUNTER: LazyLock<Family<GrpcLabels, Counter>> = LazyLock::new(|| {
let counter = Family::<GrpcLabels, Counter>::default();
prometheus::register(
"api_requests_handled",
"Number of API requests handled by service, method and status code",
counter.clone(),
);
counter
});
static GRPC_HISTOGRAM: LazyLock<Family<GrpcLabels, Histogram>> = LazyLock::new(|| {
let histogram = Family::<GrpcLabels, Histogram>::new_with_constructor(|| {
Histogram::new(
[
0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1.0, 2.5, 5.0, 10.0,
]
.into_iter(),
)
});
prometheus::register(
"api_requests_handled_seconds",
"Duration of API requests handled by service, method and status code",
histogram.clone(),
);
histogram
});
#[derive(RustEmbed)]
#[folder = "../ui/build"]

View File

@ -1,4 +1,4 @@
use std::sync::Arc;
use std::sync::{Arc, LazyLock};
use anyhow::Result;
use tokio::sync::RwLock;
@ -8,9 +8,8 @@ use crate::{config, stream};
use backend::{Client, ClientConfig};
use lrwn::{EUI64Prefix, EUI64};
lazy_static! {
static ref CLIENTS: RwLock<Vec<(EUI64Prefix, Arc<Client>)>> = RwLock::new(vec![]);
}
static CLIENTS: LazyLock<RwLock<Vec<(EUI64Prefix, Arc<Client>)>>> =
LazyLock::new(|| RwLock::new(vec![]));
pub async fn setup() -> Result<()> {
info!("Setting up Join Server clients");

View File

@ -1,7 +1,7 @@
use std::collections::HashMap;
use std::io::Cursor;
use std::str::FromStr;
use std::sync::Arc;
use std::sync::{Arc, LazyLock};
use anyhow::Result;
use chrono::{Duration, DurationRound};
@ -15,9 +15,8 @@ use backend::{Client, ClientConfig, GWInfoElement, ULMetaData};
use chirpstack_api::{common, gw};
use lrwn::{region, DevAddr, NetID, EUI64};
lazy_static! {
static ref CLIENTS: RwLock<HashMap<NetID, Arc<Client>>> = RwLock::new(HashMap::new());
}
static CLIENTS: LazyLock<RwLock<HashMap<NetID, Arc<Client>>>> =
LazyLock::new(|| RwLock::new(HashMap::new()));
pub async fn setup() -> Result<()> {
info!("Setting up roaming clients");

View File

@ -1,5 +1,5 @@
use std::path::Path;
use std::sync::{Arc, Mutex};
use std::sync::{Arc, LazyLock, Mutex};
use std::time::Duration;
use std::{env, fs};
@ -9,9 +9,8 @@ use serde::{Deserialize, Serialize};
use lrwn::region::CommonName;
use lrwn::{AES128Key, DevAddrPrefix, EUI64Prefix, NetID};
lazy_static! {
static ref CONFIG: Mutex<Arc<Configuration>> = Mutex::new(Arc::new(Default::default()));
}
static CONFIG: LazyLock<Mutex<Arc<Configuration>>> =
LazyLock::new(|| Mutex::new(Arc::new(Default::default())));
#[derive(Default, Serialize, Deserialize, Clone)]
#[serde(default)]

View File

@ -1,3 +1,5 @@
use std::sync::LazyLock;
use aes::cipher::generic_array::GenericArray;
use aes::cipher::{BlockEncrypt, KeyInit};
use aes::{Aes128, Block};
@ -7,14 +9,11 @@ use tracing::debug;
use lrwn::DevAddr;
lazy_static! {
static ref BEACON_PERIOD: Duration = Duration::try_seconds(128).unwrap();
static ref BEACON_RESERVED: Duration = Duration::try_milliseconds(2120).unwrap();
static ref BEACON_GUARD: Duration = Duration::try_seconds(3).unwrap();
static ref BEACON_WINDOW: Duration = Duration::try_milliseconds(122880).unwrap();
static ref PING_PERIOD_BASE: usize = 1 << 12;
static ref SLOT_LEN: Duration = Duration::try_milliseconds(30).unwrap();
}
static BEACON_PERIOD: LazyLock<Duration> = LazyLock::new(|| Duration::try_seconds(128).unwrap());
static BEACON_RESERVED: LazyLock<Duration> =
LazyLock::new(|| Duration::try_milliseconds(2120).unwrap());
static PING_PERIOD_BASE: usize = 1 << 12;
static SLOT_LEN: LazyLock<Duration> = LazyLock::new(|| Duration::try_milliseconds(30).unwrap());
pub fn get_beacon_start(ts: Duration) -> Duration {
Duration::try_seconds(ts.num_seconds() - (ts.num_seconds() % BEACON_PERIOD.num_seconds()))
@ -26,7 +25,7 @@ pub fn get_ping_offset(beacon_ts: Duration, dev_addr: &DevAddr, ping_nb: usize)
return Err(anyhow!("ping_nb must be > 0"));
}
let ping_period = *PING_PERIOD_BASE / ping_nb;
let ping_period = PING_PERIOD_BASE / ping_nb;
let beacon_time = (beacon_ts.num_seconds() % (1 << 32)) as u32;
let key_bytes: [u8; 16] = [0x00; 16];
@ -54,7 +53,7 @@ pub fn get_next_ping_slot_after(
}
let mut beacon_start_ts = get_beacon_start(after_gps_epoch_ts);
let ping_period = *PING_PERIOD_BASE / ping_nb;
let ping_period = PING_PERIOD_BASE / ping_nb;
loop {
let ping_offset = get_ping_offset(beacon_start_ts, dev_addr, ping_nb)?;
@ -122,7 +121,7 @@ pub mod test {
for k in 0..8 {
let mut beacon_ts = Duration::zero();
let ping_nb: usize = 1 << k;
let ping_period = *PING_PERIOD_BASE / ping_nb;
let ping_period = PING_PERIOD_BASE / ping_nb;
let dev_addr = DevAddr::from_be_bytes([0, 0, 0, 0]);
for _ in 0..100000 {

View File

@ -1,3 +1,5 @@
use std::sync::LazyLock;
use anyhow::Result;
use async_trait::async_trait;
use tokio::sync::RwLock;
@ -6,11 +8,10 @@ use chirpstack_api::gw;
use super::GatewayBackend;
lazy_static! {
static ref DOWNLINK_FRAMES: RwLock<Vec<gw::DownlinkFrame>> = RwLock::new(Vec::new());
static ref GATEWAY_CONFIGURATIONS: RwLock<Vec<gw::GatewayConfiguration>> =
RwLock::new(Vec::new());
}
static DOWNLINK_FRAMES: LazyLock<RwLock<Vec<gw::DownlinkFrame>>> =
LazyLock::new(|| RwLock::new(Vec::new()));
static GATEWAY_CONFIGURATIONS: LazyLock<RwLock<Vec<gw::GatewayConfiguration>>> =
LazyLock::new(|| RwLock::new(Vec::new()));
pub async fn reset() {
DOWNLINK_FRAMES.write().await.drain(..);

View File

@ -1,4 +1,5 @@
use std::collections::HashMap;
use std::sync::LazyLock;
use anyhow::{Context, Result};
use async_trait::async_trait;
@ -11,10 +12,8 @@ use crate::config;
pub mod mock;
mod mqtt;
lazy_static! {
static ref BACKENDS: RwLock<HashMap<String, Box<dyn GatewayBackend + Sync + Send>>> =
RwLock::new(HashMap::new());
}
static BACKENDS: LazyLock<RwLock<HashMap<String, Box<dyn GatewayBackend + Sync + Send>>>> =
LazyLock::new(|| RwLock::new(HashMap::new()));
#[async_trait]
pub trait GatewayBackend {

View File

@ -1,6 +1,6 @@
use std::collections::HashMap;
use std::io::Cursor;
use std::sync::RwLock;
use std::sync::{LazyLock, RwLock};
use std::time::Duration;
use anyhow::Result;
@ -38,27 +38,26 @@ struct CommandLabels {
command: String,
}
lazy_static! {
static ref EVENT_COUNTER: Family<EventLabels, Counter> = {
let counter = Family::<EventLabels, Counter>::default();
prometheus::register(
"gateway_backend_mqtt_events",
"Number of events received",
counter.clone(),
);
counter
};
static ref COMMAND_COUNTER: Family<CommandLabels, Counter> = {
let counter = Family::<CommandLabels, Counter>::default();
prometheus::register(
"gateway_backend_mqtt_commands",
"Number of commands sent",
counter.clone(),
);
counter
};
static ref GATEWAY_JSON: RwLock<HashMap<String, bool>> = RwLock::new(HashMap::new());
}
static EVENT_COUNTER: LazyLock<Family<EventLabels, Counter>> = LazyLock::new(|| {
let counter = Family::<EventLabels, Counter>::default();
prometheus::register(
"gateway_backend_mqtt_events",
"Number of events received",
counter.clone(),
);
counter
});
static COMMAND_COUNTER: LazyLock<Family<CommandLabels, Counter>> = LazyLock::new(|| {
let counter = Family::<CommandLabels, Counter>::default();
prometheus::register(
"gateway_backend_mqtt_commands",
"Number of commands sent",
counter.clone(),
);
counter
});
static GATEWAY_JSON: LazyLock<RwLock<HashMap<String, bool>>> =
LazyLock::new(|| RwLock::new(HashMap::new()));
pub struct MqttBackend<'a> {
client: AsyncClient,

View File

@ -1,82 +1,85 @@
use std::sync::LazyLock;
use chrono::{DateTime, Duration, TimeZone, Utc};
lazy_static! {
static ref GPS_EPOCH_TIME: DateTime<Utc> = Utc.with_ymd_and_hms(1980, 1, 6, 0, 0, 0).unwrap();
static ref LEAP_SECONDS_TABLE: Vec<(DateTime<Utc>, Duration)> = vec![
static GPS_EPOCH_TIME: LazyLock<DateTime<Utc>> =
LazyLock::new(|| Utc.with_ymd_and_hms(1980, 1, 6, 0, 0, 0).unwrap());
static LEAP_SECONDS_TABLE: LazyLock<Vec<(DateTime<Utc>, Duration)>> = LazyLock::new(|| {
vec![
(
Utc.with_ymd_and_hms(1981, 6, 30, 23, 59, 59).unwrap(),
Duration::try_seconds(1).unwrap()
Duration::try_seconds(1).unwrap(),
),
(
Utc.with_ymd_and_hms(1982, 6, 30, 23, 59, 59).unwrap(),
Duration::try_seconds(1).unwrap()
Duration::try_seconds(1).unwrap(),
),
(
Utc.with_ymd_and_hms(1983, 6, 30, 23, 59, 59).unwrap(),
Duration::try_seconds(1).unwrap()
Duration::try_seconds(1).unwrap(),
),
(
Utc.with_ymd_and_hms(1985, 6, 30, 23, 59, 59).unwrap(),
Duration::try_seconds(1).unwrap()
Duration::try_seconds(1).unwrap(),
),
(
Utc.with_ymd_and_hms(1987, 12, 31, 23, 59, 59).unwrap(),
Duration::try_seconds(1).unwrap()
Duration::try_seconds(1).unwrap(),
),
(
Utc.with_ymd_and_hms(1989, 12, 31, 23, 59, 59).unwrap(),
Duration::try_seconds(1).unwrap()
Duration::try_seconds(1).unwrap(),
),
(
Utc.with_ymd_and_hms(1990, 12, 31, 23, 59, 59).unwrap(),
Duration::try_seconds(1).unwrap()
Duration::try_seconds(1).unwrap(),
),
(
Utc.with_ymd_and_hms(1992, 6, 30, 23, 59, 59).unwrap(),
Duration::try_seconds(1).unwrap()
Duration::try_seconds(1).unwrap(),
),
(
Utc.with_ymd_and_hms(1993, 6, 30, 23, 59, 59).unwrap(),
Duration::try_seconds(1).unwrap()
Duration::try_seconds(1).unwrap(),
),
(
Utc.with_ymd_and_hms(1994, 6, 30, 23, 59, 59).unwrap(),
Duration::try_seconds(1).unwrap()
Duration::try_seconds(1).unwrap(),
),
(
Utc.with_ymd_and_hms(1995, 12, 31, 23, 59, 59).unwrap(),
Duration::try_seconds(1).unwrap()
Duration::try_seconds(1).unwrap(),
),
(
Utc.with_ymd_and_hms(1997, 6, 30, 23, 59, 59).unwrap(),
Duration::try_seconds(1).unwrap()
Duration::try_seconds(1).unwrap(),
),
(
Utc.with_ymd_and_hms(1998, 12, 31, 23, 59, 59).unwrap(),
Duration::try_seconds(1).unwrap()
Duration::try_seconds(1).unwrap(),
),
(
Utc.with_ymd_and_hms(2005, 12, 31, 23, 59, 59).unwrap(),
Duration::try_seconds(1).unwrap()
Duration::try_seconds(1).unwrap(),
),
(
Utc.with_ymd_and_hms(2008, 12, 31, 23, 59, 59).unwrap(),
Duration::try_seconds(1).unwrap()
Duration::try_seconds(1).unwrap(),
),
(
Utc.with_ymd_and_hms(2012, 6, 30, 23, 59, 59).unwrap(),
Duration::try_seconds(1).unwrap()
Duration::try_seconds(1).unwrap(),
),
(
Utc.with_ymd_and_hms(2015, 6, 30, 23, 59, 59).unwrap(),
Duration::try_seconds(1).unwrap()
Duration::try_seconds(1).unwrap(),
),
(
Utc.with_ymd_and_hms(2016, 12, 31, 23, 59, 59).unwrap(),
Duration::try_seconds(1).unwrap()
Duration::try_seconds(1).unwrap(),
),
];
}
]
});
pub trait ToGpsTime {
fn to_gps_time(&self) -> Duration;

View File

@ -1,4 +1,5 @@
use std::collections::HashMap;
use std::sync::LazyLock;
use anyhow::Result;
use async_trait::async_trait;
@ -19,10 +20,8 @@ use chirpstack_api::integration;
// implement re-connect on error. To reconnect within the Integration struct would require
// mutability of the Integration struct, which is not possible without changing the
// IntegrationTrait as we would need to change the (&self, ...) signatures to (&mut self, ...).
lazy_static! {
static ref CONNECTION: RwLock<Option<Connection>> = RwLock::new(None);
static ref CHANNEL: RwLock<Option<Channel>> = RwLock::new(None);
}
static CONNECTION: LazyLock<RwLock<Option<Connection>>> = LazyLock::new(|| RwLock::new(None));
static CHANNEL: LazyLock<RwLock<Option<Channel>>> = LazyLock::new(|| RwLock::new(None));
pub struct Integration<'a> {
templates: Handlebars<'a>,

View File

@ -1,4 +1,5 @@
use std::collections::HashMap;
use std::sync::LazyLock;
use anyhow::Result;
use async_trait::async_trait;
@ -8,17 +9,22 @@ use chirpstack_api::integration;
use super::Integration as IntegrationTrait;
lazy_static! {
static ref UPLINK_EVENTS: RwLock<Vec<integration::UplinkEvent>> = RwLock::new(Vec::new());
static ref JOIN_EVENTS: RwLock<Vec<integration::JoinEvent>> = RwLock::new(Vec::new());
static ref ACK_EVENTS: RwLock<Vec<integration::AckEvent>> = RwLock::new(Vec::new());
static ref TXACK_EVENTS: RwLock<Vec<integration::TxAckEvent>> = RwLock::new(Vec::new());
static ref LOG_EVENTS: RwLock<Vec<integration::LogEvent>> = RwLock::new(Vec::new());
static ref STATUS_EVENTS: RwLock<Vec<integration::StatusEvent>> = RwLock::new(Vec::new());
static ref LOCATION_EVENTS: RwLock<Vec<integration::LocationEvent>> = RwLock::new(Vec::new());
static ref INTEGRATION_EVENTS: RwLock<Vec<integration::IntegrationEvent>> =
RwLock::new(Vec::new());
}
static UPLINK_EVENTS: LazyLock<RwLock<Vec<integration::UplinkEvent>>> =
LazyLock::new(|| RwLock::new(Vec::new()));
static JOIN_EVENTS: LazyLock<RwLock<Vec<integration::JoinEvent>>> =
LazyLock::new(|| RwLock::new(Vec::new()));
static ACK_EVENTS: LazyLock<RwLock<Vec<integration::AckEvent>>> =
LazyLock::new(|| RwLock::new(Vec::new()));
static TXACK_EVENTS: LazyLock<RwLock<Vec<integration::TxAckEvent>>> =
LazyLock::new(|| RwLock::new(Vec::new()));
static LOG_EVENTS: LazyLock<RwLock<Vec<integration::LogEvent>>> =
LazyLock::new(|| RwLock::new(Vec::new()));
static STATUS_EVENTS: LazyLock<RwLock<Vec<integration::StatusEvent>>> =
LazyLock::new(|| RwLock::new(Vec::new()));
static LOCATION_EVENTS: LazyLock<RwLock<Vec<integration::LocationEvent>>> =
LazyLock::new(|| RwLock::new(Vec::new()));
static INTEGRATION_EVENTS: LazyLock<RwLock<Vec<integration::IntegrationEvent>>> =
LazyLock::new(|| RwLock::new(Vec::new()));
pub async fn reset() {
UPLINK_EVENTS.write().await.drain(..);

View File

@ -1,5 +1,6 @@
use std::collections::HashMap;
use std::str::FromStr;
use std::sync::LazyLock;
use anyhow::{Context, Result};
use async_trait::async_trait;
@ -33,11 +34,11 @@ mod postgresql;
mod redis;
mod thingsboard;
lazy_static! {
static ref GLOBAL_INTEGRATIONS: RwLock<Vec<Box<dyn Integration + Sync + Send>>> =
RwLock::new(Vec::new());
static ref MOCK_INTEGRATION: RwLock<bool> = RwLock::new(false);
}
static GLOBAL_INTEGRATIONS: LazyLock<RwLock<Vec<Box<dyn Integration + Sync + Send>>>> =
LazyLock::new(|| RwLock::new(Vec::new()));
#[cfg(test)]
static MOCK_INTEGRATION: LazyLock<RwLock<bool>> = LazyLock::new(|| RwLock::new(false));
pub async fn setup() -> Result<()> {
info!("Setting up global integrations");

View File

@ -1,7 +1,5 @@
#![recursion_limit = "256"]
#[macro_use]
extern crate lazy_static;
extern crate diesel_migrations;
#[macro_use]
extern crate diesel;

View File

@ -1,12 +1,10 @@
use std::sync::RwLock;
use std::sync::{LazyLock, RwLock};
use anyhow::Result;
use prometheus_client::encoding::text::encode;
use prometheus_client::registry::{Metric, Registry};
lazy_static! {
static ref REGISTRY: RwLock<Registry> = RwLock::new(<Registry>::default());
}
static REGISTRY: LazyLock<RwLock<Registry>> = LazyLock::new(|| RwLock::new(<Registry>::default()));
pub fn encode_to_string() -> Result<String> {
let registry_r = REGISTRY.read().unwrap();

View File

@ -1,5 +1,5 @@
use std::collections::HashMap;
use std::sync::{Arc, RwLock};
use std::sync::{Arc, LazyLock, RwLock};
use anyhow::{Context, Result};
use tracing::{info, span, trace, Level};
@ -7,10 +7,8 @@ use tracing::{info, span, trace, Level};
use crate::config;
use lrwn::region;
lazy_static! {
static ref REGIONS: RwLock<HashMap<String, Arc<Box<dyn region::Region + Sync + Send>>>> =
RwLock::new(HashMap::new());
}
static REGIONS: LazyLock<RwLock<HashMap<String, Arc<Box<dyn region::Region + Sync + Send>>>>> =
LazyLock::new(|| RwLock::new(HashMap::new()));
pub fn setup() -> Result<()> {
info!("Setting up regions");

View File

@ -1,3 +1,4 @@
use std::sync::LazyLock;
use std::sync::RwLock;
use std::time::Instant;
@ -47,19 +48,18 @@ pub mod user;
use crate::monitoring::prometheus;
lazy_static! {
static ref ASYNC_REDIS_POOL: TokioRwLock<Option<AsyncRedisPool>> = TokioRwLock::new(None);
static ref REDIS_PREFIX: RwLock<String> = RwLock::new("".to_string());
static ref STORAGE_REDIS_CONN_GET: Histogram = {
let histogram = Histogram::new(exponential_buckets(0.001, 2.0, 12));
prometheus::register(
"storage_redis_conn_get_duration_seconds",
"Time between requesting a Redis connection and the connection-pool returning it",
histogram.clone(),
);
histogram
};
}
static ASYNC_REDIS_POOL: LazyLock<TokioRwLock<Option<AsyncRedisPool>>> =
LazyLock::new(|| TokioRwLock::new(None));
static REDIS_PREFIX: LazyLock<RwLock<String>> = LazyLock::new(|| RwLock::new("".to_string()));
static STORAGE_REDIS_CONN_GET: LazyLock<Histogram> = LazyLock::new(|| {
let histogram = Histogram::new(exponential_buckets(0.001, 2.0, 12));
prometheus::register(
"storage_redis_conn_get_duration_seconds",
"Time between requesting a Redis connection and the connection-pool returning it",
histogram.clone(),
);
histogram
});
#[cfg(feature = "postgres")]
pub const MIGRATIONS: EmbeddedMigrations = embed_migrations!("./migrations_postgres");

View File

@ -1,4 +1,4 @@
use std::sync::RwLock;
use std::sync::{LazyLock, RwLock};
use std::time::Instant;
use anyhow::Result;
@ -20,18 +20,16 @@ use crate::helpers::tls::get_root_certs;
pub type AsyncPgPool = DeadpoolPool<AsyncPgConnection>;
pub type AsyncPgPoolConnection = DeadpoolObject<AsyncPgConnection>;
lazy_static! {
static ref ASYNC_PG_POOL: RwLock<Option<AsyncPgPool>> = RwLock::new(None);
static ref STORAGE_PG_CONN_GET: Histogram = {
let histogram = Histogram::new(exponential_buckets(0.001, 2.0, 12));
prometheus::register(
"storage_pg_conn_get_duration_seconds",
"Time between requesting a PostgreSQL connection and the connection-pool returning it",
histogram.clone(),
);
histogram
};
}
static ASYNC_PG_POOL: LazyLock<RwLock<Option<AsyncPgPool>>> = LazyLock::new(|| RwLock::new(None));
static STORAGE_PG_CONN_GET: LazyLock<Histogram> = LazyLock::new(|| {
let histogram = Histogram::new(exponential_buckets(0.001, 2.0, 12));
prometheus::register(
"storage_pg_conn_get_duration_seconds",
"Time between requesting a PostgreSQL connection and the connection-pool returning it",
histogram.clone(),
);
histogram
});
pub fn setup(conf: &config::Postgresql) -> Result<()> {
info!("Setting up PostgreSQL connection pool");

View File

@ -1,4 +1,5 @@
use std::collections::HashMap;
use std::sync::LazyLock;
use anyhow::{Context, Result};
use diesel_async::RunQueryDsl;
@ -8,9 +9,7 @@ use uuid::Uuid;
use super::{error::Error, fields, get_async_db_conn};
use lrwn::EUI64;
lazy_static! {
static ref SEARCH_TAG_RE: Regex = Regex::new(r"([^ ]+):([^ ]+)").unwrap();
}
static SEARCH_TAG_RE: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"([^ ]+):([^ ]+)").unwrap());
#[derive(QueryableByName, PartialEq, Debug)]
pub struct SearchResult {

View File

@ -1,4 +1,4 @@
use std::sync::RwLock;
use std::sync::{LazyLock, RwLock};
use std::time::Instant;
use anyhow::Result;
@ -19,18 +19,17 @@ use crate::config;
pub type AsyncSqlitePool = DeadpoolPool<SyncConnectionWrapper<SqliteConnection>>;
pub type AsyncSqlitePoolConnection = DeadpoolObject<SyncConnectionWrapper<SqliteConnection>>;
lazy_static! {
static ref ASYNC_SQLITE_POOL: RwLock<Option<AsyncSqlitePool>> = RwLock::new(None);
static ref STORAGE_SQLITE_CONN_GET: Histogram = {
let histogram = Histogram::new(exponential_buckets(0.001, 2.0, 12));
prometheus::register(
"storage_sqlite_conn_get_duration_seconds",
"Time between requesting a SQLite connection and the connection-pool returning it",
histogram.clone(),
);
histogram
};
}
static ASYNC_SQLITE_POOL: LazyLock<RwLock<Option<AsyncSqlitePool>>> =
LazyLock::new(|| RwLock::new(None));
static STORAGE_SQLITE_CONN_GET: LazyLock<Histogram> = LazyLock::new(|| {
let histogram = Histogram::new(exponential_buckets(0.001, 2.0, 12));
prometheus::register(
"storage_sqlite_conn_get_duration_seconds",
"Time between requesting a SQLite connection and the connection-pool returning it",
histogram.clone(),
);
histogram
});
pub fn setup(conf: &config::Sqlite) -> Result<()> {
info!("Setting up SQLite connection pool");

View File

@ -1,6 +1,7 @@
use std::future::Future;
use std::io::Cursor;
use std::pin::Pin;
use std::sync::LazyLock;
use std::time::Duration;
use prost::Message;
@ -17,9 +18,7 @@ use crate::storage::{
use chirpstack_api::{gw, integration as integration_pb, internal, stream};
use lrwn::EUI64;
lazy_static! {
static ref LAST_DOWNLINK_ID: RwLock<u32> = RwLock::new(0);
}
static LAST_DOWNLINK_ID: LazyLock<RwLock<u32>> = LazyLock::new(|| RwLock::new(0));
pub type Validator = Box<dyn Fn() -> Pin<Box<dyn Future<Output = ()>>>>;

View File

@ -1,5 +1,5 @@
use std::env;
use std::sync::{Mutex, Once};
use std::sync::{LazyLock, Mutex, Once};
use crate::{adr, config, region, storage};
@ -17,9 +17,7 @@ mod relay_otaa_test;
static TRACING_INIT: Once = Once::new();
lazy_static! {
static ref TEST_MUX: Mutex<()> = Mutex::new(());
}
static TEST_MUX: LazyLock<Mutex<()>> = LazyLock::new(|| Mutex::new(()));
pub async fn prepare<'a>() -> std::sync::MutexGuard<'a, ()> {
dotenv::dotenv().ok();

View File

@ -2,6 +2,7 @@ use std::collections::HashMap;
use std::convert::{TryFrom, TryInto};
use std::io::Cursor;
use std::str::FromStr;
use std::sync::LazyLock;
use std::time::Duration;
use anyhow::{Context, Result};
@ -41,35 +42,33 @@ struct UplinkLabels {
m_type: String,
}
lazy_static! {
static ref UPLINK_COUNTER: Family<UplinkLabels, Counter> = {
let counter = Family::<UplinkLabels, Counter>::default();
prometheus::register(
"uplink_count",
"Number of received uplinks (after deduplication)",
counter.clone(),
);
counter
};
static ref DEDUPLICATE_LOCKED_COUNTER: Counter = {
let counter = Counter::default();
prometheus::register(
static UPLINK_COUNTER: LazyLock<Family<UplinkLabels, Counter>> = LazyLock::new(|| {
let counter = Family::<UplinkLabels, Counter>::default();
prometheus::register(
"uplink_count",
"Number of received uplinks (after deduplication)",
counter.clone(),
);
counter
});
static DEDUPLICATE_LOCKED_COUNTER: LazyLock<Counter> = LazyLock::new(|| {
let counter = Counter::default();
prometheus::register(
"deduplicate_locked_count",
"Number of times the deduplication function was called and the deduplication was already locked",
counter.clone(),
);
counter
};
static ref DEDUPLICATE_NO_LOCK_COUNTER: Counter = {
let counter = Counter::default();
prometheus::register(
"deduplicate_no_lock_count",
"Number of times the deduplication function was called and it was not yet locked",
counter.clone(),
);
counter
};
}
counter
});
static DEDUPLICATE_NO_LOCK_COUNTER: LazyLock<Counter> = LazyLock::new(|| {
let counter = Counter::default();
prometheus::register(
"deduplicate_no_lock_count",
"Number of times the deduplication function was called and it was not yet locked",
counter.clone(),
);
counter
});
#[derive(Clone)]
pub struct RelayContext {