Skip to content

Commit 2f0bdec

Browse files
committed
wip
1 parent 739c8ea commit 2f0bdec

File tree

3 files changed

+38
-36
lines changed

3 files changed

+38
-36
lines changed

tap-agent/src/backoff.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
use std::time::{Duration, Instant};
2+
3+
#[derive(Debug, Clone)]
4+
pub struct BackoffInfo {
5+
failed_count: u32,
6+
failed_backoff_time: Instant,
7+
}
8+
9+
impl BackoffInfo {
10+
pub fn ok(&mut self) {
11+
self.failed_count = 0;
12+
}
13+
14+
pub fn fail(&mut self) {
15+
// backoff = max(100ms * 2 ^ retries, 60s)
16+
self.failed_backoff_time = Instant::now()
17+
+ (Duration::from_millis(100) * 2u32.pow(self.failed_count))
18+
.min(Duration::from_secs(60));
19+
self.failed_count += 1;
20+
}
21+
22+
pub fn in_backoff(&self) -> bool {
23+
let now = Instant::now();
24+
now < self.failed_backoff_time
25+
}
26+
}
27+
28+
impl Default for BackoffInfo {
29+
fn default() -> Self {
30+
Self {
31+
failed_count: 0,
32+
failed_backoff_time: Instant::now(),
33+
}
34+
}
35+
}

tap-agent/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ lazy_static! {
1616
}
1717

1818
pub mod agent;
19+
pub mod backoff;
1920
pub mod config;
2021
pub mod database;
2122
pub mod metrics;

tap-agent/src/tracker/sender_fee_stats.rs

Lines changed: 2 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33

44
use std::{
55
collections::VecDeque,
6-
time::{Duration, Instant, SystemTime, UNIX_EPOCH},
6+
time::{Duration, SystemTime, UNIX_EPOCH},
77
};
88

9-
use crate::agent::unaggregated_receipts::UnaggregatedReceipts;
9+
use crate::{agent::unaggregated_receipts::UnaggregatedReceipts, backoff::BackoffInfo};
1010

1111
use super::{AllocationStats, DefaultFromExtra, DurationInfo};
1212

@@ -84,31 +84,6 @@ impl BufferInfo {
8484
}
8585
}
8686

87-
#[derive(Debug, Clone)]
88-
pub struct BackoffInfo {
89-
failed_ravs_count: u32,
90-
failed_rav_backoff_time: Instant,
91-
}
92-
93-
impl BackoffInfo {
94-
pub fn ok(&mut self) {
95-
self.failed_ravs_count = 0;
96-
}
97-
98-
pub fn fail(&mut self) {
99-
// backoff = max(100ms * 2 ^ retries, 60s)
100-
self.failed_rav_backoff_time = Instant::now()
101-
+ (Duration::from_millis(100) * 2u32.pow(self.failed_ravs_count))
102-
.min(Duration::from_secs(60));
103-
self.failed_ravs_count += 1;
104-
}
105-
106-
pub fn in_backoff(&self) -> bool {
107-
let now = Instant::now();
108-
now < self.failed_rav_backoff_time
109-
}
110-
}
111-
11287
impl DefaultFromExtra<DurationInfo> for SenderFeeStats {
11388
fn default_from_extra(extra: &DurationInfo) -> Self {
11489
SenderFeeStats {
@@ -121,15 +96,6 @@ impl DefaultFromExtra<DurationInfo> for SenderFeeStats {
12196
}
12297
}
12398

124-
impl Default for BackoffInfo {
125-
fn default() -> Self {
126-
Self {
127-
failed_ravs_count: 0,
128-
failed_rav_backoff_time: Instant::now(),
129-
}
130-
}
131-
}
132-
13399
impl AllocationStats<UnaggregatedReceipts> for SenderFeeStats {
134100
fn update(&mut self, v: UnaggregatedReceipts) {
135101
self.total_fee = v.value;

0 commit comments

Comments
 (0)