From 1e57ebf746587e7e80d85adc8fdc4ab846963140 Mon Sep 17 00:00:00 2001 From: vicanso Date: Sun, 29 Sep 2024 20:54:01 +0800 Subject: [PATCH] feat: support get processing and accepted request of pingap --- src/plugin/admin.rs | 7 ++++++- src/plugin/stats.rs | 9 ++++++--- src/proxy/server.rs | 4 +++- src/state/process.rs | 20 +++++++++++++++++++- web/src/i18n/en.ts | 2 ++ web/src/i18n/zh.ts | 2 ++ web/src/pages/Home.tsx | 8 ++++++++ web/src/states/basic.ts | 4 ++++ 8 files changed, 50 insertions(+), 6 deletions(-) diff --git a/src/plugin/admin.rs b/src/plugin/admin.rs index 8cb56d68..591bc4c4 100644 --- a/src/plugin/admin.rs +++ b/src/plugin/admin.rs @@ -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; @@ -133,6 +133,8 @@ struct BasicInfo { user: String, group: String, threads: usize, + processing: i32, + accepted: u64, } impl TryFrom<&PluginConf> for AdminServe { @@ -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(), @@ -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 { diff --git a/src/plugin/stats.rs b/src/plugin/stats.rs index 64ab0e36..ebf69085 100644 --- a/src/plugin/stats.rs +++ b/src/plugin/stats.rs @@ -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; @@ -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(), diff --git a/src/proxy/server.rs b/src/proxy/server.rs index 6bad22f0..3d5fa483 100644 --- a/src/proxy/server.rs +++ b/src/proxy/server.rs @@ -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; @@ -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; @@ -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); diff --git a/src/state/process.rs b/src/state/process.rs index 8b042601..84c27ac8 100644 --- a/src/state/process.rs +++ b/src/state/process.rs @@ -20,7 +20,7 @@ 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}; @@ -28,6 +28,24 @@ static START_TIME: Lazy = Lazy::new(util::now); static ADMIN_ADDR: OnceCell = OnceCell::new(); +static ACCEPTED: Lazy = Lazy::new(|| AtomicU64::new(0)); +static PROCESSING: Lazy = 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()); } diff --git a/web/src/i18n/en.ts b/web/src/i18n/en.ts index 28fcc269..6a008442 100644 --- a/web/src/i18n/en.ts +++ b/web/src/i18n/en.ts @@ -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", diff --git a/web/src/i18n/zh.ts b/web/src/i18n/zh.ts index a244e5fa..c6d9029e 100644 --- a/web/src/i18n/zh.ts +++ b/web/src/i18n/zh.ts @@ -32,6 +32,8 @@ export default { basic: "基础信息", pid: "后台服务进程id", threads: "线程数", + accepted: "处理请求数", + processing: "正在处理请求数", memory: "内存", startTime: "启用时间", arch: "架构", diff --git a/web/src/pages/Home.tsx b/web/src/pages/Home.tsx index b760e7c5..dd4be39f 100644 --- a/web/src/pages/Home.tsx +++ b/web/src/pages/Home.tsx @@ -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, diff --git a/web/src/states/basic.ts b/web/src/states/basic.ts index 19feebea..760bd015 100644 --- a/web/src/states/basic.ts +++ b/web/src/states/basic.ts @@ -12,6 +12,8 @@ interface Basic { user: string; group: string; threads: number; + accepted: number; + processing: number; } interface ConfigState { @@ -32,6 +34,8 @@ const useBasicState = create()((set) => ({ user: "", group: "", threads: 0, + accepted: 0, + processing: 0, }, initialized: false, fetch: async () => {