Skip to content

Commit a788778

Browse files
committed
refactor: remove state's dependency on util
1 parent fdaf7b0 commit a788778

File tree

6 files changed

+76
-25
lines changed

6 files changed

+76
-25
lines changed

Cargo.lock

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

docs/modules.md

-5
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,6 @@ graph TD
5656
plugin --> state
5757
plugin --> util
5858
59-
state --> config
60-
state --> location
61-
state --> upstream
62-
state --> util
63-
6459
upstream --> config
6560
upstream --> discovery
6661
upstream --> health

pingap-state/Cargo.toml

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,15 @@ path = "src/lib.rs"
1616
ahash = { workspace = true }
1717
bytes = { workspace = true }
1818
http = { workspace = true }
19+
once_cell = { workspace = true }
20+
hostname = "0.4.0"
1921
pingora = { workspace = true }
2022
pingora-limits = { workspace = true }
2123
itoa = { workspace = true }
2224
opentelemetry = { version = "0.27.1", default-features = false, features = [
2325
"trace",
2426
], optional = true }
2527
tokio = { workspace = true }
26-
pingap-util = { path = "../pingap-util" }
2728

2829

2930
[features]

pingap-state/src/ctx.rs

+32-10
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,42 @@ use ahash::AHashMap;
1616
use bytes::{Bytes, BytesMut};
1717
use http::StatusCode;
1818
use http::Uri;
19-
use pingap_util::format_duration;
20-
use pingora::cache::CacheKey;
21-
2219
#[cfg(feature = "full")]
2320
use opentelemetry::{
2421
global::{BoxedSpan, BoxedTracer, ObjectSafeSpan},
2522
trace::{SpanKind, TraceContextExt, Tracer},
2623
Context,
2724
};
25+
use pingora::cache::CacheKey;
2826
use pingora_limits::inflight::Guard;
29-
use std::time::Duration;
27+
use std::time::{Duration, SystemTime, UNIX_EPOCH};
28+
29+
const SEC: u64 = 1_000;
30+
31+
#[inline]
32+
fn format_duration(mut buf: BytesMut, ms: u64) -> BytesMut {
33+
if ms < 1000 {
34+
buf.extend(itoa::Buffer::new().format(ms).as_bytes());
35+
buf.extend(b"ms");
36+
} else {
37+
buf.extend(itoa::Buffer::new().format(ms / SEC).as_bytes());
38+
let value = (ms % SEC) / 100;
39+
if value != 0 {
40+
buf.extend(b".");
41+
buf.extend(itoa::Buffer::new().format(value).as_bytes());
42+
}
43+
buf.extend(b"s");
44+
}
45+
buf
46+
}
47+
48+
#[inline]
49+
fn now_ms() -> u64 {
50+
SystemTime::now()
51+
.duration_since(UNIX_EPOCH)
52+
.unwrap_or_default()
53+
.as_millis() as u64
54+
}
3055

3156
pub trait ModifyResponseBody: Sync + Send {
3257
fn handle(&self, data: Bytes) -> Bytes;
@@ -180,7 +205,7 @@ impl Ctx {
180205
/// set to their default values.
181206
pub fn new() -> Self {
182207
Self {
183-
created_at: pingap_util::now_ms(),
208+
created_at: now_ms(),
184209
..Default::default()
185210
}
186211
}
@@ -358,10 +383,7 @@ impl Ctx {
358383
}
359384
},
360385
"service_time" => {
361-
buf = format_duration(
362-
buf,
363-
pingap_util::now_ms() - self.created_at,
364-
)
386+
buf = format_duration(buf, now_ms() - self.created_at)
365387
},
366388
_ => {},
367389
}
@@ -541,7 +563,7 @@ mod tests {
541563
.as_ref()
542564
);
543565

544-
ctx.created_at = pingap_util::now_ms() - 1;
566+
ctx.created_at = now_ms() - 1;
545567
assert_eq!(
546568
true,
547569
ctx.append_value(BytesMut::new(), "service_time")

pingap-state/src/http_header.rs

+22-8
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
use super::Ctx;
15+
use super::{get_hostname, Ctx};
1616
use bytes::BytesMut;
17-
use http::HeaderValue;
18-
use pingap_util::get_hostname;
17+
use http::{HeaderName, HeaderValue};
18+
use pingora::http::RequestHeader;
1919
use pingora::proxy::Session;
2020

2121
pub const HOST_NAME_TAG: &[u8] = b"$hostname";
@@ -31,6 +31,21 @@ const UPSTREAM_ADDR_TAG: &[u8] = b"$upstream_addr";
3131
static SCHEME_HTTPS: HeaderValue = HeaderValue::from_static("https");
3232
static SCHEME_HTTP: HeaderValue = HeaderValue::from_static("http");
3333

34+
/// Get request host in this order of precedence:
35+
/// host name from the request line,
36+
/// or host name from the "Host" request header field
37+
fn get_host(header: &RequestHeader) -> Option<&str> {
38+
if let Some(host) = header.uri.host() {
39+
return Some(host);
40+
}
41+
if let Some(host) = header.headers.get("Host") {
42+
if let Ok(value) = host.to_str().map(|host| host.split(':').next()) {
43+
return value;
44+
}
45+
}
46+
None
47+
}
48+
3449
/// Processes special header values that contain dynamic variables.
3550
/// Supports variables like $host, $scheme, $remote_addr etc.
3651
///
@@ -58,8 +73,7 @@ pub fn convert_header_value(
5873
let to_header_value = |s: &str| HeaderValue::from_str(s).ok();
5974

6075
match buf {
61-
HOST_TAG => pingap_util::get_host(session.req_header())
62-
.and_then(to_header_value),
76+
HOST_TAG => get_host(session.req_header()).and_then(to_header_value),
6377
SCHEME_TAG => Some(if ctx.tls_version.is_some() {
6478
SCHEME_HTTPS.clone()
6579
} else {
@@ -85,9 +99,9 @@ pub fn convert_header_value(
8599
},
86100
PROXY_ADD_FORWARDED_TAG => {
87101
ctx.remote_addr.as_deref().and_then(|remote_addr| {
88-
let value = match session.get_header(
89-
pingap_util::HTTP_HEADER_X_FORWARDED_FOR.clone(),
90-
) {
102+
let value = match session
103+
.get_header(HeaderName::from_static("X-Forwarded-For"))
104+
{
91105
Some(existing) => format!(
92106
"{}, {}",
93107
existing.to_str().unwrap_or_default(),

pingap-state/src/lib.rs

+18
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,26 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
use once_cell::sync::Lazy;
16+
1517
mod ctx;
1618
mod http_header;
1719

20+
static HOST_NAME: Lazy<String> = Lazy::new(|| {
21+
hostname::get()
22+
.unwrap_or_default()
23+
.to_str()
24+
.unwrap_or_default()
25+
.to_string()
26+
});
27+
28+
/// Returns the system hostname.
29+
///
30+
/// Returns:
31+
/// * `&'static str` - The system's hostname as a string slice
32+
pub fn get_hostname() -> &'static str {
33+
HOST_NAME.as_str()
34+
}
35+
1836
pub use ctx::*;
1937
pub use http_header::*;

0 commit comments

Comments
 (0)