Skip to content

Commit

Permalink
refactor: adjust super timestamp function
Browse files Browse the repository at this point in the history
  • Loading branch information
vicanso committed May 5, 2024
1 parent 26d6760 commit b94c96f
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 22 deletions.
3 changes: 2 additions & 1 deletion benches/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ use bytes::Bytes;
use criterion::{criterion_group, criterion_main, Criterion};
use http::{HeaderName, HeaderValue, StatusCode};
use pingap::config::{LocationConf, UpstreamConf};
use pingap::http_extra::{convert_headers, get_super_ts, HttpResponse};
use pingap::http_extra::{convert_headers, HttpResponse};
use pingap::proxy::{Location, Parser, Upstream};
use pingap::state::State;
use pingap::util::get_super_ts;
use pingora::http::ResponseHeader;
use pingora::proxy::Session;
use std::sync::Arc;
Expand Down
25 changes: 4 additions & 21 deletions src/http_extra/http_response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,30 +21,13 @@ use bytes::Bytes;
use http::header;
use http::StatusCode;
use log::error;
use once_cell::sync::Lazy;
use pingora::http::ResponseHeader;
use pingora::proxy::Session;
use serde::Serialize;
use std::pin::Pin;
use std::time::{Duration, SystemTime, UNIX_EPOCH};
use std::time::{SystemTime, UNIX_EPOCH};
use tokio::io::AsyncReadExt;

// 2022-05-07: 1651852800
// const SUPER_TIMESTAMP: u64 = 1651852800;
static SUPER_TIMESTAMP: Lazy<SystemTime> = Lazy::new(|| {
UNIX_EPOCH
.checked_add(Duration::from_secs(1651852800))
.unwrap_or(SystemTime::now())
});

pub fn get_super_ts() -> u32 {
if let Ok(value) = SystemTime::now().duration_since(*SUPER_TIMESTAMP) {
value.as_secs() as u32
} else {
0
}
}

pub fn get_hour_duration() -> u32 {
if let Ok(value) = SystemTime::now().duration_since(UNIX_EPOCH) {
(value.as_millis() % (3600 * 1000)) as u32
Expand Down Expand Up @@ -161,7 +144,7 @@ impl HttpResponse {
resp.insert_header(cache_control.0, cache_control.1)?;

if let Some(created_at) = self.created_at {
let secs = get_super_ts() - created_at;
let secs = util::get_super_ts() - created_at;
if let Ok(value) = header::HeaderValue::from_str(&secs.to_string()) {
resp.insert_header(header::AGE, value)?;
}
Expand Down Expand Up @@ -256,9 +239,9 @@ where

#[cfg(test)]
mod tests {
use super::{get_cache_control, get_super_ts, HttpChunkResponse, HttpResponse};
use super::{get_cache_control, HttpChunkResponse, HttpResponse};
use crate::http_extra::convert_headers;
use crate::util::resolve_path;
use crate::util::{get_super_ts, resolve_path};
use bytes::Bytes;
use http::StatusCode;
use pretty_assertions::assert_eq;
Expand Down
17 changes: 17 additions & 0 deletions src/util/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use http::HeaderName;
use once_cell::sync::Lazy;
use path_absolutize::*;
use pingora::{http::RequestHeader, proxy::Session};
use std::time::{Duration, SystemTime, UNIX_EPOCH};
use std::{path::Path, str::FromStr};
use substring::Substring;

Expand Down Expand Up @@ -137,3 +138,19 @@ pub fn new_internal_error(status: u16, message: String) -> pingora::BError {
pub fn is_pem(value: &str) -> bool {
value.starts_with("-----")
}

// 2022-05-07: 1651852800
// const SUPER_TIMESTAMP: u64 = 1651852800;
static SUPER_TIMESTAMP: Lazy<SystemTime> = Lazy::new(|| {
UNIX_EPOCH
.checked_add(Duration::from_secs(1651852800))
.unwrap_or(SystemTime::now())
});

pub fn get_super_ts() -> u32 {
if let Ok(value) = SystemTime::now().duration_since(*SUPER_TIMESTAMP) {
value.as_secs() as u32
} else {
0
}
}

0 comments on commit b94c96f

Please sign in to comment.