Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

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

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