Skip to content

Commit

Permalink
fix: Flakey submission
Browse files Browse the repository at this point in the history
  • Loading branch information
jan-auer committed Dec 11, 2023
1 parent b515db3 commit 1267668
Showing 1 changed file with 12 additions and 14 deletions.
26 changes: 12 additions & 14 deletions sentry-core/src/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -408,25 +408,22 @@ impl MetricAggregator {
}

fn worker_thread(inner: Arc<Mutex<AggregatorInner>>, transport: TransportArc) {
loop {
let mut guard = inner.lock().unwrap();
let should_stop = !guard.running;
let mut running = true;

let last_flush = Instant::now();
let buckets = guard.take_buckets();
drop(guard);
while running {
// Park instead of sleep so we can wake the thread up. Do not account for delays during
// flushing, since we benefit from some drift to spread out metric submissions.
thread::park_timeout(FLUSH_INTERVAL);

let buckets = {
let mut guard = inner.lock().unwrap();
running = guard.running;
guard.take_buckets()
};

if !buckets.is_empty() {
Self::flush_buckets(buckets, &transport);
}

if should_stop {
break;
}

// Park instead of sleep so we can wake the thread up
let park_time = FLUSH_INTERVAL.saturating_sub(last_flush.elapsed());
thread::park_timeout(park_time);
}
}

Expand Down Expand Up @@ -499,6 +496,7 @@ impl Drop for MetricAggregator {
fn drop(&mut self) {
self.inner.lock().unwrap().running = false;
if let Some(handle) = self.handle.take() {
handle.thread().unpark();
handle.join().unwrap();
}
}
Expand Down

0 comments on commit 1267668

Please sign in to comment.