Skip to content

Commit

Permalink
feat: support get processing and accepted request of pingap
Browse files Browse the repository at this point in the history
  • Loading branch information
vicanso committed Sep 29, 2024
1 parent 3205786 commit 1e57ebf
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 6 deletions.
7 changes: 6 additions & 1 deletion src/plugin/admin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use crate::config::{
};
use crate::http_extra::{HttpResponse, HTTP_HEADER_WWW_AUTHENTICATE};
use crate::limit::TtlLruLimit;
use crate::state::get_start_time;
use crate::state::{get_processing_accepted, get_start_time};
use crate::state::{restart_now, State};
use crate::util::{self, base64_decode};
use async_trait::async_trait;
Expand Down Expand Up @@ -133,6 +133,8 @@ struct BasicInfo {
user: String,
group: String,
threads: usize,
processing: i32,
accepted: u64,
}

impl TryFrom<&PluginConf> for AdminServe {
Expand Down Expand Up @@ -457,6 +459,7 @@ impl Plugin for AdminServe {
threads += count;
}
}
let (processing, accepted) = get_processing_accepted();

HttpResponse::try_from_json(&BasicInfo {
start_time: get_start_time(),
Expand All @@ -469,6 +472,8 @@ impl Plugin for AdminServe {
pid,
memory,
threads,
accepted,
processing,
})
.unwrap_or(HttpResponse::unknown_error("Json serde fail".into()))
} else if path == "/restart" && method == Method::POST {
Expand Down
9 changes: 6 additions & 3 deletions src/plugin/stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
use super::{get_hash_key, get_step_conf, get_str_conf, Error, Plugin, Result};
use crate::config::{PluginCategory, PluginConf, PluginStep};
use crate::http_extra::HttpResponse;
use crate::state::{get_hostname, get_start_time, State};
use crate::state::{
get_hostname, get_processing_accepted, get_start_time, State,
};
use crate::util;
use async_trait::async_trait;
use bytes::Bytes;
Expand Down Expand Up @@ -100,9 +102,10 @@ impl Plugin for Stats {
let uptime: humantime::Duration =
Duration::from_secs(util::now().as_secs() - get_start_time())
.into();
let (processing, accepted) = get_processing_accepted();
let resp = HttpResponse::try_from_json(&ServerStats {
accepted: ctx.accepted,
processing: ctx.processing,
accepted,
processing,
location_processing: ctx.location_processing,
location_accepted: ctx.location_accepted,
hostname: get_hostname().to_string(),
Expand Down
4 changes: 3 additions & 1 deletion src/proxy/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use crate::plugin::{get_plugin, ADMIN_SERVER_PLUGIN};
use crate::proxy::dynamic_certificate::TlsSettingParams;
use crate::proxy::location::get_location;
use crate::service::CommonServiceTask;
use crate::state::OtelTracer;
use crate::state::{accept_request, end_request, OtelTracer};
use crate::state::{new_prometheus, new_prometheus_push_service};
use crate::state::{CompressionStat, Prometheus, State};
use crate::util;
Expand Down Expand Up @@ -427,6 +427,7 @@ impl ProxyHttp for Server {
ctx.tls_cipher = digest_detail.tls_cipher;
ctx.tls_version = digest_detail.tls_version;
};
accept_request();

ctx.processing = self.processing.fetch_add(1, Ordering::Relaxed) + 1;
ctx.accepted = self.accepted.fetch_add(1, Ordering::Relaxed) + 1;
Expand Down Expand Up @@ -967,6 +968,7 @@ impl ProxyHttp for Server {
) where
Self::CTX: Send + Sync,
{
end_request();
self.processing.fetch_sub(1, Ordering::Relaxed);
if let Some(location) = &ctx.location {
location.processing.fetch_sub(1, Ordering::Relaxed);
Expand Down
20 changes: 19 additions & 1 deletion src/state/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,32 @@ use std::io;
use std::path::PathBuf;
use std::process;
use std::process::Command;
use std::sync::atomic::{AtomicBool, AtomicU8, Ordering};
use std::sync::atomic::{AtomicBool, AtomicI32, AtomicU64, AtomicU8, Ordering};
use std::time::Duration;
use tracing::{error, info};

static START_TIME: Lazy<Duration> = Lazy::new(util::now);

static ADMIN_ADDR: OnceCell<String> = OnceCell::new();

static ACCEPTED: Lazy<AtomicU64> = Lazy::new(|| AtomicU64::new(0));
static PROCESSING: Lazy<AtomicI32> = Lazy::new(|| AtomicI32::new(0));

pub fn accept_request() {
ACCEPTED.fetch_add(1, Ordering::Relaxed);
PROCESSING.fetch_add(1, Ordering::Relaxed);
}

pub fn end_request() {
PROCESSING.fetch_sub(1, Ordering::Relaxed);
}

pub fn get_processing_accepted() -> (i32, u64) {
let processing = PROCESSING.load(Ordering::Relaxed);
let accepted = ACCEPTED.load(Ordering::Relaxed);
(processing, accepted)
}

pub fn set_admin_addr(addr: &str) {
ADMIN_ADDR.get_or_init(|| addr.to_string());
}
Expand Down
2 changes: 2 additions & 0 deletions web/src/i18n/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ export default {
basic: "Basic Information",
pid: "Daemon Process Id",
threads: "Threads",
accepted: "Accepted",
processing: "Processing",
memory: "Memory",
startTime: "Start Time",
arch: "Arch",
Expand Down
2 changes: 2 additions & 0 deletions web/src/i18n/zh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ export default {
basic: "基础信息",
pid: "后台服务进程id",
threads: "线程数",
accepted: "处理请求数",
processing: "正在处理请求数",
memory: "内存",
startTime: "启用时间",
arch: "架构",
Expand Down
8 changes: 8 additions & 0 deletions web/src/pages/Home.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,14 @@ export default function Home() {
name: "startTime",
value: new Date(basicInfo.start_time * 1000).toLocaleString(),
},
{
name: "processing",
value: basicInfo.processing.toLocaleString(),
},
{
name: "accepted",
value: basicInfo.accepted.toLocaleString(),
},
{
name: "arch",
value: basicInfo.arch,
Expand Down
4 changes: 4 additions & 0 deletions web/src/states/basic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ interface Basic {
user: string;
group: string;
threads: number;
accepted: number;
processing: number;
}

interface ConfigState {
Expand All @@ -32,6 +34,8 @@ const useBasicState = create<ConfigState>()((set) => ({
user: "",
group: "",
threads: 0,
accepted: 0,
processing: 0,
},
initialized: false,
fetch: async () => {
Expand Down

0 comments on commit 1e57ebf

Please sign in to comment.