Struct segment::AutoBatcher
source · pub struct AutoBatcher { /* private fields */ }
Expand description
A batcher can accept messages into an internal buffer, and report when messages must be flushed.
The recommended usage pattern looks something like this:
use segment::{AutoBatcher, Batcher, HttpClient};
use segment::message::{BatchMessage, Track, User};
use serde_json::json;
let client = HttpClient::default();
let batcher= Batcher::new(None);
let mut batcher = AutoBatcher::new(client, batcher, "your_write_key".to_string());
for i in 0..100 {
let msg = Track {
user: User::UserId { user_id: format!("user-{}", i) },
event: "Example".to_owned(),
properties: json!({ "foo": "bar" }),
..Default::default()
};
batcher.push(msg); // .await
}
Batcher will attempt to fit messages into maximally-sized batches, thus reducing the number of round trips required with Segment’s tracking API. However, if you produce messages infrequently, this may significantly delay the sending of messages to Segment.
If this delay is a concern, it is recommended that you periodically flush the batcher on your own by calling Self::flush.
Implementations§
source§impl AutoBatcher
impl AutoBatcher
sourcepub fn new(client: HttpClient, batcher: Batcher, key: String) -> Self
pub fn new(client: HttpClient, batcher: Batcher, key: String) -> Self
Construct a new, empty batcher.
use segment::{AutoBatcher, Batcher, HttpClient};
let client = HttpClient::default();
let batcher = Batcher::new(None);
let mut batcher = AutoBatcher::new(client, batcher, "your_write_key".to_string());
sourcepub async fn push(&mut self, msg: impl Into<BatchMessage>) -> Result<()>
pub async fn push(&mut self, msg: impl Into<BatchMessage>) -> Result<()>
Push a message into the batcher. If the batcher is full, send it and create a new batcher with the message.
Returns an error if the message is too large to be sent to Segment’s API.
use serde_json::json;
use segment::{AutoBatcher, Batcher, HttpClient};
use segment::message::{BatchMessage, Track, User};
let client = HttpClient::default();
let batcher = Batcher::new(None);
let mut batcher = AutoBatcher::new(client, batcher, "your_write_key".to_string());
let msg = BatchMessage::Track(Track {
user: User::UserId { user_id: String::from("user") },
event: "Example".to_owned(),
properties: json!({ "foo": "bar" }),
..Default::default()
});
batcher.push(msg); // .await
sourcepub async fn flush(&mut self) -> Result<()>
pub async fn flush(&mut self) -> Result<()>
Send all the message currently contained in the batcher, full or empty.
Returns an error if the message is too large to be sent to Segment’s API.
use serde_json::json;
use segment::{AutoBatcher, Batcher, HttpClient};
use segment::message::{BatchMessage, Track, User};
let client = HttpClient::default();
let batcher = Batcher::new(None);
let mut batcher = AutoBatcher::new(client, batcher, "your_write_key".to_string());
let msg = BatchMessage::Track(Track {
user: User::UserId { user_id: String::from("user") },
event: "Example".to_owned(),
properties: json!({ "foo": "bar" }),
..Default::default()
});
batcher.push(msg); // .await
batcher.flush(); // .await
Trait Implementations§
source§impl Clone for AutoBatcher
impl Clone for AutoBatcher
source§fn clone(&self) -> AutoBatcher
fn clone(&self) -> AutoBatcher
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moreAuto Trait Implementations§
impl Freeze for AutoBatcher
impl !RefUnwindSafe for AutoBatcher
impl Send for AutoBatcher
impl Sync for AutoBatcher
impl Unpin for AutoBatcher
impl !UnwindSafe for AutoBatcher
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§default unsafe fn clone_to_uninit(&self, dst: *mut T)
default unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)