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