use super::*;
#[inline(always)]
pub(crate) fn request_header_back<'a>(req: &'a Request, lower_key: &str) -> Option<&'a str> {
req.get_headers()
.get(lower_key)
.and_then(|values: &VecDeque<String>| values.back())
.map(String::as_str)
}
#[inline(always)]
pub(crate) fn format_u64_nl_into(mut value: u64, buf: &mut [u8; INT_STACK_BUF_LEN]) -> usize {
let mut pos: usize = INT_STACK_BUF_LEN - 1;
buf[pos] = b'\n';
while value >= 10_000 {
let chunk: usize = (value % 10_000) as usize;
value /= 10_000;
let lo_pair: usize = (chunk % 100) * 2;
let hi_pair: usize = (chunk / 100) * 2;
pos -= 4;
buf[pos] = PROM_DEC_DIGITS_LUT[hi_pair];
buf[pos + 1] = PROM_DEC_DIGITS_LUT[hi_pair + 1];
buf[pos + 2] = PROM_DEC_DIGITS_LUT[lo_pair];
buf[pos + 3] = PROM_DEC_DIGITS_LUT[lo_pair + 1];
}
if value >= 100 {
let pair: usize = ((value % 100) as usize) * 2;
value /= 100;
pos -= 2;
buf[pos] = PROM_DEC_DIGITS_LUT[pair];
buf[pos + 1] = PROM_DEC_DIGITS_LUT[pair + 1];
}
if value < 10 {
pos -= 1;
buf[pos] = ASCII_ZERO + (value as u8);
} else {
let pair: usize = (value as usize) * 2;
pos -= 2;
buf[pos] = PROM_DEC_DIGITS_LUT[pair];
buf[pos + 1] = PROM_DEC_DIGITS_LUT[pair + 1];
}
pos
}
#[inline(always)]
pub(crate) fn push_u64_nl(value: u64, out: &mut String) {
if value < 100 {
out.push_str(PROM_DEC_0_99_NL[value as usize]);
return;
}
let mut buf: [u8; INT_STACK_BUF_LEN] = [0u8; INT_STACK_BUF_LEN];
let pos: usize = format_u64_nl_into(value, &mut buf);
if let Ok(text) = from_utf8(&buf[pos..]) {
out.push_str(text);
}
}
#[inline(always)]
pub(crate) fn push_i64_nl(value: i64, out: &mut String) {
if value < 0 {
out.push('-');
}
push_u64_nl(value.unsigned_abs(), out);
}
#[inline(always)]
pub(crate) fn push_f64_nl(value: f64, out: &mut String) {
if value == 0.0 {
out.push_str(PROM_DEC_0_99_NL[0]);
return;
}
let _: fmt::Result = writeln!(out, "{value}");
}
#[inline]
pub(crate) fn constant_time_eq(a: &[u8], b: &[u8]) -> bool {
if a.len() != b.len() {
return false;
}
let mut diff: u8 = 0;
for i in 0..a.len() {
diff |= a[i] ^ b[i];
}
diff == 0
}
#[inline]
pub(crate) fn is_scrape_authorized(ctx: &Context, expected_token: Option<&str>) -> bool {
let Some(expected) = expected_token else {
return true;
};
let Some(header) = request_header_back(ctx.get_request(), AUTHORIZATION) else {
return false;
};
let trimmed: &str = header.trim();
if trimmed.len() < PREFIX_BEARER.len() {
return false;
}
let (scheme, token): (&str, &str) = trimmed.split_at(PREFIX_BEARER.len());
scheme.eq_ignore_ascii_case(PREFIX_BEARER)
&& constant_time_eq(token.trim().as_bytes(), expected.as_bytes())
}
#[inline(always)]
pub(crate) fn atomic_f64_add(target: &AtomicU64, delta: f64) {
let mut observed: u64 = target.load(Ordering::Relaxed);
loop {
let next: u64 = (f64::from_bits(observed) + delta).to_bits();
match target.compare_exchange_weak(observed, next, Ordering::Relaxed, Ordering::Relaxed) {
Ok(_) => break,
Err(actual) => observed = actual,
}
}
}
#[inline(always)]
pub(crate) fn worker_metrics_shard_index() -> usize {
WORKER_METRICS_SHARD.with(|slot: &Cell<usize>| {
let current: usize = slot.get();
if current != usize::MAX {
return current;
}
let assigned: usize =
METRICS_NEXT_SHARD_ID.fetch_add(1, Ordering::Relaxed) & METRICS_SHARD_MASK;
slot.set(assigned);
assigned
})
}
#[inline(always)]
pub(crate) fn put_metrics_started_at(ctx: &mut Context, started: Instant) {
if let Some(slot) = ctx
.get_mut_attributes()
.get_mut(ATTR_METRICS_STARTED_AT_MAP_KEY)
{
if let Some(any_mut) = Arc::get_mut(slot)
&& let Some(existing) = any_mut.downcast_mut::<Instant>()
{
*existing = started;
return;
}
*slot = Arc::new(started);
return;
}
ctx.get_mut_attributes().insert(
ATTR_METRICS_STARTED_AT_MAP_KEY.to_owned(),
Arc::new(started),
);
}
#[inline(always)]
pub(crate) fn get_metrics_started_at(ctx: &Context) -> Option<Instant> {
ctx.get_attributes()
.get(ATTR_METRICS_STARTED_AT_MAP_KEY)
.and_then(|arc: &ArcAnySendSync| arc.downcast_ref::<Instant>())
.copied()
}