Skip to content

Commit fdaf7b0

Browse files
committed
refactor: adjust location and upstream of ctx
1 parent ebe820f commit fdaf7b0

File tree

13 files changed

+50
-92
lines changed

13 files changed

+50
-92
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,5 @@ dist
2222
dhat*.json
2323
default_*
2424
report.*
25+
.shuttle*
26+
Secrets*.toml

Cargo.lock

-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,6 @@ reqwest = { version = "0.12.12", default-features = false, features = [
182182
"json",
183183
"default-tls",
184184
] }
185-
hostname = "0.4.0"
186185
futures = "0.3.31"
187186
humantime = "2.1.0"
188187
url = "2.5.4"

benches/bench.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ fn bench_logger_format(c: &mut Criterion) {
293293
upstream_connected: Some(10),
294294
upstream_processing_time: Some(50),
295295
upstream_response_time: Some(5),
296-
location: None,
296+
location: "".to_string(),
297297
connection_time: 300,
298298
tls_version: Some("tls1.2".to_string()),
299299
processing: 10,
@@ -314,7 +314,12 @@ fn bench_logger_format(c: &mut Criterion) {
314314

315315
fn bench_get_location(c: &mut Criterion) {
316316
let mut map: AHashMap<String, Arc<Location>> = AHashMap::new();
317-
for _ in 0..10 {
317+
let id = nanoid!(6);
318+
map.insert(
319+
id.clone(),
320+
Arc::new(Location::new(id.as_str(), &LocationConf::default()).unwrap()),
321+
);
322+
for _ in 0..20 {
318323
let id = nanoid!(6);
319324
map.insert(
320325
id.clone(),
@@ -326,9 +331,10 @@ fn bench_get_location(c: &mut Criterion) {
326331

327332
let locations: ArcSwap<AHashMap<String, Arc<Location>>> =
328333
ArcSwap::from_pointee(map);
334+
locations.load().get(&id).unwrap();
329335
c.bench_function("get location", |b| {
330336
b.iter(|| {
331-
let _ = locations.load().get("1").cloned();
337+
let _ = locations.load().get(&id).cloned();
332338
})
333339
});
334340
}

docs/modules.md

+1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ graph TD
5050
performance --> upstream
5151
performance --> util
5252
53+
plugin --> cache
5354
plugin --> config
5455
plugin --> http-extra
5556
plugin --> state

pingap-logger/src/access.rs

+1-13
Original file line numberDiff line numberDiff line change
@@ -427,12 +427,8 @@ impl Parser {
427427

428428
#[cfg(test)]
429429
mod tests {
430-
use std::sync::Arc;
431-
432430
use super::{format_extra_tag, Parser, Tag, TagCategory};
433431
use http::Method;
434-
use pingap_config::LocationConf;
435-
use pingap_location::Location;
436432
use pingap_state::Ctx;
437433
use pingora::proxy::Session;
438434
use pretty_assertions::assert_eq;
@@ -658,15 +654,7 @@ mod tests {
658654
upstream_address: "192.186.1.1:6188".to_string(),
659655
processing: 1,
660656
upstream_connect_time: Some(100),
661-
location: Some(Arc::new(
662-
Location::new(
663-
"test",
664-
&LocationConf {
665-
..Default::default()
666-
},
667-
)
668-
.unwrap(),
669-
)),
657+
location: "test".to_string(),
670658
connection_time: 300,
671659
tls_version: Some("1.2".to_string()),
672660
request_id: Some("nanoid".to_string()),

pingap-performance/src/prom.rs

+5-20
Original file line numberDiff line numberDiff line change
@@ -179,12 +179,8 @@ impl Prometheus {
179179
/// This method performs multiple metric updates but uses efficient
180180
/// atomic operations to minimize overhead.
181181
pub fn after(&self, session: &Session, ctx: &Ctx) {
182-
let mut location = "";
183-
let mut upstream = "";
184-
if let Some(lo) = &ctx.location {
185-
location = &lo.name;
186-
upstream = &lo.upstream;
187-
}
182+
let location = &ctx.location;
183+
let upstream = &ctx.upstream;
188184
let response_time =
189185
((pingap_util::now_ms()) - ctx.created_at) as f64 / SECOND;
190186
// payload size(kb)
@@ -255,7 +251,7 @@ impl Prometheus {
255251

256252
// upstream
257253
if !upstream.is_empty() {
258-
let upstream_labels = &[upstream];
254+
let upstream_labels = &[upstream.as_str()];
259255
if let Some(count) = ctx.upstream_connected {
260256
self.upstream_connections
261257
.with_label_values(upstream_labels)
@@ -801,12 +797,9 @@ pub fn new_prometheus(server: &str) -> Result<Prometheus> {
801797
mod tests {
802798
use super::new_prometheus;
803799
use http::StatusCode;
804-
use pingap_config::LocationConf;
805-
use pingap_location::Location;
806800
use pingap_state::{CompressionStat, Ctx};
807801
use pingora::proxy::Session;
808802
use pretty_assertions::assert_eq;
809-
use std::sync::Arc;
810803
use std::time::Duration;
811804
use tokio_test::io::Builder;
812805

@@ -831,15 +824,6 @@ mod tests {
831824
let p = new_prometheus("pingap").unwrap();
832825
p.before("");
833826

834-
let lo = Location::new(
835-
"lo",
836-
&LocationConf {
837-
upstream: Some("upstream".to_string()),
838-
..Default::default()
839-
},
840-
)
841-
.unwrap();
842-
843827
p.after(
844828
&session,
845829
&Ctx {
@@ -860,7 +844,8 @@ mod tests {
860844
out_bytes: 512,
861845
duration: Duration::from_millis(20),
862846
}),
863-
location: Some(Arc::new(lo)),
847+
location: "lo".to_string(),
848+
upstream: "upstream".to_string(),
864849
..Default::default()
865850
},
866851
);

pingap-state/Cargo.toml

-3
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,7 @@ opentelemetry = { version = "0.27.1", default-features = false, features = [
2323
"trace",
2424
], optional = true }
2525
tokio = { workspace = true }
26-
pingap-location = { path = "../pingap-location" }
27-
pingap-upstream = { path = "../pingap-upstream" }
2826
pingap-util = { path = "../pingap-util" }
29-
pingap-config = { path = "../pingap-config" }
3027

3128

3229
[features]

pingap-state/src/ctx.rs

+6-19
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ use ahash::AHashMap;
1616
use bytes::{Bytes, BytesMut};
1717
use http::StatusCode;
1818
use http::Uri;
19-
use pingap_location::Location;
20-
use pingap_upstream::Upstream;
2119
use pingap_util::format_duration;
2220
use pingora::cache::CacheKey;
2321

@@ -28,7 +26,7 @@ use opentelemetry::{
2826
Context,
2927
};
3028
use pingora_limits::inflight::Guard;
31-
use std::{sync::Arc, time::Duration};
29+
use std::time::Duration;
3230

3331
pub trait ModifyResponseBody: Sync + Send {
3432
fn handle(&self, data: Bytes) -> Bytes;
@@ -102,7 +100,7 @@ pub struct Ctx {
102100
/// Indicates if this connection is being reused
103101
pub connection_reused: bool,
104102
/// The location configuration handling this request
105-
pub location: Option<Arc<Location>>,
103+
pub location: String,
106104
/// Address of the upstream server
107105
pub upstream_address: String,
108106
/// Client's IP address
@@ -131,7 +129,7 @@ pub struct Ctx {
131129
pub cache_lock_time: Option<u64>,
132130
/// Maximum time-to-live for cache entries
133131
pub cache_max_ttl: Option<Duration>,
134-
pub upstream: Option<Arc<Upstream>>,
132+
pub upstream: String,
135133
/// Indicates if the upstream connection is being reused
136134
pub upstream_reused: bool,
137135
/// Number of requests being processed by upstream
@@ -309,8 +307,8 @@ impl Ctx {
309307
}
310308
},
311309
"location" => {
312-
if let Some(location) = &self.location {
313-
buf.extend(location.name.as_bytes())
310+
if !self.location.is_empty() {
311+
buf.extend(self.location.as_bytes())
314312
}
315313
},
316314
"connection_time" => {
@@ -395,10 +393,7 @@ pub fn get_cache_key(ctx: &Ctx, method: &str, uri: &Uri) -> CacheKey {
395393
mod tests {
396394
use super::*;
397395
use bytes::BytesMut;
398-
use pingap_config::LocationConf;
399-
use pingap_location::Location;
400396
use pretty_assertions::assert_eq;
401-
use std::sync::Arc;
402397
use std::time::Duration;
403398

404399
#[test]
@@ -477,15 +472,7 @@ mod tests {
477472
.as_ref()
478473
);
479474

480-
ctx.location = Some(Arc::new(
481-
Location::new(
482-
"pingap",
483-
&LocationConf {
484-
..Default::default()
485-
},
486-
)
487-
.unwrap(),
488-
));
475+
ctx.location = "pingap".to_string();
489476
assert_eq!(
490477
b"pingap",
491478
ctx.append_value(BytesMut::new(), "location").as_ref()

pingap-upstream/src/upstream.rs

+3
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,9 @@ static UPSTREAM_MAP: Lazy<ArcSwap<Upstreams>> =
564564
Lazy::new(|| ArcSwap::from_pointee(AHashMap::new()));
565565

566566
pub fn get_upstream(name: &str) -> Option<Arc<Upstream>> {
567+
if name.is_empty() {
568+
return None;
569+
}
567570
UPSTREAM_MAP.load().get(name).cloned()
568571
}
569572

pingap-webhook/Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,3 @@ tracing = { workspace = true }
1818
strum = { workspace = true }
1919
reqwest = { workspace = true }
2020
pingap-util = { path = "../pingap-util" }
21-
hostname = { workspace = true }

pingap-webhook/src/lib.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -159,11 +159,7 @@ pub async fn send_notification(params: SendNotificationParams) {
159159

160160
let client = reqwest::Client::new();
161161
let mut data = serde_json::Map::new();
162-
let hostname = hostname::get()
163-
.unwrap_or_default()
164-
.to_str()
165-
.unwrap_or_default()
166-
.to_string();
162+
let hostname = pingap_util::get_hostname();
167163
// TODO get app name from config
168164
let name = "pingap".to_string();
169165
let color_type = match level {

0 commit comments

Comments
 (0)