zimq 0.1.2

Tokio-based asynchronous in-process message queue.
Documentation
  • Coverage
  • 100%
    1 out of 1 items documented0 out of 0 items with examples
  • Size
  • Source code size: 23.0 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 218.7 kB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 23s Average build duration of successful builds.
  • all releases: 25s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • cuspate/zimq
    1 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • lzfvvv

ZIMQ

Table of Contents

Quick-pitch

Tokio-based asynchronous in-process message queue. This library prioritizes simplicity and ease of use. Future development will focus solely on bug fixes and dependency updates, with no new features planned.

Features

  • Lightweight, high‑performance, simple to use, and in‑process.
  • Supports both immediate and delayed messages.
  • Supports custom IDs for delayed messages. If a message with the same ID has not yet been consumed, subsequent attempts to send a message with that ID will immediately return an error.
  • Automatically reloads unconsumed delayed messages after an application restart.
  • Supports graceful shutdown, waiting for all immediate or matured delayed messages in the queue to be processed before exiting.

Quick-start

Cargo Install

cargo add zimq

Basic Example

use std::collections::HashMap;
use std::time::Duration;
use zimq::message::Message;

#[tokio::main]
async fn main() {
    // Specify the directory for delayed message data on initialization to allow recovery after restart.
    // Absolute and relative paths are supported; relative paths are relative to the working directory.
    zimq::init("zimq").await;
    zimq::subscribe("user", user_consume).await;
    zimq::start().await;

    let mut data = HashMap::new();
    data.insert("id".to_string(), "123456".to_string());

    // Immediate message
    if let Err(err) = zimq::send_immediate("user", data.clone()).await {
        println!("{}", err);
    }
    // Delayed message
    if let Err(err) = zimq::send_delay("user", data.clone(), Duration::from_secs(5)).await {
        println!("{}", err);
    }
    // Delayed messages with custom msgid
    let msgid = "msgid123".to_string();
    for _ in 0..2 {
        // Sending multiple delayed messages with the same ID here; only the first one will succeed.
        if let Err(err) = zimq::send_delay_ex("user", msgid.clone(), data.clone(), Duration::from_secs(10)).await {
            println!("{}", err);
        }
    }
    // After `cleanup` is called, it first interrupts the asynchronous task that checks for expired delayed messages.
    // Sleeping for 11 seconds here allows any remaining delayed messages to expire and be moved to the execution queue.
    // Additionally, it only waits for messages that have already entered the execution queue (immediate and expired messages) to finish processing before exiting.
    tokio::time::sleep(Duration::from_secs(11)).await;
    zimq::cleanup().await;
}

async fn user_consume(msg: Message) {
    println!("msg: {:?}", msg);
    if msg.msgid == "msgid123".to_string() {
        // Sleep for 5 seconds for the custom msgid message to verify that cleanup() waits for messages already in the execution queue to finish before exiting.
        tokio::time::sleep(Duration::from_secs(5)).await;
    }
}