CernVM-FS  2.13.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
backoff.cc
Go to the documentation of this file.
1 
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 void BackoffThrottle::Init(const unsigned init_delay_ms,
19  const unsigned max_delay_ms,
20  const unsigned reset_after_ms) {
21  init_delay_ms_ = init_delay_ms;
22  max_delay_ms_ = max_delay_ms;
23  reset_after_ms_ = reset_after_ms;
24  prng_.InitLocaltime();
25 
26  lock_ = reinterpret_cast<pthread_mutex_t *>(smalloc(sizeof(pthread_mutex_t)));
27  int retval = pthread_mutex_init(lock_, NULL);
28  assert(retval == 0);
29 
30  Reset();
31 }
32 
33 
35  pthread_mutex_destroy(lock_);
36  free(lock_);
37 }
38 
39 
41  pthread_mutex_lock(lock_);
42  delay_range_ = 0;
43  last_throttle_ = 0;
44  pthread_mutex_unlock(lock_);
45 }
46 
47 
49  time_t now = time(NULL);
50 
51  pthread_mutex_lock(lock_);
52  if (unsigned(now - last_throttle_) < reset_after_ms_ / 1000) {
53  if (delay_range_ < max_delay_ms_) {
54  if (delay_range_ == 0)
55  delay_range_ = init_delay_ms_;
56  else
57  delay_range_ *= 2;
58  }
59  unsigned delay = prng_.Next(delay_range_) + 1;
60  if (delay > max_delay_ms_)
61  delay = max_delay_ms_;
62 
63  pthread_mutex_unlock(lock_);
64  LogCvmfs(kLogCvmfs, kLogDebug, "backoff throttle %d ms", delay);
65  SafeSleepMs(delay);
66  pthread_mutex_lock(lock_);
67  }
68  last_throttle_ = now;
69  pthread_mutex_unlock(lock_);
70 }
void Init(const unsigned init_delay_ms, const unsigned max_delay_ms, const unsigned reset_after_ms)
Definition: backoff.cc:18
assert((mem||(size==0))&&"Out Of Memory")
void Throttle()
Definition: backoff.cc:48
void Reset()
Definition: backoff.cc:40
void SafeSleepMs(const unsigned ms)
Definition: posix.cc:2024
CVMFS_EXPORT void LogCvmfs(const LogSource source, const int mask, const char *format,...)
Definition: logging.cc:545