| Directory: | cvmfs/ |
|---|---|
| File: | cvmfs/backoff.cc |
| Date: | 2026-04-05 02:35:23 |
| Exec | Total | Coverage | |
|---|---|---|---|
| Lines: | 38 | 38 | 100.0% |
| Branches: | 9 | 10 | 90.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 | 1003600 | void BackoffThrottle::Init(const unsigned init_delay_ms, | |
| 19 | const unsigned max_delay_ms, | ||
| 20 | const unsigned reset_after_ms) { | ||
| 21 | 1003600 | init_delay_ms_ = init_delay_ms; | |
| 22 | 1003600 | max_delay_ms_ = max_delay_ms; | |
| 23 | 1003600 | reset_after_ms_ = reset_after_ms; | |
| 24 | 1003600 | prng_.InitLocaltime(); | |
| 25 | |||
| 26 | 1003652 | lock_ = reinterpret_cast<pthread_mutex_t *>(smalloc(sizeof(pthread_mutex_t))); | |
| 27 | 1003556 | const int retval = pthread_mutex_init(lock_, NULL); | |
| 28 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1003664 times.
|
1003664 | assert(retval == 0); |
| 29 | |||
| 30 | 1003664 | Reset(); | |
| 31 | 1003660 | } | |
| 32 | |||
| 33 | |||
| 34 | 1001509 | BackoffThrottle::~BackoffThrottle() { | |
| 35 | 1001509 | pthread_mutex_destroy(lock_); | |
| 36 | 1001529 | free(lock_); | |
| 37 | 1001529 | } | |
| 38 | |||
| 39 | |||
| 40 | 1003652 | void BackoffThrottle::Reset() { | |
| 41 | 1003652 | pthread_mutex_lock(lock_); | |
| 42 | 1004060 | delay_range_ = 0; | |
| 43 | 1004060 | last_throttle_ = 0; | |
| 44 | 1004060 | pthread_mutex_unlock(lock_); | |
| 45 | 1003864 | } | |
| 46 | |||
| 47 | |||
| 48 | 145 | void BackoffThrottle::Throttle() { | |
| 49 | 145 | const time_t now = time(NULL); | |
| 50 | |||
| 51 | 145 | pthread_mutex_lock(lock_); | |
| 52 |
2/2✓ Branch 0 taken 58 times.
✓ Branch 1 taken 87 times.
|
145 | if (unsigned(now - last_throttle_) < reset_after_ms_ / 1000) { |
| 53 |
2/2✓ Branch 0 taken 55 times.
✓ Branch 1 taken 3 times.
|
58 | if (delay_range_ < max_delay_ms_) { |
| 54 |
2/2✓ Branch 0 taken 43 times.
✓ Branch 1 taken 12 times.
|
55 | if (delay_range_ == 0) |
| 55 | 43 | delay_range_ = init_delay_ms_; | |
| 56 | else | ||
| 57 | 12 | delay_range_ *= 2; | |
| 58 | } | ||
| 59 | 58 | unsigned delay = prng_.Next(delay_range_) + 1; | |
| 60 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 55 times.
|
58 | if (delay > max_delay_ms_) |
| 61 | 3 | delay = max_delay_ms_; | |
| 62 | |||
| 63 | 58 | pthread_mutex_unlock(lock_); | |
| 64 | 58 | LogCvmfs(kLogCvmfs, kLogDebug, "backoff throttle %d ms", delay); | |
| 65 | 58 | SafeSleepMs(delay); | |
| 66 | 58 | pthread_mutex_lock(lock_); | |
| 67 | } | ||
| 68 | 145 | last_throttle_ = now; | |
| 69 | 145 | pthread_mutex_unlock(lock_); | |
| 70 | 145 | } | |
| 71 |