File tree Expand file tree Collapse file tree 2 files changed +57
-0
lines changed
main/java/io/opentelemetry/opamp/client/internal/request/delay
test/java/io/opentelemetry/opamp/client/internal/request/delay Expand file tree Collapse file tree 2 files changed +57
-0
lines changed Original file line number Diff line number Diff line change 1+ package io .opentelemetry .opamp .client .internal .request .delay ;
2+
3+ import java .time .Duration ;
4+ import javax .annotation .concurrent .GuardedBy ;
5+
6+ public final class ExponentialBackoffPeriodicDelay implements PeriodicDelay {
7+ private final Duration initialDelay ;
8+ private final Object delayNanosLock = new Object ();
9+
10+ @ GuardedBy ("delayNanosLock" )
11+ private long delayNanos ;
12+
13+ public ExponentialBackoffPeriodicDelay (Duration initialDelay ) {
14+ this .initialDelay = initialDelay ;
15+ delayNanos = initialDelay .toNanos ();
16+ }
17+
18+ @ Override
19+ public Duration getNextDelay () {
20+ synchronized (delayNanosLock ) {
21+ long previousValue = delayNanos ;
22+ delayNanos = delayNanos * 2 ;
23+ return Duration .ofNanos (previousValue );
24+ }
25+ }
26+
27+ @ Override
28+ public void reset () {
29+ synchronized (delayNanosLock ) {
30+ delayNanos = initialDelay .toNanos ();
31+ }
32+ }
33+ }
Original file line number Diff line number Diff line change 1+ package io .opentelemetry .opamp .client .internal .request .delay ;
2+
3+ import static org .assertj .core .api .Assertions .assertThat ;
4+
5+ import java .time .Duration ;
6+ import org .junit .jupiter .api .Test ;
7+
8+ class ExponentialBackoffPeriodicDelayTest {
9+ @ Test
10+ void verifyDelayUpdates () {
11+ ExponentialBackoffPeriodicDelay delay =
12+ new ExponentialBackoffPeriodicDelay (Duration .ofSeconds (1 ));
13+
14+ assertThat (delay .getNextDelay ()).isEqualTo (Duration .ofSeconds (1 ));
15+ assertThat (delay .getNextDelay ()).isEqualTo (Duration .ofSeconds (2 ));
16+ assertThat (delay .getNextDelay ()).isEqualTo (Duration .ofSeconds (4 ));
17+ assertThat (delay .getNextDelay ()).isEqualTo (Duration .ofSeconds (8 ));
18+ assertThat (delay .getNextDelay ()).isEqualTo (Duration .ofSeconds (16 ));
19+
20+ // Reset
21+ delay .reset ();
22+ assertThat (delay .getNextDelay ()).isEqualTo (Duration .ofSeconds (1 ));
23+ }
24+ }
You can’t perform that action at this time.
0 commit comments