repl5_backoff.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. /** BEGIN COPYRIGHT BLOCK
  2. * This Program is free software; you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation; version 2 of the License.
  5. *
  6. * This Program is distributed in the hope that it will be useful, but WITHOUT
  7. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  9. *
  10. * You should have received a copy of the GNU General Public License along with
  11. * this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
  12. * Place, Suite 330, Boston, MA 02111-1307 USA.
  13. *
  14. * In addition, as a special exception, Red Hat, Inc. gives You the additional
  15. * right to link the code of this Program with code not covered under the GNU
  16. * General Public License ("Non-GPL Code") and to distribute linked combinations
  17. * including the two, subject to the limitations in this paragraph. Non-GPL Code
  18. * permitted under this exception must only link to the code of this Program
  19. * through those well defined interfaces identified in the file named EXCEPTION
  20. * found in the source code files (the "Approved Interfaces"). The files of
  21. * Non-GPL Code may instantiate templates or use macros or inline functions from
  22. * the Approved Interfaces without causing the resulting work to be covered by
  23. * the GNU General Public License. Only Red Hat, Inc. may make changes or
  24. * additions to the list of Approved Interfaces. You must obey the GNU General
  25. * Public License in all respects for all of the Program code and other code used
  26. * in conjunction with the Program except the Non-GPL Code covered by this
  27. * exception. If you modify this file, you may extend this exception to your
  28. * version of the file, but you are not obligated to do so. If you do not wish to
  29. * provide this exception without modification, you must delete this exception
  30. * statement from your version and license this file solely under the GPL without
  31. * exception.
  32. *
  33. *
  34. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  35. * Copyright (C) 2005 Red Hat, Inc.
  36. * All rights reserved.
  37. * END COPYRIGHT BLOCK **/
  38. #ifdef HAVE_CONFIG_H
  39. # include <config.h>
  40. #endif
  41. /* repl5_backoff.c */
  42. /*
  43. The backoff object implements a backoff timer. The timer can operate
  44. with a fixed interval, an expontially increasing interval, or a
  45. random interval.
  46. The caller creates a new backoff timer, specifying the backoff behavior
  47. desired (fixed, exponential, or random), the initial backoff value,
  48. and the maximum backoff interval. This does not start the timer - the
  49. backoff_reset() function must be used to actually start the timer.
  50. The backoff_reset() function takes an optional function that
  51. will be called when the backoff time has expired, and a void *
  52. that can be used to pass arguments into the callback function.
  53. When the time expires, the callback function will be called. If no
  54. callback function has been provided, the timer simply expires.
  55. A timer does not recompute the next interval and begin timing until
  56. the backoff_step() function is called. Therefore, callers that
  57. do not install a callback function may use the timer by polling.
  58. When a callback function is provided, the timer is typically reset
  59. inside the callback function.
  60. */
  61. #include "repl5.h"
  62. typedef struct backoff_timer {
  63. int type;
  64. int running;
  65. slapi_eq_fn_t callback;
  66. void *callback_arg;
  67. time_t initial_interval;
  68. time_t next_interval;
  69. time_t max_interval;
  70. time_t last_fire_time;
  71. Slapi_Eq_Context pending_event;
  72. PRLock *lock;
  73. } backoff_timer;
  74. /* Forward declarations */
  75. static PRIntervalTime random_interval_in_range(time_t lower_bound, time_t upper_bound);
  76. /*
  77. Create a new backoff timer. The timer is initialized, but is not
  78. started.
  79. */
  80. Backoff_Timer *
  81. backoff_new(int timer_type, int initial_interval, int max_interval)
  82. {
  83. Backoff_Timer *bt;
  84. bt = (Backoff_Timer *)slapi_ch_calloc(1, sizeof(struct backoff_timer));
  85. bt->type = timer_type;
  86. bt->initial_interval = initial_interval;
  87. bt->next_interval = bt->initial_interval;
  88. bt->max_interval = max_interval;
  89. bt->running = 0;
  90. if ((bt->lock = PR_NewLock()) == NULL)
  91. {
  92. slapi_ch_free((void **)&bt);
  93. }
  94. return bt;
  95. }
  96. /*
  97. * Reset and start the timer. Returns the time (as a time_t) when the
  98. * time will next expire.
  99. */
  100. time_t
  101. backoff_reset(Backoff_Timer *bt, slapi_eq_fn_t callback, void *callback_data)
  102. {
  103. time_t return_value = 0UL;
  104. PR_ASSERT(NULL != bt);
  105. PR_ASSERT(NULL != callback);
  106. PR_Lock(bt->lock);
  107. bt->running = 1;
  108. bt->callback = callback;
  109. bt->callback_arg = callback_data;
  110. /* Cancel any pending events in the event queue */
  111. if (NULL != bt->pending_event)
  112. {
  113. slapi_eq_cancel(bt->pending_event);
  114. bt->pending_event = NULL;
  115. }
  116. /* Compute the first fire time */
  117. if (BACKOFF_RANDOM == bt->type)
  118. {
  119. bt->next_interval = random_interval_in_range(bt->initial_interval,
  120. bt->max_interval);
  121. }
  122. else
  123. {
  124. bt->next_interval = bt->initial_interval;
  125. }
  126. /* Schedule the callback */
  127. time(&bt->last_fire_time);
  128. return_value = bt->last_fire_time + bt->next_interval;
  129. bt->pending_event = slapi_eq_once(bt->callback, bt->callback_arg,
  130. return_value);
  131. PR_Unlock(bt->lock);
  132. return return_value;
  133. }
  134. /*
  135. Step the timer - compute the new backoff interval and start
  136. counting. Note that the next expiration time is based on the
  137. last timer expiration time, *not* the current time.
  138. Returns the time (as a time_t) when the timer will next expire.
  139. */
  140. time_t
  141. backoff_step(Backoff_Timer *bt)
  142. {
  143. time_t return_value = 0UL;
  144. PR_ASSERT(NULL != bt);
  145. /* If the timer has never been reset, then return 0 */
  146. PR_Lock(bt->lock);
  147. if (bt->running)
  148. {
  149. time_t previous_interval = bt->next_interval;
  150. switch (bt->type) {
  151. case BACKOFF_FIXED:
  152. /* Interval stays the same */
  153. break;
  154. case BACKOFF_EXPONENTIAL:
  155. /* Interval doubles, up to a maximum */
  156. if (bt->next_interval < bt->max_interval)
  157. {
  158. bt->next_interval *= 2;
  159. if (bt->next_interval > bt->max_interval)
  160. {
  161. bt->next_interval = bt->max_interval;
  162. }
  163. }
  164. break;
  165. case BACKOFF_RANDOM:
  166. /* Compute the new random interval time */
  167. bt->next_interval = random_interval_in_range(bt->initial_interval,
  168. bt->max_interval);
  169. break;
  170. }
  171. /* Schedule the callback, if any */
  172. bt->last_fire_time += previous_interval;
  173. return_value = bt->last_fire_time + bt->next_interval;
  174. bt->pending_event = slapi_eq_once(bt->callback, bt->callback_arg,
  175. return_value);
  176. }
  177. PR_Unlock(bt->lock);
  178. return return_value;
  179. }
  180. /*
  181. * Return 1 if the backoff timer has expired, 0 otherwise.
  182. */
  183. int
  184. backoff_expired(Backoff_Timer *bt, int margin)
  185. {
  186. int return_value = 0;
  187. PR_ASSERT(NULL != bt);
  188. PR_Lock(bt->lock);
  189. return_value = (current_time() >= (bt->last_fire_time + bt->next_interval + margin));
  190. PR_Unlock(bt->lock);
  191. return return_value;
  192. }
  193. /*
  194. Destroy and deallocate a timer object
  195. */
  196. void
  197. backoff_delete(Backoff_Timer **btp)
  198. {
  199. Backoff_Timer *bt;
  200. PR_ASSERT(NULL != btp && NULL != *btp);
  201. bt = *btp;
  202. PR_Lock(bt->lock);
  203. /* Cancel any pending events in the event queue */
  204. if (NULL != bt->pending_event)
  205. {
  206. slapi_eq_cancel(bt->pending_event);
  207. }
  208. PR_Unlock(bt->lock);
  209. PR_DestroyLock(bt->lock);
  210. slapi_ch_free((void **)btp);
  211. }
  212. /*
  213. * Return the next fire time for the timer.
  214. */
  215. time_t
  216. backoff_get_next_fire_time(Backoff_Timer *bt)
  217. {
  218. time_t return_value;
  219. PR_ASSERT(NULL != bt);
  220. PR_Lock(bt->lock);
  221. return_value = bt->last_fire_time + bt->next_interval;
  222. PR_Unlock(bt->lock);
  223. return return_value;
  224. }
  225. static PRIntervalTime
  226. random_interval_in_range(time_t lower_bound, time_t upper_bound)
  227. {
  228. /*
  229. * slapi_rand() provides some entropy from two or three system timer
  230. * calls (depending on the platform) down in NSS. If more entropy is
  231. * required, slapi_rand_r(unsigned int *seed) can be called instead.
  232. */
  233. return(lower_bound + (slapi_rand() % (upper_bound - lower_bound)));
  234. }