File: repl5_backoff.c

package info (click to toggle)
389-ds-base 2.3.1%2Bdfsg1-1%2Bdeb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 37,536 kB
  • sloc: ansic: 306,972; python: 96,937; cpp: 10,257; perl: 2,854; makefile: 2,046; sh: 925; yacc: 806; xml: 379; lex: 366; javascript: 148; java: 50
file content (231 lines) | stat: -rw-r--r-- 6,654 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/** BEGIN COPYRIGHT BLOCK
 * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
 * Copyright (C) 2005 Red Hat, Inc.
 * All rights reserved.
 *
 * License: GPL (version 3 or any later version).
 * See LICENSE for details.
 * END COPYRIGHT BLOCK **/

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

/* repl5_backoff.c */
/*

 The backoff object implements a backoff timer. The timer can operate
 with a fixed interval, an expontially increasing interval, or a
 random interval.

 The caller creates a new backoff timer, specifying the backoff behavior
 desired (fixed, exponential, or random), the initial backoff value,
 and the maximum backoff interval. This does not start the timer - the
 backoff_reset() function must be used to actually start the timer.

 The backoff_reset() function takes an optional function that
 will be called when the backoff time has expired, and a void *
 that can be used to pass arguments into the callback function.

 When the time expires, the callback function will be called. If no
 callback function has been provided, the timer simply expires.
 A timer does not recompute the next interval and begin timing until
 the backoff_step() function is called. Therefore, callers that
 do not install a callback function may use the timer by polling.
 When a callback function is provided, the timer is typically reset
 inside the callback function.

*/

#include "repl5.h"


typedef struct backoff_timer
{
    int type;
    int running;
    slapi_eq_fn_t callback;
    void *callback_arg;
    time_t initial_interval;
    time_t next_interval;
    time_t max_interval;
    time_t last_fire_time;
    Slapi_Eq_Context pending_event;
    PRLock *lock;

} backoff_timer;

/* Forward declarations */
static PRIntervalTime random_interval_in_range(time_t lower_bound, time_t upper_bound);


/*
 Create a new backoff timer. The timer is initialized, but is not
 started.
 */
Backoff_Timer *
backoff_new(int timer_type, int initial_interval, int max_interval)
{
    Backoff_Timer *bt;

    bt = (Backoff_Timer *)slapi_ch_calloc(1, sizeof(struct backoff_timer));
    bt->type = timer_type;
    bt->initial_interval = initial_interval;
    bt->next_interval = bt->initial_interval;
    bt->max_interval = max_interval;
    bt->running = 0;
    if ((bt->lock = PR_NewLock()) == NULL) {
        slapi_ch_free((void **)&bt);
    }
    return bt;
}


/*
 * Reset and start the timer. Returns the time (as a time_t) when the
 * time will next expire.
 */
time_t
backoff_reset(Backoff_Timer *bt, slapi_eq_fn_t callback, void *callback_data)
{
    time_t return_value = 0UL;

    PR_ASSERT(NULL != bt);
    PR_ASSERT(NULL != callback);

    PR_Lock(bt->lock);
    bt->running = 1;
    bt->callback = callback;
    bt->callback_arg = callback_data;
    /* Cancel any pending events in the event queue */
    if (NULL != bt->pending_event) {
        slapi_eq_cancel_rel(bt->pending_event);
        bt->pending_event = NULL;
    }
    /* Compute the first fire time */
    if (BACKOFF_RANDOM == bt->type) {
        bt->next_interval = random_interval_in_range(bt->initial_interval,
                                                     bt->max_interval);
    } else {
        bt->next_interval = bt->initial_interval;
    }
    /* Schedule the callback */
    bt->last_fire_time = slapi_current_rel_time_t();
    return_value = bt->last_fire_time + bt->next_interval;
    bt->pending_event = slapi_eq_once_rel(bt->callback, bt->callback_arg,
                                          return_value);
    PR_Unlock(bt->lock);
    return return_value;
}


/*
 Step the timer - compute the new backoff interval and start
 counting. Note that the next expiration time is based on the
 last timer expiration time, *not* the current time.

 Returns the time (as a time_t) when the timer will next expire.
 */
time_t
backoff_step(Backoff_Timer *bt)
{
    time_t return_value = 0UL;

    PR_ASSERT(NULL != bt);

    /* If the timer has never been reset, then return 0 */
    PR_Lock(bt->lock);
    if (bt->running) {
        time_t previous_interval = bt->next_interval;
        switch (bt->type) {
        case BACKOFF_FIXED:
            /* Interval stays the same */
            break;
        case BACKOFF_EXPONENTIAL:
            /* Interval doubles, up to a maximum */
            if (bt->next_interval < bt->max_interval) {
                bt->next_interval *= 2;
                if (bt->next_interval > bt->max_interval) {
                    bt->next_interval = bt->max_interval;
                }
            }
            break;
        case BACKOFF_RANDOM:
            /* Compute the new random interval time */
            bt->next_interval = random_interval_in_range(bt->initial_interval,
                                                         bt->max_interval);
            break;
        }
        /* Schedule the callback, if any */
        bt->last_fire_time += previous_interval;
        return_value = bt->last_fire_time + bt->next_interval;
        bt->pending_event = slapi_eq_once_rel(bt->callback, bt->callback_arg,
                                              return_value);
    }
    PR_Unlock(bt->lock);
    return return_value;
}


/*
 * Return 1 if the backoff timer has expired, 0 otherwise.
 */
int
backoff_expired(Backoff_Timer *bt, int margin)
{
    int return_value = 0;

    PR_ASSERT(NULL != bt);
    PR_Lock(bt->lock);
    return_value = (slapi_current_rel_time_t() >= (bt->last_fire_time + bt->next_interval + margin));
    PR_Unlock(bt->lock);
    return return_value;
}


/*
 Destroy and deallocate a timer object
 */
void
backoff_delete(Backoff_Timer **btp)
{
    Backoff_Timer *bt;

    PR_ASSERT(NULL != btp && NULL != *btp);
    bt = *btp;
    PR_Lock(bt->lock);
    /* Cancel any pending events in the event queue */
    if (NULL != bt->pending_event) {
        slapi_eq_cancel_rel(bt->pending_event);
    }
    PR_Unlock(bt->lock);
    PR_DestroyLock(bt->lock);
    slapi_ch_free((void **)btp);
}


/*
 * Return the next fire time for the timer.
 */
time_t
backoff_get_next_fire_time(Backoff_Timer *bt)
{
    time_t return_value;

    PR_ASSERT(NULL != bt);
    PR_Lock(bt->lock);
    return_value = bt->last_fire_time + bt->next_interval;
    PR_Unlock(bt->lock);
    return return_value;
}

static PRIntervalTime
random_interval_in_range(time_t lower_bound, time_t upper_bound)
{
    /*
     * slapi_rand() provides some entropy from two or three system timer
     * calls (depending on the platform) down in NSS. If more entropy is
     * required, slapi_rand_r(unsigned int *seed) can be called instead.
      */
    return (lower_bound + (slapi_rand() % (upper_bound - lower_bound)));
}