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 |
|
81 |
BackoffThrottle() { |
28 |
|
81 |
Init(kDefaultInitDelay, kDefaultMaxDelay, kDefaultResetAfter); |
29 |
|
81 |
} |
30 |
|
250053 |
BackoffThrottle(const unsigned init_delay_ms, |
31 |
|
|
const unsigned max_delay_ms, |
32 |
|
|
const unsigned reset_after_ms) |
33 |
|
250053 |
{ |
34 |
|
250024 |
Init(init_delay_ms, max_delay_ms, reset_after_ms); |
35 |
|
250055 |
} |
36 |
|
|
~BackoffThrottle(); |
37 |
|
|
void Throttle(); |
38 |
|
|
void Reset(); |
39 |
|
|
|
40 |
|
|
private: |
41 |
|
|
void Init(const unsigned init_delay_ms, |
42 |
|
|
const unsigned max_delay_ms, |
43 |
|
|
const unsigned reset_after_ms); |
44 |
|
|
unsigned delay_range_; |
45 |
|
|
unsigned init_delay_ms_; |
46 |
|
|
unsigned max_delay_ms_; |
47 |
|
|
unsigned reset_after_ms_; |
48 |
|
|
time_t last_throttle_; |
49 |
|
|
Prng prng_; |
50 |
|
|
pthread_mutex_t *lock_; |
51 |
|
|
}; |
52 |
|
|
|
53 |
|
|
#endif // CVMFS_BACKOFF_H_ |
54 |
|
|
|