Skip to content

Commit

Permalink
refactor: adjust stats and compression plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
vicanso committed Sep 2, 2024
1 parent 8da5d08 commit 2334f5a
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 66 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ flowchart LR
- The service supports configuring multiple Locations, filtering locations by host and path, and matching and selecting them one by one according to the weight
- Supports regular form configuration to rewrite Path
- Support HTTP 1/2, including h2c
- Support static, dns and docker label service discovery
- Configuration based on TOML format, the configuration method is very simple, and can be saved to files or etcd
- Supports more than 10 Prometheus indicators, pull and push mode
- Opentelemetry supports w3c context trace and jaeger trace
Expand Down
1 change: 1 addition & 0 deletions README_zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ flowchart LR
- 服务支持配置多个Location,通过host与path筛选对应的location,按权重逐一匹配选择
- 支持正则形式配置重写Path,方便应用按前缀区分转发
- HTTP 1/2 的全链路支持,包括h2c的支持
- 支持静态配置、DNS以及docker label的三种服务发现形式
- 基于TOML格式的配置,配置方式非常简洁,可保存至文件或etcd
- 支持10多个Prometheus指标,可以使用pull与push的形式收集相关指标
- Opentelemetry支持w3c context trace与jaeger trace的形式
Expand Down
4 changes: 2 additions & 2 deletions conf/pingap.toml
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ tcp_interval = "1m"

# the maximum number of TCP keep-alive probes to send
# before giving up and killing the connection
tcp_probe_count = 100
tcp_probe_count = 9

# tcp receive buffer size (default none)
tcp_recv_buf = "4kb"
Expand Down Expand Up @@ -194,7 +194,7 @@ tcp_interval = "1m"

# the maximum number of TCP keep-alive probes to send
# before giving up and killing the connection
tcp_probe_count = 100
tcp_probe_count = 9

# enable TCP fast open and set the backlog size of it (defualt none)
tcp_fast_open = 10
Expand Down
6 changes: 2 additions & 4 deletions src/plugin/admin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,9 @@ pub struct EmbeddedStaticFile(pub Option<EmbeddedFile>, pub Duration);

impl From<EmbeddedStaticFile> for HttpResponse {
fn from(value: EmbeddedStaticFile) -> Self {
if value.0.is_none() {
let Some(file) = value.0 else {
return HttpResponse::not_found("Not Found".into());
}
// value 0 is some
let file = value.0.unwrap();
};
// generate content hash
let str = &encode(file.metadata.sha256_hash())[0..8];
let mime_type = file.metadata.mimetype();
Expand Down
111 changes: 52 additions & 59 deletions src/plugin/compression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,66 +98,59 @@ impl Plugin for Compression {
return Ok(None);
}
let header = session.req_header_mut();
if let Some(accept_encoding) =
let Some(accept_encoding) =
header.headers.get(http::header::ACCEPT_ENCODING)
{
let accept_encoding = accept_encoding.to_str().unwrap_or_default();
if accept_encoding.is_empty() {
return Ok(None);
}
// compression order, zstd > br > gzip
// Wait for pingora support to specify the order
let level = if self.zstd_level > 0 && accept_encoding.contains(ZSTD)
{
let _ = header.insert_header(
http::header::ACCEPT_ENCODING,
ZSTD_ENCODING.clone(),
);
self.zstd_level
} else if self.br_level > 0 && accept_encoding.contains(BR) {
let _ = header.insert_header(
http::header::ACCEPT_ENCODING,
BR_ENCODING.clone(),
);
self.br_level
} else if self.gzip_level > 0 && accept_encoding.contains(GZIP) {
let _ = header.insert_header(
http::header::ACCEPT_ENCODING,
GZIP_ENCODING.clone(),
);
self.gzip_level
} else {
0
};
debug!(level, "compression level");
if level > 0 {
if let Some(c) = session
.downstream_modules_ctx
.get_mut::<ResponseCompression>()
{
if let Some(decompression) = self.decompression {
c.adjust_decompression(decompression);
}
if self.zstd_level > 0 {
c.adjust_algorithm_level(
Algorithm::Zstd,
self.zstd_level,
);
}
if self.br_level > 0 {
c.adjust_algorithm_level(
Algorithm::Brotli,
self.br_level,
);
}
if self.gzip_level > 0 {
c.adjust_algorithm_level(
Algorithm::Gzip,
self.gzip_level,
);
}
}
}
else {
return Ok(None);
};
let accept_encoding = accept_encoding.to_str().unwrap_or_default();
if accept_encoding.is_empty() {
return Ok(None);
}
// compression order, zstd > br > gzip
// Wait for pingora support to specify the order
let level = if self.zstd_level > 0 && accept_encoding.contains(ZSTD) {
let _ = header.insert_header(
http::header::ACCEPT_ENCODING,
ZSTD_ENCODING.clone(),
);
self.zstd_level
} else if self.br_level > 0 && accept_encoding.contains(BR) {
let _ = header.insert_header(
http::header::ACCEPT_ENCODING,
BR_ENCODING.clone(),
);
self.br_level
} else if self.gzip_level > 0 && accept_encoding.contains(GZIP) {
let _ = header.insert_header(
http::header::ACCEPT_ENCODING,
GZIP_ENCODING.clone(),
);
self.gzip_level
} else {
0
};
debug!(level, "compression level");
if level == 0 {
return Ok(None);
}
let Some(c) = session
.downstream_modules_ctx
.get_mut::<ResponseCompression>()
else {
return Ok(None);
};
if let Some(decompression) = self.decompression {
c.adjust_decompression(decompression);
}
if self.zstd_level > 0 {
c.adjust_algorithm_level(Algorithm::Zstd, self.zstd_level);
}
if self.br_level > 0 {
c.adjust_algorithm_level(Algorithm::Brotli, self.br_level);
}
if self.gzip_level > 0 {
c.adjust_algorithm_level(Algorithm::Gzip, self.gzip_level);
}
Ok(None)
}
Expand Down
5 changes: 4 additions & 1 deletion src/plugin/stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use crate::http_extra::HttpResponse;
use crate::state::{get_hostname, get_start_time, State};
use crate::util;
use async_trait::async_trait;
use bytes::Bytes;
use bytesize::ByteSize;
use memory_stats::memory_stats;
use pingora::proxy::Session;
Expand Down Expand Up @@ -112,7 +113,9 @@ impl Plugin for Stats {
start_time: get_start_time(),
uptime: uptime.to_string(),
})
.map_err(|e| util::new_internal_error(500, e.to_string()))?;
.unwrap_or_else(|e| {
HttpResponse::unknown_error(Bytes::from(e.to_string()))
});
return Ok(Some(resp));
}
Ok(None)
Expand Down

0 comments on commit 2334f5a

Please sign in to comment.