• Stars
    star
    283
  • Rank 146,066 (Top 3 %)
  • Language
    Rust
  • License
    Apache License 2.0
  • Created about 5 years ago
  • Updated about 1 month ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A generic connection pool for Rust with async/await support

Mobc

A generic connection pool with async/await support.

Inspired by Deadpool, Sqlx, r2d2 and Golang SQL package.

Changelog

Note: mobc requires at least Rust 1.60.

Usage

[dependencies]
mobc = "0.8"

# For async-std runtime
# mobc = { version = "0.8", features = ["async-std"] }

# For actix-rt 1.0
# mobc = { version = "0.8", features = ["actix-rt"] }

Features

  • Support async/.await syntax
  • Support both tokio and async-std
  • Tokio metric support
  • Production battle tested
  • High performance
  • Easy to customize
  • Dynamic configuration

Adaptors

Backend Adaptor Crate
bolt-client mobc-bolt
tokio-postgres mobc-postgres
redis mobc-redis
arangodb mobc-arangors
lapin mobc-lapin
reql mobc-reql
redis-cluster mobc-redis-cluster

More DB adaptors are welcome.

Examples

More examples

Using an imaginary "foodb" database.

use mobc::{async_trait, Manager};

#[derive(Debug)]
pub struct FooError;

pub struct FooConnection;

impl FooConnection {
    pub async fn query(&self) -> String {
        "PONG".to_string()
    }
}

pub struct FooManager;

#[async_trait]
impl Manager for FooManager {
    type Connection = FooConnection;
    type Error = FooError;

    async fn connect(&self) -> Result<Self::Connection, Self::Error> {
        Ok(FooConnection)
    }

    async fn check(&self, conn: Self::Connection) -> Result<Self::Connection, Self::Error> {
        Ok(conn)
    }
}

Configures

max_open

Sets the maximum number of connections managed by the pool.

0 means unlimited, defaults to 10.

min_idle

Sets the maximum idle connection count maintained by the pool. The pool will maintain at most this many idle connections at all times, while respecting the value of max_open.

max_lifetime

Sets the maximum lifetime of connections in the pool. Expired connections may be closed lazily before reuse.

None meas reuse forever, defaults to None.

get_timeout

Sets the get timeout used by the pool. Calls to Pool::get will wait this long for a connection to become available before returning an error.

None meas never timeout, defaults to 30 seconds.

Variable

Some of the connection pool configurations can be adjusted dynamically. Each connection pool instance has the following methods:

  • set_max_open_conns
  • set_max_idle_conns
  • set_conn_max_lifetime

Stats

  • max_open - Maximum number of open connections to the database.
  • connections - The number of established connections both in use and idle.
  • in_use - The number of connections currently in use.
  • idle - The number of idle connections.
  • wait_count - The total number of connections waited for.
  • wait_duration - The total time blocked waiting for a new connection.
  • max_idle_closed - The total number of connections closed due to max_idle.
  • max_lifetime_closed - The total number of connections closed due to max_lifetime.

Metrics

  • Counters
    • mobc_pool_connections_opened_total - Total number of Pool Connections opened
    • mobc_pool_connections_closed_total - Total number of Pool Connections closed
  • Gauges
    • mobc_pool_connections_open - Number of currently open Pool Connections
    • mobc_pool_connections_busy - Number of currently busy Pool Connections (executing a database query)"
    • mobc_pool_connections_idle - Number of currently unused Pool Connections (waiting for the next pool query to run)
    • mobc_client_queries_wait - Number of queries currently waiting for a connection
  • Histograms
    • mobc_client_queries_wait_histogram_ms - Histogram of the wait time of all queries in ms

Compatibility

Because tokio is not compatible with other runtimes, such as async-std. So a database driver written with tokio cannot run in the async-std runtime. For example, you can't use redis-rs in tide because it uses tokio, so the connection pool which bases on redis-res can't be used in tide either.

More Repositories

1

sensitive

敏感词查找,验证,过滤和替换 🤓 FindAll, Validate, Filter and Replace words.
Go
630
star
2

rust-miniproxy

手把手教你用Rust写代理, 代码已完成, 文章不定期更新
JavaScript
154
star
3

nipper

A Rust crate for manipulating HTML with CSS selectors
Rust
121
star
4

rust-ajson

Rust port of gjson,get JSON value by dotpath syntax
Rust
104
star
5

gkd-rs

A multi-connections TCP accelerator, written in Rust
Rust
26
star
6

trie-go

The golang implementation of trie tree.
Go
24
star
7

danmu.go

基于golang的命令行形式的直播网站(斗鱼)的弹幕浏览
Go
18
star
8

react-affix

A simple react affix component(Deprecated, use sticky instead)
JavaScript
15
star
9

mobc-redis

Redis support for the mobc connection pool
Rust
12
star
10

notes

My notes
10
star
11

mobc-postgres

Rust
9
star
12

neighbor

基于geohash和mysql实现的附近的人
Go
6
star
13

Posts

📫 Posts is a Python library for send mail easily.
Python
5
star
14

douyu

Crawler For DouyuTv Statistics
Python
4
star
15

yake

Pelican theme.
HTML
4
star
16

tiresias

simple demos for learning scarpy
Python
2
star
17

sparta

Douyu Statistic Backend
JavaScript
2
star
18

comethandler

🔫 helps the server which is builded with golang to push messages to the http clients easily.
Go
2
star
19

tide-demo

Rust
1
star
20

hackathon-2015

Eleme Hackathon 2015
Java
1
star
21

wisburg-terminal

JavaScript
1
star
22

flask_restful_doc

Generate api docs for flask restful resource
Python
1
star
23

bomd-server

base on markdown. a lightweight blog framework which id managed in local
CSS
1
star
24

noname

Go
1
star
25

remote-shortcuts

通过手机使用快捷键操作windows程序
Rust
1
star
26

init-react-webpack-project

A simple and small python2 script that can help you to init a react & webpack project.
Python
1
star