Reqwest Management Pool
English
Overview
reqwest-management-pool is a high-performance HTTP client connection pool library for Rust, built on top of reqwest and tokio. It automatically manages a pool of HTTP clients, enabling connection reuse and reducing memory consumption while supporting concurrent access.
Features
- ✅ Automatic Pool Management: Automatically manages the reqwest client connection pool
- ✅ Connection Reuse: Supports connection reuse, significantly improving performance
- ✅ Automatic Cleanup: Automatically releases and cleans up idle connections
- ✅ Thread-Safe: Fully thread-safe, supporting concurrent access from multiple tasks
- ✅ Dynamic Scaling: Automatically expands and shrinks the pool size based on demand
- ✅ Zero Configuration: Works out of the box with sensible defaults
Installation
Add this to your Cargo.toml:
[]
= "0.1.3"
= { = "1.49.0", = ["full"] }
= "0.13.2"
Quick Start
use ClientPool;
async
Usage Examples
Basic Usage
use ClientPool;
async
Concurrent Requests
use ClientPool;
async
Pool Statistics
use ClientPool;
async
API Reference
ClientPool
The main structure for managing the HTTP client pool.
Methods
-
new() -> ClientPoolCreates a new client pool with default settings:
- Default pool size: 8 clients
- Maximum pool size: 100 clients
- Idle timeout: 120 seconds
- Cleanup interval: 90 seconds
-
malloc() -> Option<PooledClientInner>Acquires a client from the pool. Returns
Noneif the pool is exhausted (though the pool will automatically expand).The returned client is automatically returned to the pool when dropped.
-
total_count() -> usizeReturns the current total number of clients in the pool.
-
working_count() -> usizeReturns the number of clients currently in use.
-
idle_count() -> usizeReturns the number of currently idle clients.
PooledClientInner
A wrapper around a reqwest::Client that automatically returns to the pool when dropped.
Configuration
The pool uses the following default constants (defined in src/lib.rs):
MIN_IDLE_SIZE: 8 (default pool size)MAX_SIZE: 100 (maximum pool size)IDLE_TIMEOUT: 120 seconds (idle client timeout)CLEANUP_INTERVAL: 90 seconds (cleanup task interval)
To customize these values, you can modify the constants in the source code or fork the repository.
How It Works
-
Initialization: The pool pre-creates a default number of clients (8 by default).
-
Client Acquisition: When you call
malloc(), the pool:- First tries to find an idle client
- If no idle client is available, creates a new one
- Marks the client as "in use"
-
Client Release: When a
PooledClientInneris dropped:- It automatically sends a release message back to the pool
- The pool marks the client as idle and resets its idle timer
-
Automatic Cleanup: A background task periodically:
- Checks for idle clients that have exceeded the idle timeout
- Removes them from the pool (but keeps at least the default pool size)
- This prevents memory bloat during low-traffic periods
License
This project is licensed under the MIT License.
中文
概述
reqwest-management-pool 是一个基于 Rust 的高性能 HTTP 客户端连接池库,构建在 reqwest 和 tokio 之上。它自动管理 HTTP 客户端池,支持连接复用,减少内存消耗,同时支持并发访问。
特性
- ✅ 自动池管理: 自动管理 reqwest 客户端连接池
- ✅ 连接复用: 支持连接复用,显著提升性能
- ✅ 自动清理: 自动释放和清理空闲连接
- ✅ 线程安全: 完全线程安全,支持多任务并发访问
- ✅ 动态扩展: 根据需求自动扩展和收缩池大小
- ✅ 零配置: 开箱即用,提供合理的默认值
安装
在 Cargo.toml 中添加以下依赖:
[]
= "0.1.3"
= { = "1.49.0", = ["full"] }
= "0.13.2"
快速开始
use ClientPool;
async
使用示例
基本用法
use ClientPool;
async
并发请求
use ClientPool;
async
池统计信息
use ClientPool;
async
API 参考
ClientPool
管理 HTTP 客户端池的主要结构体。
方法
-
new() -> ClientPool创建一个具有默认设置的新客户端池:
- 默认池大小:8 个客户端
- 空闲超时:120 秒
- 清理间隔:90 秒
-
malloc() -> Option<PooledClientInner>从池中获取一个客户端。如果池已耗尽则返回
None(尽管池会自动扩展)。返回的客户端在销毁时会自动返回池中。
-
total_count() -> usize返回池中当前客户端的总数。
-
working_count() -> usize返回当前正在使用的客户端数量。
-
idle_count() -> usize返回当前空闲的客户端数量。
PooledClientInner
一个包装了 reqwest::Client 的结构体,在销毁时自动返回池中。
方法
-
get() -> &Client返回底层
reqwest::Client的引用。 -
id() -> usize返回此客户端在池中的唯一 ID。
配置
池使用以下默认常量(在 src/lib.rs 中定义):
MIN_IDLE_SIZE: 8(默认池大小)MAX_SIZE: 100(最大池大小)IDLE_TIMEOUT: 120 秒(空闲客户端超时)CLEANUP_INTERVAL: 90 秒(清理任务间隔)
要自定义这些值,您可以修改源代码中的常量或 fork 仓库。
工作原理
-
初始化: 池会预创建默认数量的客户端(默认为 8 个)。
-
客户端获取: 当您调用
malloc()时,池会:- 首先尝试找到一个空闲的客户端
- 如果没有可用的空闲客户端,则创建一个新的
- 将客户端标记为"使用中"
-
客户端释放: 当
PooledClientInner被销毁时:- 它会自动发送释放消息回池中
- 池将客户端标记为空闲并重置其空闲计时器
-
自动清理: 后台任务定期:
- 检查超过空闲超时的空闲客户端
- 将它们从池中移除(但至少保留默认池大小)
- 这可以防止在低流量期间内存膨胀
许可证
本项目采用 MIT 许可证。
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
欢迎贡献!请随时提交 Pull Request。