Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/riron/swim
A generic connection pool for Rust
https://github.com/riron/swim
async-await connection-pool database rust rust-lang
Last synced: 20 days ago
JSON representation
A generic connection pool for Rust
- Host: GitHub
- URL: https://github.com/riron/swim
- Owner: Riron
- License: mit
- Created: 2023-05-09T20:19:35.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2023-05-11T07:50:05.000Z (over 1 year ago)
- Last Synced: 2024-10-16T02:41:23.288Z (2 months ago)
- Topics: async-await, connection-pool, database, rust, rust-lang
- Language: Rust
- Homepage:
- Size: 11.7 KB
- Stars: 3
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Swim
A generic connection pool with [Tokio](https://tokio.rs/) support.
Inspired by [a Mobc rewrite that never happened](https://github.com/importcjj/mobc/pull/60#issuecomment-879616110).
This connection pool is **lock free** and uses a **single-thread event loop** instead. It seems to have a much better performance.
# Example
```rust
use swim::{Pool, PoolConfig};#[tokio::main]
async fn main() {
let manager = FooConnectionManager::new("foo://localhost:0000"); // Imaginary "foo" DB.
let pool = Pool::new(manager, PoolConfig::default().max_open(50));for i in 0..20 {
let pool = pool.clone();
tokio::spawn(async move {
let client = pool.get().await.unwrap();
// use the connection
// it will be returned to the pool when it falls out of scope.
});
}
}
```# Config
The config must be passed to the pool upon creation. It comes with default params that can be changed through a fluent API.
```rust
let config = PoolConfig::default()
.max_open(20)
.cleanup_rate(Duration::from_secs(5 * 60));
```|Param|Type|Description|Default|
|-----|----|-----------|-------|
|max_open|`u16`|Maximum number of connections managed by the pool. Defaults to 10.
|max_idle|`Option`|Maximum idle connection count maintained by the pool.|None, meaning reuse forever
|max_lifetime|`Option`|Maximum lifetime of connections in the pool. |None, meaning reuse forever
|idle_timeout|`Option`|Maximum lifetime of idle connections in the pool.|5min
|cleanup_rate|`Duration`|Rate at which a connection cleanup is scheduled. |60sec
|test_on_check_out|`bool`|Check the connection before returning it to the client.|false
|get_timeout|`Duration`|Maximum time to wait for a connection to become available before returning an error.|30sec