Skip to content

Commit 4560ef0

Browse files
committed
Format
1 parent a2792be commit 4560ef0

File tree

3 files changed

+28
-12
lines changed

3 files changed

+28
-12
lines changed

src/api/v1/caching.rs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use std::collections::HashSet;
2-
31
use actix_web::{
42
HttpResponse,
53
body::{BoxBody, EitherBody, MessageBody},
@@ -22,7 +20,9 @@ pub async fn middleware(
2220
service_request: ServiceRequest,
2321
next: Next<impl MessageBody>
2422
) -> Result<ServiceResponse<EitherBody<impl MessageBody>>, actix_web::Error> {
25-
let match_pattern = service_request.match_pattern().unwrap_or("default".to_string());
23+
let match_pattern = service_request
24+
.match_pattern()
25+
.unwrap_or("default".to_string());
2626
let Some(app_data) = service_request.app_data::<web::Data<ApiData>>() else {
2727
// If we don't have ApiData for whatever reason, we can't do much
2828
// cache-related Technically this could probably be an unwrap, but this is
@@ -42,7 +42,9 @@ pub async fn middleware(
4242
}
4343

4444
let cache = app_data.cache.clone();
45-
let metric_labels = CacheLabels { endpoint: match_pattern };
45+
let metric_labels = CacheLabels {
46+
endpoint: match_pattern
47+
};
4648
let cache_key = CacheKey {
4749
path: service_request.path().to_string(),
4850
query: service_request.query_string().to_string()
@@ -62,7 +64,11 @@ pub async fn middleware(
6264
// Resolve cache entry with path & query
6365
if let Some(cache_value) = cache.get(&cache_key).await {
6466
// Record a cache hit to the metrics
65-
app_data.metrics.cache_hits.get_or_create(&metric_labels).inc();
67+
app_data
68+
.metrics
69+
.cache_hits
70+
.get_or_create(&metric_labels)
71+
.inc();
6672

6773
// Short circuit with HttpResponse::NotModified() if the If-None-Match header
6874
// matches cache
@@ -96,7 +102,11 @@ pub async fn middleware(
96102
return Ok(service_request.into_response(res).map_into_right_body());
97103
} else {
98104
// Record a cache miss to the metrics
99-
app_data.metrics.cache_misses.get_or_create(&metric_labels).inc();
105+
app_data
106+
.metrics
107+
.cache_misses
108+
.get_or_create(&metric_labels)
109+
.inc();
100110
}
101111

102112
// If none of the caching cases were handled, pass through to other handlers

src/api/v1/metrics.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ use prometheus_client::{
1515
};
1616

1717
use crate::api::v1::{
18-
caching::CacheLabels, endpoints::artifacts::{ArtifactQuery, OneConfigVersionInfo}, ApiData
18+
ApiData,
19+
caching::CacheLabels,
20+
endpoints::artifacts::{ArtifactQuery, OneConfigVersionInfo}
1921
};
2022

2123
/// A macro that automatically initializes and registers a metric using inferred
@@ -115,7 +117,11 @@ pub async fn middleware(
115117
) -> Result<ServiceResponse<impl MessageBody>, actix_web::Error> {
116118
let data = service_request.extract::<web::Data<ApiData>>().await?;
117119

118-
match service_request.match_pattern().unwrap_or("default".to_string()).as_str() {
120+
match service_request
121+
.match_pattern()
122+
.unwrap_or("default".to_string())
123+
.as_str()
124+
{
119125
"/v1/artifacts/oneconfig" => {
120126
data.metrics
121127
.oneconfig_artifacts_requests
@@ -133,9 +139,9 @@ pub async fn middleware(
133139
.get_or_create(&PlatformAgnosticArtifactLabels {
134140
// Unfortunately actix makes it difficult to extract the real
135141
// parsed URL parameter, so just substring instead as a substitute
136-
r#type: service_request.uri().path()
137-
[const { "/v1/artifacts/".len() }..]
138-
.to_string()
142+
r#type:
143+
service_request.uri().path()[const { "/v1/artifacts/".len() }..]
144+
.to_string()
139145
})
140146
.inc();
141147
}

src/api/v1/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ pub struct ApiData {
4040
/// The internal cache used to cache artifact responses.
4141
pub cache: Cache<CacheKey, CacheValue>,
4242
/// All the metrics objects used for encoding and recording metrics
43-
pub metrics: ApiMetrics,
43+
pub metrics: ApiMetrics
4444
}
4545

4646
pub fn configure() -> impl FnOnce(&mut ServiceConfig) {

0 commit comments

Comments
 (0)