6 releases (3 breaking)

0.7.0 Dec 26, 2020
0.6.2 Nov 4, 2020
0.6.0 Jul 14, 2020
0.5.0 Mar 13, 2020
0.0.0 Dec 3, 2019

#36 in Asynchronous

Download history 788/week @ 2020-11-07 620/week @ 2020-11-14 351/week @ 2020-11-21 502/week @ 2020-11-28 464/week @ 2020-12-05 405/week @ 2020-12-12 390/week @ 2020-12-19 214/week @ 2020-12-26 808/week @ 2021-01-02 430/week @ 2021-01-09 392/week @ 2021-01-16 282/week @ 2021-01-23 570/week @ 2021-01-30 323/week @ 2021-02-06 519/week @ 2021-02-13 378/week @ 2021-02-20

3,070 downloads per month

MIT/Apache

55KB
692 lines

Deadpool for Lapin Latest Version

Deadpool is a dead simple async pool for connections and objects of any type.

This crate implements a deadpool manager for lapin.

Features

Feature Description Extra dependencies Default
config Enable support for config crate config, serde/derive yes

Example with tokio-amqp crate

use std::sync::Arc;

use deadpool_lapin::{Config, Manager, Pool };
use deadpool_lapin::lapin::{
    options::BasicPublishOptions,
    BasicProperties
};
use tokio::runtime::Runtime;
use tokio_amqp::LapinTokioExt;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let mut cfg = Config::default();
    cfg.url = Some("amqp://127.0.0.1:5672/%2f".to_string());
    cfg.connection_properties = lapin::ConnectionProperties::default()
            .with_tokio();
    let pool = cfg.create_pool();
    for i in 1..10usize {
        let mut connection = pool.get().await?;
        let channel = connection.create_channel().await?;
        channel.basic_publish(
            "",
            "hello",
            BasicPublishOptions::default(),
            b"hello from deadpool".to_vec(),
            BasicProperties::default()
        ).await?;
    }
    Ok(())
}

Example with config, dotenv and tokio-amqp crate

use std::sync::Arc;

use deadpool_lapin::lapin::{
    options::BasicPublishOptions,
    BasicProperties
};
use dotenv::dotenv;
use serde::Deserialize;
use tokio_amqp::LapinTokioExt;

#[derive(Debug, Deserialize)]
struct Config {
    #[serde(default)]
    amqp: deadpool_lapin::Config
}

impl Config {
    pub fn from_env() -> Result<Self, ::config_crate::ConfigError> {
        let mut cfg = ::config_crate::Config::new();
        cfg.merge(::config_crate::Environment::new().separator("__"))?;
        cfg.try_into()
    }
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    dotenv().ok();
    let mut cfg = Config::from_env().unwrap();
    cfg.amqp.connection_properties = lapin::ConnectionProperties::default()
            .with_tokio();
    let pool = cfg.amqp.create_pool();
    for i in 1..10usize {
        let mut connection = pool.get().await?;
        let channel = connection.create_channel().await?;
        channel.basic_publish(
            "",
            "hello",
            BasicPublishOptions::default(),
            b"hello from deadpool".to_vec(),
            BasicProperties::default()
        ).await?;
    }
    Ok(())
}

License

Licensed under either of

at your option.

Dependencies

~10MB
~216K SLoC