repl5_backoff.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /** BEGIN COPYRIGHT BLOCK
  2. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  3. * Copyright (C) 2005 Red Hat, Inc.
  4. * All rights reserved.
  5. *
  6. * License: GPL (version 3 or any later version).
  7. * See LICENSE for details.
  8. * END COPYRIGHT BLOCK **/
  9. #ifdef HAVE_CONFIG_H
  10. # include <config.h>
  11. #endif
  12. /* repl5_backoff.c */
  13. /*
  14. The backoff object implements a backoff timer. The timer can operate
  15. with a fixed interval, an expontially increasing interval, or a
  16. random interval.
  17. The caller creates a new backoff timer, specifying the backoff behavior
  18. desired (fixed, exponential, or random), the initial backoff value,
  19. and the maximum backoff interval. This does not start the timer - the
  20. backoff_reset() function must be used to actually start the timer.
  21. The backoff_reset() function takes an optional function that
  22. will be called when the backoff time has expired, and a void *
  23. that can be used to pass arguments into the callback function.
  24. When the time expires, the callback function will be called. If no
  25. callback function has been provided, the timer simply expires.
  26. A timer does not recompute the next interval and begin timing until
  27. the backoff_step() function is called. Therefore, callers that
  28. do not install a callback function may use the timer by polling.
  29. When a callback function is provided, the timer is typically reset
  30. inside the callback function.
  31. */
  32. #include "repl5.h"
  33. typedef struct backoff_timer {
  34. int type;
  35. int running;
  36. slapi_eq_fn_t callback;
  37. void *callback_arg;
  38. time_t initial_interval;
  39. time_t next_interval;
  40. time_t max_interval;
  41. time_t last_fire_time;
  42. Slapi_Eq_Context pending_event;
  43. PRLock *lock;
  44. } backoff_timer;
  45. /* Forward declarations */
  46. static PRIntervalTime random_interval_in_range(time_t lower_bound, time_t upper_bound);
  47. /*
  48. Create a new backoff timer. The timer is initialized, but is not
  49. started.
  50. */
  51. Backoff_Timer *
  52. backoff_new(int timer_type, int initial_interval, int max_interval)
  53. {
  54. Backoff_Timer *bt;
  55. bt = (Backoff_Timer *)slapi_ch_calloc(1, sizeof(struct backoff_timer));
  56. bt->type = timer_type;
  57. bt->initial_interval = initial_interval;
  58. bt->next_interval = bt->initial_interval;
  59. bt->max_interval = max_interval;
  60. bt->running = 0;
  61. if ((bt->lock = PR_NewLock()) == NULL)
  62. {
  63. slapi_ch_free((void **)&bt);
  64. }
  65. return bt;
  66. }
  67. /*
  68. * Reset and start the timer. Returns the time (as a time_t) when the
  69. * time will next expire.
  70. */
  71. time_t
  72. backoff_reset(Backoff_Timer *bt, slapi_eq_fn_t callback, void *callback_data)
  73. {
  74. time_t return_value = 0UL;
  75. PR_ASSERT(NULL != bt);
  76. PR_ASSERT(NULL != callback);
  77. PR_Lock(bt->lock);
  78. bt->running = 1;
  79. bt->callback = callback;
  80. bt->callback_arg = callback_data;
  81. /* Cancel any pending events in the event queue */
  82. if (NULL != bt->pending_event)
  83. {
  84. slapi_eq_cancel(bt->pending_event);
  85. bt->pending_event = NULL;
  86. }
  87. /* Compute the first fire time */
  88. if (BACKOFF_RANDOM == bt->type)
  89. {
  90. bt->next_interval = random_interval_in_range(bt->initial_interval,
  91. bt->max_interval);
  92. }
  93. else
  94. {
  95. bt->next_interval = bt->initial_interval;
  96. }
  97. /* Schedule the callback */
  98. time(&bt->last_fire_time);
  99. return_value = bt->last_fire_time + bt->next_interval;
  100. bt->pending_event = slapi_eq_once(bt->callback, bt->callback_arg,
  101. return_value);
  102. PR_Unlock(bt->lock);
  103. return return_value;
  104. }
  105. /*
  106. Step the timer - compute the new backoff interval and start
  107. counting. Note that the next expiration time is based on the
  108. last timer expiration time, *not* the current time.
  109. Returns the time (as a time_t) when the timer will next expire.
  110. */
  111. time_t
  112. backoff_step(Backoff_Timer *bt)
  113. {
  114. time_t return_value = 0UL;
  115. PR_ASSERT(NULL != bt);
  116. /* If the timer has never been reset, then return 0 */
  117. PR_Lock(bt->lock);
  118. if (bt->running)
  119. {
  120. time_t previous_interval = bt->next_interval;
  121. switch (bt->type) {
  122. case BACKOFF_FIXED:
  123. /* Interval stays the same */
  124. break;
  125. case BACKOFF_EXPONENTIAL:
  126. /* Interval doubles, up to a maximum */
  127. if (bt->next_interval < bt->max_interval)
  128. {
  129. bt->next_interval *= 2;
  130. if (bt->next_interval > bt->max_interval)
  131. {
  132. bt->next_interval = bt->max_interval;
  133. }
  134. }
  135. break;
  136. case BACKOFF_RANDOM:
  137. /* Compute the new random interval time */
  138. bt->next_interval = random_interval_in_range(bt->initial_interval,
  139. bt->max_interval);
  140. break;
  141. }
  142. /* Schedule the callback, if any */
  143. bt->last_fire_time += previous_interval;
  144. return_value = bt->last_fire_time + bt->next_interval;
  145. bt->pending_event = slapi_eq_once(bt->callback, bt->callback_arg,
  146. return_value);
  147. }
  148. PR_Unlock(bt->lock);
  149. return return_value;
  150. }
  151. /*
  152. * Return 1 if the backoff timer has expired, 0 otherwise.
  153. */
  154. int
  155. backoff_expired(Backoff_Timer *bt, int margin)
  156. {
  157. int return_value = 0;
  158. PR_ASSERT(NULL != bt);
  159. PR_Lock(bt->lock);
  160. return_value = (current_time() >= (bt->last_fire_time + bt->next_interval + margin));
  161. PR_Unlock(bt->lock);
  162. return return_value;
  163. }
  164. /*
  165. Destroy and deallocate a timer object
  166. */
  167. void
  168. backoff_delete(Backoff_Timer **btp)
  169. {
  170. Backoff_Timer *bt;
  171. PR_ASSERT(NULL != btp && NULL != *btp);
  172. bt = *btp;
  173. PR_Lock(bt->lock);
  174. /* Cancel any pending events in the event queue */
  175. if (NULL != bt->pending_event)
  176. {
  177. slapi_eq_cancel(bt->pending_event);
  178. }
  179. PR_Unlock(bt->lock);
  180. PR_DestroyLock(bt->lock);
  181. slapi_ch_free((void **)btp);
  182. }
  183. /*
  184. * Return the next fire time for the timer.
  185. */
  186. time_t
  187. backoff_get_next_fire_time(Backoff_Timer *bt)
  188. {
  189. time_t return_value;
  190. PR_ASSERT(NULL != bt);
  191. PR_Lock(bt->lock);
  192. return_value = bt->last_fire_time + bt->next_interval;
  193. PR_Unlock(bt->lock);
  194. return return_value;
  195. }
  196. static PRIntervalTime
  197. random_interval_in_range(time_t lower_bound, time_t upper_bound)
  198. {
  199. /*
  200. * slapi_rand() provides some entropy from two or three system timer
  201. * calls (depending on the platform) down in NSS. If more entropy is
  202. * required, slapi_rand_r(unsigned int *seed) can be called instead.
  203. */
  204. return(lower_bound + (slapi_rand() % (upper_bound - lower_bound)));
  205. }