universal-inserter
A runtime-agnostic Rust library implementing the batch inserter pattern (similar to ClickHouse's Inserter). Buffers items and flushes them via a user-provided async insert function based on configurable limits.
Features
- Runtime-agnostic: Works with Tokio, async-std, smol, or any async runtime
- Row limit: Flush when buffer reaches N items
- Time period: Flush after duration elapsed
- Period bias: Optional randomization to prevent synchronized flushes
- Zero dependencies by default (only
randfor period_bias feature)
Installation
[]
= "0.1"
# Optional: enable period bias randomization
= { = "0.1", = ["period_bias"] }
Usage
Basic Example
use Inserter;
use Duration;
async
With Period Bias
use Inserter;
use Duration;
let mut inserter = new
.with_max_rows
.with_period
.with_period_bias; // ±20% randomization
With Commit Callback
use Inserter;
let mut inserter = new
.with_max_rows
.with_commit_callback;
Force Commit
// Flush unconditionally, regardless of limits
let stats = inserter.force_commit.await?;
Check Pending Stats
let pending = inserter.pending;
println!;
if let Some = inserter.time_left
API
Inserter Methods
| Method | Description |
|---|---|
new(insert_fn) |
Create inserter with async insert function |
with_max_rows(n) |
Set row limit (default: unlimited) |
with_period(duration) |
Set time-based flush interval |
with_period_bias(bias) |
Add randomization ±bias (requires period_bias feature) |
with_commit_callback(fn) |
Register callback after successful commits |
write(item) |
Add item to buffer (clones item) |
write_owned(item) |
Add item to buffer (moves item) |
commit() |
Check limits and flush if reached |
force_commit() |
Flush unconditionally |
end() |
Consume inserter and flush remaining |
pending() |
Get current buffer statistics |
time_left() |
Duration until next period tick |
License
MIT