GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/backoff.cc
Date: 2026-02-22 02:35:58
Exec Total Coverage
Lines: 36 38 94.7%
Branches: 6 10 60.0%

Line Branch Exec Source
1 /**
2 * This file is part of the CernVM File System.
3 *
4 * Exponential backoff (sleep) with cutoff.
5 */
6
7
8 #include "backoff.h"
9
10 #include <ctime>
11
12 #include "util/logging.h"
13 #include "util/posix.h"
14 #include "util/smalloc.h"
15
16 using namespace std; // NOLINT
17
18 6752488 void BackoffThrottle::Init(const unsigned init_delay_ms,
19 const unsigned max_delay_ms,
20 const unsigned reset_after_ms) {
21 6752488 init_delay_ms_ = init_delay_ms;
22 6752488 max_delay_ms_ = max_delay_ms;
23 6752488 reset_after_ms_ = reset_after_ms;
24 6752488 prng_.InitLocaltime();
25
26 6753028 lock_ = reinterpret_cast<pthread_mutex_t *>(smalloc(sizeof(pthread_mutex_t)));
27 6752974 const int retval = pthread_mutex_init(lock_, NULL);
28
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6753190 times.
6753190 assert(retval == 0);
29
30 6753190 Reset();
31 6754540 }
32
33
34 6741625 BackoffThrottle::~BackoffThrottle() {
35 6741625 pthread_mutex_destroy(lock_);
36 6741733 free(lock_);
37 6741733 }
38
39
40 6753599 void BackoffThrottle::Reset() {
41 6753599 pthread_mutex_lock(lock_);
42 6755003 delay_range_ = 0;
43 6755003 last_throttle_ = 0;
44 6755003 pthread_mutex_unlock(lock_);
45 6755030 }
46
47
48 204 void BackoffThrottle::Throttle() {
49 204 const time_t now = time(NULL);
50
51 204 pthread_mutex_lock(lock_);
52
2/2
✓ Branch 0 taken 73 times.
✓ Branch 1 taken 131 times.
204 if (unsigned(now - last_throttle_) < reset_after_ms_ / 1000) {
53
1/2
✓ Branch 0 taken 73 times.
✗ Branch 1 not taken.
73 if (delay_range_ < max_delay_ms_) {
54
1/2
✓ Branch 0 taken 73 times.
✗ Branch 1 not taken.
73 if (delay_range_ == 0)
55 73 delay_range_ = init_delay_ms_;
56 else
57 delay_range_ *= 2;
58 }
59 73 unsigned delay = prng_.Next(delay_range_) + 1;
60
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 73 times.
73 if (delay > max_delay_ms_)
61 delay = max_delay_ms_;
62
63 73 pthread_mutex_unlock(lock_);
64 73 LogCvmfs(kLogCvmfs, kLogDebug, "backoff throttle %d ms", delay);
65 73 SafeSleepMs(delay);
66 73 pthread_mutex_lock(lock_);
67 }
68 204 last_throttle_ = now;
69 204 pthread_mutex_unlock(lock_);
70 204 }
71