Skip to content

Commit 9e27bd7

Browse files
committed
fix(accumulator): Allow zero duration granularity
1 parent 5dc57aa commit 9e27bd7

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

src/accumulator.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,13 @@ impl UsageAccumulator {
5151
amount: u64,
5252
usage_unit: UsageUnit,
5353
) {
54-
let quantized_timestamp: DateTime<Utc> =
55-
usage_time.duration_trunc(self.granularity).unwrap();
54+
// Check for zero here because of chrono bug, which causes a panic:
55+
// https://github.com/chronotope/chrono/pull/1474
56+
let quantized_timestamp = if self.granularity.is_zero() {
57+
usage_time
58+
} else {
59+
usage_time.duration_trunc(self.granularity).unwrap()
60+
};
5661

5762
if self.first_timestamp.is_none() {
5863
self.first_timestamp = Some(quantized_timestamp);
@@ -75,9 +80,12 @@ impl UsageAccumulator {
7580
/// and at least `granularity` seconds have passed since
7681
/// the first chunk of data was added.
7782
pub fn should_flush(&self, current_time: DateTime<Utc>) -> bool {
78-
return self.first_timestamp.is_some()
79-
&& self.usage_batch.keys().len() > 0
80-
&& current_time - self.first_timestamp.unwrap() > self.granularity;
83+
let Some(first_timestamp) = self.first_timestamp else {
84+
return false;
85+
};
86+
87+
return self.usage_batch.keys().len() > 0
88+
&& current_time - first_timestamp >= self.granularity;
8189
}
8290

8391
/// Return the current bucket and clears up the state.

0 commit comments

Comments
 (0)