| Directory: | cvmfs/ |
|---|---|
| File: | cvmfs/backoff.cc |
| Date: | 2025-11-09 02:35:23 |
| 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 | 7251851 | void BackoffThrottle::Init(const unsigned init_delay_ms, | |
| 19 | const unsigned max_delay_ms, | ||
| 20 | const unsigned reset_after_ms) { | ||
| 21 | 7251851 | init_delay_ms_ = init_delay_ms; | |
| 22 | 7251851 | max_delay_ms_ = max_delay_ms; | |
| 23 | 7251851 | reset_after_ms_ = reset_after_ms; | |
| 24 | 7251851 | prng_.InitLocaltime(); | |
| 25 | |||
| 26 | 7253040 | lock_ = reinterpret_cast<pthread_mutex_t *>(smalloc(sizeof(pthread_mutex_t))); | |
| 27 | 7252315 | const int retval = pthread_mutex_init(lock_, NULL); | |
| 28 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 7252750 times.
|
7252750 | assert(retval == 0); |
| 29 | |||
| 30 | 7252750 | Reset(); | |
| 31 | 7253069 | } | |
| 32 | |||
| 33 | |||
| 34 | 7240126 | BackoffThrottle::~BackoffThrottle() { | |
| 35 | 7240126 | pthread_mutex_destroy(lock_); | |
| 36 | 7240242 | free(lock_); | |
| 37 | 7240242 | } | |
| 38 | |||
| 39 | |||
| 40 | 7253021 | void BackoffThrottle::Reset() { | |
| 41 | 7253021 | pthread_mutex_lock(lock_); | |
| 42 | 7255370 | delay_range_ = 0; | |
| 43 | 7255370 | last_throttle_ = 0; | |
| 44 | 7255370 | pthread_mutex_unlock(lock_); | |
| 45 | 7254703 | } | |
| 46 | |||
| 47 | |||
| 48 | 311 | void BackoffThrottle::Throttle() { | |
| 49 | 311 | const time_t now = time(NULL); | |
| 50 | |||
| 51 | 311 | pthread_mutex_lock(lock_); | |
| 52 |
2/2✓ Branch 0 taken 95 times.
✓ Branch 1 taken 216 times.
|
311 | if (unsigned(now - last_throttle_) < reset_after_ms_ / 1000) { |
| 53 |
1/2✓ Branch 0 taken 95 times.
✗ Branch 1 not taken.
|
95 | if (delay_range_ < max_delay_ms_) { |
| 54 |
1/2✓ Branch 0 taken 95 times.
✗ Branch 1 not taken.
|
95 | if (delay_range_ == 0) |
| 55 | 95 | delay_range_ = init_delay_ms_; | |
| 56 | else | ||
| 57 | ✗ | delay_range_ *= 2; | |
| 58 | } | ||
| 59 | 95 | unsigned delay = prng_.Next(delay_range_) + 1; | |
| 60 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 95 times.
|
95 | if (delay > max_delay_ms_) |
| 61 | ✗ | delay = max_delay_ms_; | |
| 62 | |||
| 63 | 95 | pthread_mutex_unlock(lock_); | |
| 64 | 95 | LogCvmfs(kLogCvmfs, kLogDebug, "backoff throttle %d ms", delay); | |
| 65 | 95 | SafeSleepMs(delay); | |
| 66 | 95 | pthread_mutex_lock(lock_); | |
| 67 | } | ||
| 68 | 311 | last_throttle_ = now; | |
| 69 | 311 | pthread_mutex_unlock(lock_); | |
| 70 | 311 | } | |
| 71 |