Skip to content

Commit

Permalink
fix(deps): remove dependency to lazy_static
Browse files Browse the repository at this point in the history
once_cell supersets lazy_static features and its API is better and
"soon" to be stabilized in the standard library:
rust-lang/rust#109736
  • Loading branch information
remi-dupre committed Apr 13, 2023
1 parent 5dfcf61 commit e13aebb
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 31 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ serde = { version = "1.0.136", features = ["derive"] }
serde_json = "1.0.78"
serde_yaml = "0.8.23"
bytes = "1.1.0"
lazy_static = "1.4.0"
prometheus = "0.13.0"
regex = "1.5.4"
once_cell = "1.9.0"
Expand Down
10 changes: 3 additions & 7 deletions src/endpoint.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
use std::str::FromStr;

use lazy_static::lazy_static;

use schemars::JsonSchema;

use hyper::Method;
use once_cell::sync::Lazy;
use regex::Regex;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

lazy_static! {
static ref PATH_TO_PERM: Regex = Regex::new("\\{[^/]*\\}").unwrap();
}
static PATH_TO_PERM: Lazy<Regex> = Lazy::new(|| Regex::new("\\{[^/]*\\}").unwrap());

#[derive(Serialize, Deserialize, Debug, Clone, JsonSchema)]
pub struct Endpoint {
Expand Down
49 changes: 31 additions & 18 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::net::SocketAddr;
use std::process::exit;
use std::time::Instant;

use anyhow::anyhow;
use http_body::SizeHint;
use hyper::body::HttpBody;
use hyper::client::HttpConnector;
Expand All @@ -14,13 +15,10 @@ use hyper::header::{
use hyper::service::{make_service_fn, service_fn};
use hyper::{Body, Client, HeaderMap, Method, Request, Response, Server, StatusCode, Uri};
use hyper_tungstenite::is_upgrade_request;
use once_cell::sync::Lazy;
use std::sync::Arc;
use tokio::sync::RwLock;

use lazy_static::lazy_static;

use anyhow::anyhow;

use prometheus::{
exponential_buckets, opts, register_counter_vec, register_histogram_vec, CounterVec, Encoder,
HistogramVec, TextEncoder,
Expand Down Expand Up @@ -137,50 +135,65 @@ fn get_metric_name(name: &str) -> String {
)
}

lazy_static! {
static ref HTTP_COUNTER: CounterVec = register_counter_vec!(
static HTTP_COUNTER: Lazy<CounterVec> = Lazy::new(|| {
register_counter_vec!(
opts!(
get_metric_name("requests_total"),
"Number of HTTP requests made."
),
&LABEL_NAMES
)
.unwrap();
static ref HTTP_REQ_LAT_HISTOGRAM: HistogramVec = register_histogram_vec!(
.unwrap()
});

static HTTP_REQ_LAT_HISTOGRAM: Lazy<HistogramVec> = Lazy::new(|| {
register_histogram_vec!(
get_metric_name("request_duration_seconds"),
"The HTTP request latencies in seconds.",
&LABEL_NAMES
)
.unwrap();
static ref HTTP_REQ_SIZE_HISTOGRAM_LOW: HistogramVec = register_histogram_vec!(
.unwrap()
});

static HTTP_REQ_SIZE_HISTOGRAM_LOW: Lazy<HistogramVec> = Lazy::new(|| {
register_histogram_vec!(
get_metric_name("request_size_low_bytes"),
"The HTTP request size in bytes (lower bound).",
&LABEL_NAMES,
exponential_buckets(1.0, 2.0, 35).unwrap()
)
.unwrap();
static ref HTTP_REQ_SIZE_HISTOGRAM_HIGH: HistogramVec = register_histogram_vec!(
.unwrap()
});

static HTTP_REQ_SIZE_HISTOGRAM_HIGH: Lazy<HistogramVec> = Lazy::new(|| {
register_histogram_vec!(
get_metric_name("request_size_high_bytes"),
"The HTTP request size in bytes (upper bound).",
&LABEL_NAMES,
exponential_buckets(1.0, 2.0, 35).unwrap()
)
.unwrap();
static ref HTTP_RES_SIZE_HISTOGRAM_LOW: HistogramVec = register_histogram_vec!(
.unwrap()
});

static HTTP_RES_SIZE_HISTOGRAM_LOW: Lazy<HistogramVec> = Lazy::new(|| {
register_histogram_vec!(
get_metric_name("response_size_low_bytes"),
"The HTTP response size in bytes (lower bound).",
&LABEL_NAMES,
exponential_buckets(1.0, 2.0, 35).unwrap()
)
.unwrap();
static ref HTTP_RES_SIZE_HISTOGRAM_HIGH: HistogramVec = register_histogram_vec!(
.unwrap()
});

static HTTP_RES_SIZE_HISTOGRAM_HIGH: Lazy<HistogramVec> = Lazy::new(|| {
register_histogram_vec!(
get_metric_name("response_size_high_bytes"),
"The HTTP response size in bytes (upper bound).",
&LABEL_NAMES,
exponential_buckets(1.0, 2.0, 35).unwrap()
)
.unwrap();
}
.unwrap()
});

fn inject_cors(headers: &mut HeaderMap<HeaderValue>) {
headers.insert(ACCESS_CONTROL_ALLOW_ORIGIN, "*".parse().unwrap());
Expand Down
6 changes: 2 additions & 4 deletions src/route.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
use std::collections::HashMap;

use lazy_static::lazy_static;
use once_cell::sync::Lazy;
use regex::Regex;

use crate::api::{ApiDefinition, ApiMode};
use crate::endpoint::Endpoint;

lazy_static! {
static ref IS_PARAM: Regex = Regex::new("\\{[^/]*\\}").unwrap();
}
static IS_PARAM: Lazy<Regex> = Lazy::new(|| Regex::new("\\{[^/]*\\}").unwrap());

#[derive(Debug)]
pub struct Node {
Expand Down

0 comments on commit e13aebb

Please sign in to comment.