File tree Expand file tree Collapse file tree 1 file changed +13
-5
lines changed Expand file tree Collapse file tree 1 file changed +13
-5
lines changed Original file line number Diff line number Diff line change @@ -51,8 +51,13 @@ impl UsageAccumulator {
51
51
amount : u64 ,
52
52
usage_unit : UsageUnit ,
53
53
) {
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
+ } ;
56
61
57
62
if self . first_timestamp . is_none ( ) {
58
63
self . first_timestamp = Some ( quantized_timestamp) ;
@@ -75,9 +80,12 @@ impl UsageAccumulator {
75
80
/// and at least `granularity` seconds have passed since
76
81
/// the first chunk of data was added.
77
82
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 ;
81
89
}
82
90
83
91
/// Return the current bucket and clears up the state.
You can’t perform that action at this time.
0 commit comments