GithubHelp home page GithubHelp logo

baitcenter / mobc Goto Github PK

View Code? Open in Web Editor NEW

This project forked from importcjj/mobc

0.0 1.0 0.0 266 KB

A generic connection pool for Rust with async/await support

Home Page: https://docs.rs/mobc

License: Apache License 2.0

Rust 100.00%

mobc's Introduction

mobc

A generic connection pool with async/await support.

Inspired by r2d2 and Golang SQL package.

Build Status crates.io

Documentation

Changelog

Note: mobc requires at least Rust 1.39.

Usage

[dependencies]
mobc = "0.5"

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

Features

  • Support async/.await syntax
  • Support both tokio and async-std
  • High performance
  • Easy to customize
  • Dynamic configuration
Backend Adaptor Crate
tokio-postgres mobc-postgres
redis mobc-redis

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.

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.

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)
    }
}

mobc's People

Contributors

importcjj avatar jrb0001 avatar pimeys avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.