Skip to content

Commit

Permalink
增加Layer组件Compression (#16)
Browse files Browse the repository at this point in the history
增加Layer组件Compression
  • Loading branch information
w-sodalite authored Jan 24, 2024
1 parent ea61f81 commit 1625680
Show file tree
Hide file tree
Showing 41 changed files with 445 additions and 116 deletions.
116 changes: 116 additions & 0 deletions Cargo.lock

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

27 changes: 14 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,20 @@

- #### Layer

| 名称 | 描述 | 使用文档 |
|:--------------------|--------------|:----------------------------------:|
| `Cors` | 处理跨域请求 | [文档](docs/layers/cors.md) |
| `KeepHostHeader` | 保持原始的Host请求头 | [文档](docs/matchers/query.md) |
| `PathStrip` | 从请求路径中删除特定部分 | [文档](docs/matchers/header.md) |
| `RateLimit` | 限制请求频率 | [文档](docs/matchers/host.md) |
| `RewritePath` | 重写请求的接口地址 | [文档](docs/matchers/time.md) |
| `SetRequestHeader` | 设置请求头信息 | [文档](docs/matchers/path.md) |
| `SetResponseHeader` | 设置响应头信息 | [文档](docs/matchers/path.md) |
| `XForward` | 设置XForward信息 | [文档](docs/matchers/path.md) |
| `ConcurrentcyLimit` | 限制同时处理的请求数量 | [文档](docs/matchers/remote_addr.md) |
| `RequestBodyLimit` | 限制请求体的最大大小 | [文档](docs/matchers/cookie.md) |
| `SetStatus` | 设置响应状态码 | [文档](docs/matchers/time.md) |
| 名称 | 描述 | 使用文档 |
|:--------------------|--------------|:----------------------------------------:|
| `Cors` | 处理跨域请求 | [文档](docs/layers/cors.md) |
| `KeepHostHeader` | 保持原始的Host请求头 | [文档](docs/layers/keep_host_header.md) |
| `PathStrip` | 从请求路径中删除特定部分 | [文档](docs/layers/path_strip.md) |
| `RateLimit` | 限制请求频率 | [文档](docs/layers/rate_limit.md) |
| `RewritePath` | 重写请求的接口地址 | [文档](docs/layers/rewrite_path.md) |
| `SetRequestHeader` | 设置请求头信息 | [文档](docs/layers/set_request_header.md) |
| `SetResponseHeader` | 设置响应头信息 | [文档](docs/layers/set_response_header.md) |
| `XForward` | 设置XForward信息 | [文档](docs/layers/x_forward.md) |
| `ConcurrentcyLimit` | 限制同时处理的请求数量 | [文档](docs/layers/concurrency_limit.md) |
| `RequestBodyLimit` | 限制请求体的大小 | [文档](docs/layers/request_body_limit.md) |
| `SetStatus` | 设置响应状态码 | [文档](docs/layers/set_status.md) |
| `Compression` | 压缩响应包体 | [文档](docs/layers/compression.md) |

- #### Service

Expand Down
47 changes: 47 additions & 0 deletions docs/layers/compression.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Compression

> 用来压缩响应包体
## 参数

| 参数 | 类型 | 是否必须 | 默认值 | 描述 |
|:----------------------|:-------:|:----:|:-----:|:---------------------|
| gzip | `bool` | `N` | true | 支持`gzip`压缩 |
| deflate | `bool` | `N` | false | 支持`deflate`压缩 |
| br | `bool` | `N` | false | 支持`br`压缩 |
| zstd | `bool` | `N` | false | 支持`zstd`压缩 |
| level | `i32` | `N` | 5 | 压缩级别,越小压缩率越低,但是速度越快。 |
| above_size | `u16` | `N` | 128 | 压缩的最小包体大小 |
| exclude_content_types | `[str]` | `N` | | 指定不需要压缩的content_type |

## 配置示例

- ### 简单配置

> 不支持配置值,使用默认值。
```yaml
router:
routes:
- id: static
layers:
- Compression
service: Static=examples/resources
```
- ### 完整模式
```yaml
router:
routes:
- id: static
layers:
- kind: Compression
args:
gzip: true
deflate: true
above_size: 128
exclude_content_types:
- application/grpc
service: Static=examples/resources
```
7 changes: 7 additions & 0 deletions examples/static.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,11 @@ router:
routes:
# 静态文件
- id: static
layers:
- kind: Compression
args:
gzip: true
above_size: 32
exclude_content_types:
- application/json
service: Static=examples/resources
4 changes: 2 additions & 2 deletions satex-core/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -334,11 +334,11 @@ impl Default for Trace {

impl Trace {
fn default_level() -> Level {
Level::DEBUG
Level::INFO
}

fn default_include_headers() -> bool {
true
false
}

pub fn level(&self) -> Level {
Expand Down
4 changes: 2 additions & 2 deletions satex-discovery/src/lb/make/ip_hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ use satex_core::essential::Essential;
use satex_core::Error;

use crate::lb::make::MakeLoadBalance;
use crate::lb::make::{make_load_balance, valid_endpoints};
use crate::lb::LoadBalance;
use crate::selector::SortedEndpoint;
use crate::{__make_load_balance, valid_endpoints};

const DEFAULT_TIMEOUT_SECS: u64 = 1800;
const DEFAULT_INTERVAL_SECS: u64 = 10;
Expand Down Expand Up @@ -133,7 +133,7 @@ impl LoadBalance for IpHashLoadBalance {
}
}

__make_load_balance! {
make_load_balance! {
IpHash,
#[serde(
deserialize_with = "satex_core::serde::tot::as_u64",
Expand Down
8 changes: 5 additions & 3 deletions satex-discovery/src/lb/make/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ make! {
///
#[doc(hidden)]
#[macro_export]
macro_rules! __make_load_balance {
macro_rules! make_load_balance {
($name:ident $(,)?) => {
satex_core::make_impl!(MakeLoadBalance,LoadBalance,$name);
};
Expand All @@ -34,11 +34,11 @@ macro_rules! __make_load_balance {
};
}

pub(crate) use make_load_balance;

///
/// 内部API
///
#[doc(hidden)]
#[macro_export]
macro_rules! valid_endpoints {
($endpoints:expr) => {{
match $endpoints.len() {
Expand All @@ -48,6 +48,8 @@ macro_rules! valid_endpoints {
}};
}

pub(crate) use valid_endpoints;

#[cfg(test)]
mod test {
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
Expand Down
4 changes: 2 additions & 2 deletions satex-discovery/src/lb/make/random.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ use satex_core::endpoint::Endpoint;
use satex_core::essential::Essential;
use satex_core::Error;

use crate::lb::make::{make_load_balance, valid_endpoints};
use crate::lb::{LoadBalance, MakeLoadBalance};
use crate::selector::SortedEndpoint;
use crate::{__make_load_balance, valid_endpoints};

pub struct RandomLoadBalance;

Expand All @@ -25,7 +25,7 @@ impl LoadBalance for RandomLoadBalance {
}
}

__make_load_balance!(Random);
make_load_balance!(Random);

fn make(_: Args) -> Result<RandomLoadBalance, Error> {
Ok(RandomLoadBalance)
Expand Down
4 changes: 2 additions & 2 deletions satex-discovery/src/lb/make/sequential.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ use satex_core::endpoint::Endpoint;
use satex_core::essential::Essential;
use satex_core::Error;

use crate::lb::make::{make_load_balance, valid_endpoints};
use crate::lb::{LoadBalance, MakeLoadBalance};
use crate::selector::SortedEndpoint;
use crate::{__make_load_balance, valid_endpoints};

#[derive(Default)]
pub struct SequentialLoadBalance {
Expand All @@ -30,7 +30,7 @@ impl LoadBalance for SequentialLoadBalance {
}
}

__make_load_balance!(Sequential);
make_load_balance!(Sequential);

fn make(_: Args) -> Result<SequentialLoadBalance, Error> {
Ok(SequentialLoadBalance::default())
Expand Down
Loading

0 comments on commit 1625680

Please sign in to comment.