Skip to content

Commit

Permalink
Add push_many which optimises adding multiple events (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
hpeebles authored Feb 13, 2024
1 parent a354867 commit dd239bb
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
18 changes: 17 additions & 1 deletion client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ impl<R: Runtime + Send + 'static> ClientBuilder<R> {
}

impl<R: Runtime + Send + 'static> Client<R> {
pub fn push_event(&mut self, event: Event) {
pub fn push(&mut self, event: Event) {
let mut guard = self.inner.try_lock().unwrap();
let idempotency_key = guard.runtime.rng();
guard.events.push(IdempotentEvent {
Expand All @@ -158,6 +158,22 @@ impl<R: Runtime + Send + 'static> Client<R> {
self.process_events(guard, true);
}

pub fn push_many(&mut self, events: impl Iterator<Item = Event>) {
let mut guard = self.inner.try_lock().unwrap();
for event in events {
let idempotency_key = guard.runtime.rng();
guard.events.push(IdempotentEvent {
idempotency_key,
name: event.name,
timestamp: event.timestamp,
user: event.user,
source: event.source,
payload: event.payload,
});
}
self.process_events(guard, true);
}

pub fn flush_batch(&self) {
let guard = self.inner.try_lock().unwrap();
self.flush_batch_within_lock(guard);
Expand Down
4 changes: 2 additions & 2 deletions client/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ fn batch_flushed_when_max_batch_size_reached(flush_synchronously: bool) {

for i in 0..10 {
for _ in 0..5 {
client.push_event(Event {
client.push(Event {
name: i.to_string(),
timestamp: 0,
user: None,
Expand All @@ -41,7 +41,7 @@ fn batch_flushed_when_flush_delay_reached(flush_synchronously: bool) {

for i in 0..10 {
for _ in 0..5 {
client.push_event(Event {
client.push(Event {
name: i.to_string(),
timestamp: 0,
user: None,
Expand Down

0 comments on commit dd239bb

Please sign in to comment.