Pooled Fetch
pooled-fetch is a lightweight, asynchronous HTTP client connection pool built on hyper. It's designed to improve performance by reusing connections, reducing the overhead of establishing new TCP and HTTP handshakes for each request.
pooled-fetch 是一个基于 hyper 构建的轻量级、异步的 HTTP 客户端连接池。它通过复用连接来提升性能,减少了为每个请求建立新的 TCP 连接和 HTTP 握手的开销。
English README
Overview
This library provides a simple function, http, for making HTTP requests. It transparently manages a global pool of connections. When a request is made to a specific address, the library first attempts to retrieve a ready-to-use connection from the pool. If no connection is available, it establishes a new one and adds it to the pool for future use.
The magic lies in the custom Body wrapper. When the response body is fully consumed or dropped, the underlying connection is automatically returned to the pool, making it available for subsequent requests to the same destination.
Features
- Connection Pooling: Automatically reuses
hyperclient connections to reduce latency. - Asynchronous: Built on
tokioandhyperfor non-blocking I/O. - Thread-Safe: Uses
dashmapandcrossbeam-skiplistto ensure safe concurrent access to the connection pool. - Automatic Lifecycle Management: Connections are automatically returned to the pool upon response body completion.
How It Works
- Request: Call the
pooled_fetch::http(addr, request)function. - Pool Check: The library checks a global
DashMapfor an available connection to the targetSocketAddr. - Connection Reuse: If a cached connection is found, it's used to send the request.
- New Connection: If no connection is available, a new one is established using
tokio::net::TcpStreamandhyper, and the connection is stored in the pool. - Response and Return: The response body is wrapped in a custom
Bodystruct. Once theBodyis dropped (i.e., the response is fully read or goes out of scope), itsDropimplementation returns the connection to the pool.
Example Usage
use ;
use ;
use SocketAddr;
use FromStr;
async
async
中文说明
概述
pooled-fetch 是一个基于 hyper 构建的轻量级、异步的 HTTP 客户端连接池。它通过复用连接来提升性能,减少了为每个请求建立新的 TCP 连接和 HTTP 握手的开销。
本库提供了一个简单的 http 函数来发送 HTTP 请求,并透明地管理一个全局连接池。当向特定地址发出请求时,它会首先尝试从池中获取一个可用的连接。如果没有可用连接,它会建立一个新连接,并将其放入池中以备将来使用。
其核心机制在于自定义的 Body 封装类型。当响应体被完全消耗或被丢弃时,底层的连接会自动返回到池中,使其可以被后续发送到同一目标的请求复用。
特性
- 连接池: 自动复用
hyper客户端连接,以降低延迟。 - 异步: 基于
tokio和hyper构建,实现完全的非阻塞 I/O。 - 线程安全: 使用
dashmap和crossbeam-skiplist确保对连接池的并发访问是安全的。 - 自动生命周期管理: 在响应体处理完毕后,连接会自动返回到池中。
工作原理
- 发起请求: 调用
pooled_fetch::http(addr, request)函数。 - 检查池: 库会检查一个全局的
DashMap,查找是否有到目标SocketAddr的可用连接。 - 复用连接: 如果找到缓存的连接,就用它来发送请求。
- 新建连接: 如果没有可用连接,库会使用
tokio::net::TcpStream和hyper建立一个新连接,并将其存入池中。 - 响应与归还: 响应体被封装在一个自定义的
Body结构中。一旦Body被drop(例如,响应被完全读取或超出作用域),其Drop实现会将连接归还到池中。
使用示例
use ;
use ;
use SocketAddr;
use FromStr;
async
async