-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackoff.c
41 lines (30 loc) · 1.47 KB
/
backoff.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// Copyright (c) 2021 Bjørn Brodtkorb
#include "backoff.h"
#include "random.h"
//--------------------------------------------------------------------------------------------------
void backoff_init(Backoff* backoff, Time start_timeout, Time max_timeout, int jitter_fraction) {
backoff->max_timeout = max_timeout;
backoff->start_timeout = start_timeout;
backoff->jitter_fraction = jitter_fraction;
backoff_reset(backoff);
}
//--------------------------------------------------------------------------------------------------
bool backoff_timeout(Backoff* backoff) {
return backoff->count == 0 || get_elapsed(backoff->time, get_time()) > backoff->timeout_with_jitter;
}
//--------------------------------------------------------------------------------------------------
void next_backoff(Backoff* backoff) {
backoff->time = get_time();
backoff->count++;
// @Hack for making the user interface a bit simpler. We want the retransmission to fire immediately
// the first time. After the first time, the timeout is used.
if (backoff->count) {
backoff->timeout = limit(backoff->timeout * 2, backoff->max_timeout);
backoff->timeout_with_jitter = backoff->timeout + (s32)random() % (backoff->timeout / backoff->jitter_fraction);
}
}
//--------------------------------------------------------------------------------------------------
void backoff_reset(Backoff* backoff) {
backoff->count = 0;
backoff->timeout = backoff->start_timeout;
}