Skip to content

Commit

Permalink
feat: support get memory usage of pingap
Browse files Browse the repository at this point in the history
  • Loading branch information
vicanso committed Apr 3, 2024
1 parent e6c113d commit ad8bec9
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 0 deletions.
11 changes: 11 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ http = "1.1.0"
humantime = "2.1.0"
humantime-serde = "1.1.1"
log = "0.4.21"
memory-stats = { version = "1.1.0", features = ["always_use_statm"] }
mime_guess = "2.0.4"
num_cpus = "1.16.0"
once_cell = "1.19.0"
Expand Down
1 change: 1 addition & 0 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@
- [x] add remark for config
- [ ] support multi host for location?
- [ ] support set upstream_keepalive_pool_size
- [ ] graceful restart for admin web
11 changes: 11 additions & 0 deletions src/proxy/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ use crate::utils;
use async_trait::async_trait;
use base64::{engine::general_purpose::STANDARD, Engine};
use bytes::Bytes;
use bytesize::ByteSize;
use http::StatusCode;
use log::{error, info};
use memory_stats::memory_stats;
use once_cell::sync::Lazy;
use pingora::http::ResponseHeader;
use pingora::listeners::TlsSettings;
Expand Down Expand Up @@ -167,6 +169,8 @@ struct ServerStats {
processing: i32,
accepted: u64,
hostname: String,
physical_mem_mb: usize,
physical_mem: String,
}

pub struct ServerServices {
Expand Down Expand Up @@ -269,10 +273,17 @@ impl Server {
Ok(ServerServices { lb, bg_services })
}
async fn send_stats_response(&self, session: &mut Session, ctx: &mut State) {
let mut physical_mem = 0;
if let Some(value) = memory_stats() {
physical_mem = value.physical_mem;
}

let buf = serde_json::to_vec(&ServerStats {
accepted: self.accepted.load(Ordering::Relaxed),
processing: self.processing.load(Ordering::Relaxed),
hostname: HOST_NAME.to_string(),
physical_mem: ByteSize(physical_mem as u64).to_string_as(true),
physical_mem_mb: physical_mem / (1024 * 1024),
})
.unwrap_or_default();

Expand Down

0 comments on commit ad8bec9

Please sign in to comment.