From 2334f5a395c5cde8e1650b2fc27c3626193a9239 Mon Sep 17 00:00:00 2001 From: vicanso Date: Mon, 2 Sep 2024 21:12:20 +0800 Subject: [PATCH] refactor: adjust stats and compression plugin --- README.md | 1 + README_zh.md | 1 + conf/pingap.toml | 4 +- src/plugin/admin.rs | 6 +-- src/plugin/compression.rs | 111 ++++++++++++++++++-------------------- src/plugin/stats.rs | 5 +- 6 files changed, 62 insertions(+), 66 deletions(-) diff --git a/README.md b/README.md index bdbd92e..b8dc6e0 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/README_zh.md b/README_zh.md index ae3540a..e02f7c7 100644 --- a/README_zh.md +++ b/README_zh.md @@ -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的形式 diff --git a/conf/pingap.toml b/conf/pingap.toml index b32d5f7..ec24f62 100644 --- a/conf/pingap.toml +++ b/conf/pingap.toml @@ -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" @@ -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 diff --git a/src/plugin/admin.rs b/src/plugin/admin.rs index 6e2eb85..ce717d1 100644 --- a/src/plugin/admin.rs +++ b/src/plugin/admin.rs @@ -55,11 +55,9 @@ pub struct EmbeddedStaticFile(pub Option, pub Duration); impl From 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(); diff --git a/src/plugin/compression.rs b/src/plugin/compression.rs index 341a9e0..b2e3075 100644 --- a/src/plugin/compression.rs +++ b/src/plugin/compression.rs @@ -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::() - { - 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::() + 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) } diff --git a/src/plugin/stats.rs b/src/plugin/stats.rs index eac29b3..64ab0e3 100644 --- a/src/plugin/stats.rs +++ b/src/plugin/stats.rs @@ -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; @@ -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)