Skip to content

Commit e0ec3bc

Browse files
authored
fix: setting last_flush to 0 makes us flush on the first invocation (#843)
Setting the last flush time to 0 causes the logic for continuous flushing to always evaluate to `flush` on the first invocation. This change fixes that, unless there is a proactive initialization or some other out-of-band cause.
1 parent ae3e674 commit e0ec3bc

1 file changed

Lines changed: 13 additions & 1 deletion

File tree

bottlecap/src/lifecycle/flush_control.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,13 @@ pub enum FlushDecision {
2727
impl FlushControl {
2828
#[must_use]
2929
pub fn new(flush_strategy: FlushStrategy, flush_timeout: u64) -> FlushControl {
30+
let now = time::SystemTime::now()
31+
.duration_since(time::UNIX_EPOCH)
32+
.expect("unable to poll clock, unrecoverable")
33+
.as_secs();
3034
FlushControl {
3135
flush_strategy,
32-
last_flush: 0,
36+
last_flush: now,
3337
invocation_times: InvocationTimes::new(),
3438
flush_timeout,
3539
}
@@ -126,6 +130,8 @@ mod tests {
126130
FlushStrategy::EndPeriodically(PeriodicStrategy { interval: 1 }),
127131
60,
128132
);
133+
// Set last_flush to an older time to trigger flush
134+
flush_control.last_flush -= 2;
129135
assert_eq!(flush_control.evaluate_flush_decision(), FlushDecision::End);
130136

131137
let mut flush_control = FlushControl::new(FlushStrategy::End, 60);
@@ -135,6 +141,8 @@ mod tests {
135141
FlushStrategy::Periodically(PeriodicStrategy { interval: 1 }),
136142
60,
137143
);
144+
// Set last_flush to an older time to trigger flush
145+
flush_control.last_flush -= 2;
138146
assert_eq!(
139147
flush_control.evaluate_flush_decision(),
140148
FlushDecision::Periodic
@@ -154,6 +162,8 @@ mod tests {
154162
for _ in 0..LOOKBACK_COUNT - 1 {
155163
assert_eq!(flush_control.evaluate_flush_decision(), FlushDecision::End);
156164
}
165+
// Set last_flush to an older time to trigger flush after lookback threshold
166+
flush_control.last_flush -= 61;
157167
assert_eq!(
158168
flush_control.evaluate_flush_decision(),
159169
FlushDecision::Continuous
@@ -166,6 +176,8 @@ mod tests {
166176
FlushStrategy::Periodically(PeriodicStrategy { interval: 1 }),
167177
60,
168178
);
179+
// Set last_flush to an older time to trigger flush
180+
flush_control.last_flush -= 2;
169181
assert_eq!(
170182
flush_control.evaluate_flush_decision(),
171183
FlushDecision::Periodic,

0 commit comments

Comments
 (0)