buffer-trigger 0.1.0

A trigger for data collection based on quantity and time.
Documentation

Introduction

buffer-trigger 一个基于数量和时间的数据收集触发器

使用场景:

  • 聚合日志,定时定量输出
  • 聚合 MQ 的大量数据,合并处理
  • 大量更新请求时,可以先更新 cache,再合并刷 db
  • ...所有需要聚合、节流等操作都可以用

Basic usage

see tests

#[macro_use]
extern crate lazy_static;
use buffer_trigger::{BufferTrigger, SimpleBufferTrigger, SimpleBufferTriggerBuilder};
use log::LevelFilter;
use std::{sync::Mutex, thread, time::Duration};

lazy_static! {
    static ref BUFFER_TRIGGER: Mutex<SimpleBufferTrigger<i32, Vec<i32>>> = Mutex::new(
        SimpleBufferTriggerBuilder::<i32, Vec<i32>>::builder()
            .name("test".to_owned())
            .default_container(Vec::default)
            .accumulator(|c, e| c.push(e))
            .consumer(|c| println!("{:?}", c))
            .max_len(2)
            .interval(Duration::from_millis(500))
            .build()
    );
}
#[test]
fn it_works() {
    let _ = env_logger::builder()
        .is_test(true)
        .filter_level(LevelFilter::Debug)
        .try_init();

    thread::spawn(|| {
        BUFFER_TRIGGER.lock().unwrap().listen_clock_trigger();
    });

    BUFFER_TRIGGER.lock().unwrap().push(1);
    BUFFER_TRIGGER.lock().unwrap().push(2);
    BUFFER_TRIGGER.lock().unwrap().push(3);
    BUFFER_TRIGGER.lock().unwrap().push(4);
    BUFFER_TRIGGER.lock().unwrap().push(5);

    thread::sleep(Duration::from_secs(5));
}

output:

[1, 2]
[3, 4]
[5]

Features

This project is still under development. The following features with the check marks are supported.

If you are concerned about an unimplemented feature, please tell me and I will finish writing it ASAP.

  • 根据数量定时触发
  • 根据延迟定时触发(每个元素最多在容器中保存的时长)
  • 多种类型容器
    • 本地容器存储
    • 远程容器存储(redis)
  • 可以指定 runtime 的异步版本
    • async-std
    • tokio

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions