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