use crate::client;
use std::time::{SystemTime, UNIX_EPOCH};
use super::{Partitioner, Topics};
pub struct UniformPartitioner;
impl Default for UniformPartitioner {
fn default() -> Self {
Self
}
}
impl UniformPartitioner {
#[must_use]
pub fn new() -> Self {
Self
}
}
impl Partitioner for UniformPartitioner {
fn partition(&mut self, topics: Topics<'_>, rec: &mut client::ProduceMessage<'_, '_>) {
if rec.partition >= 0 {
return;
}
let Some(partitions) = topics.partitions(rec.topic) else {
return;
};
let avail = partitions.available_ids();
if avail.is_empty() {
return;
}
let seed = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_nanos() as usize;
let idx = seed % avail.len();
rec.partition = avail[idx];
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_uniform_default() {
let _ = UniformPartitioner;
}
#[test]
fn test_uniform_new() {
let _ = UniformPartitioner::new();
}
}