| Line |
Branch |
Exec |
Source |
| 1 |
|
|
/** |
| 2 |
|
|
* This file is part of the CernVM File System. |
| 3 |
|
|
*/ |
| 4 |
|
|
|
| 5 |
|
|
#ifndef CVMFS_BACKOFF_H_ |
| 6 |
|
|
#define CVMFS_BACKOFF_H_ |
| 7 |
|
|
|
| 8 |
|
|
#include <pthread.h> |
| 9 |
|
|
|
| 10 |
|
|
#include "util/prng.h" |
| 11 |
|
|
#include "util/single_copy.h" |
| 12 |
|
|
|
| 13 |
|
|
/** |
| 14 |
|
|
* When Throttle() is called in quick succession, the exponential backoff will |
| 15 |
|
|
* start (sleep). The class forgets a call to Throttle() after reset_after_ms |
| 16 |
|
|
* milliseconds. |
| 17 |
|
|
*/ |
| 18 |
|
|
class BackoffThrottle : public SingleCopy { |
| 19 |
|
|
public: |
| 20 |
|
|
static const unsigned kDefaultInitDelay = 32; /**< 32ms */ |
| 21 |
|
|
static const unsigned kDefaultMaxDelay = 2000; /**< Maximum 2 seconds */ |
| 22 |
|
|
/** |
| 23 |
|
|
* Clear memory after 10s |
| 24 |
|
|
*/ |
| 25 |
|
|
static const unsigned kDefaultResetAfter = 10000; |
| 26 |
|
|
|
| 27 |
|
2714 |
BackoffThrottle() { |
| 28 |
|
2714 |
Init(kDefaultInitDelay, kDefaultMaxDelay, kDefaultResetAfter); |
| 29 |
|
2714 |
} |
| 30 |
|
7251253 |
BackoffThrottle(const unsigned init_delay_ms, |
| 31 |
|
|
const unsigned max_delay_ms, |
| 32 |
|
7251253 |
const unsigned reset_after_ms) { |
| 33 |
|
7251079 |
Init(init_delay_ms, max_delay_ms, reset_after_ms); |
| 34 |
|
7252036 |
} |
| 35 |
|
|
~BackoffThrottle(); |
| 36 |
|
|
void Throttle(); |
| 37 |
|
|
void Reset(); |
| 38 |
|
|
|
| 39 |
|
|
private: |
| 40 |
|
|
void Init(const unsigned init_delay_ms, |
| 41 |
|
|
const unsigned max_delay_ms, |
| 42 |
|
|
const unsigned reset_after_ms); |
| 43 |
|
|
unsigned delay_range_; |
| 44 |
|
|
unsigned init_delay_ms_; |
| 45 |
|
|
unsigned max_delay_ms_; |
| 46 |
|
|
unsigned reset_after_ms_; |
| 47 |
|
|
time_t last_throttle_; |
| 48 |
|
|
Prng prng_; |
| 49 |
|
|
pthread_mutex_t *lock_; |
| 50 |
|
|
}; |
| 51 |
|
|
|
| 52 |
|
|
#endif // CVMFS_BACKOFF_H_ |
| 53 |
|
|
|