atomic-batcher
atomic-batcher
is a simple batching function that allows you to atomically batch a series of operations. Adapted from mafintosh/atomic-batcher
Installation
cargo add atomic-batcher
Install cargo-edit to extend Cargo, allowing you to add, remove, and upgrade dependencies by modifying your Cargo.toml file from the command line.
Usage
extern crate atomic_batcher;
extern crate tokio;
use atomic_batcher::*;
use std::sync::Arc;
use std::time::{Duration, Instant};
use tokio::prelude::*;
use tokio::timer::Delay;
fn main() {
let when = Instant::now() + Duration::from_millis(2000);
let run = move |val: Vec<u64>, _batcher: Batcher<u64>| -> () {
println!("{:?}", val);
};
let batcher = Batcher::new(Arc::new(run));
batcher.append(vec![1, 2, 3], None);
batcher.append(vec![4, 5, 6], None);
batcher.append(vec![7, 8, 9], None);
let task = Delay::new(when)
.and_then(|_| {
batcher.done(Ok(()));
Ok(())
})
.map_err(|e| panic!("delay errored; err={:?}", e));
tokio::run(task);
}
Running the above example will print
[1, 2, 3]
// two seconds later
[4, 5, 6, 7, 8, 9]
License
Licensed under the AGPL-3.0+. See LICENSE.