Merge from vendor branch LIBARCHIVE:
[dragonfly.git] / contrib / bind-9.2.4rc7 / lib / isc / ratelimiter.c
1 /*
2  * Copyright (C) 2004  Internet Systems Consortium, Inc. ("ISC")
3  * Copyright (C) 1999-2001  Internet Software Consortium.
4  *
5  * Permission to use, copy, modify, and distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15  * PERFORMANCE OF THIS SOFTWARE.
16  */
17
18 /* $Id: ratelimiter.c,v 1.18.2.1 2004/03/09 06:11:50 marka Exp $ */
19
20 #include <config.h>
21
22 #include <isc/mem.h>
23 #include <isc/ratelimiter.h>
24 #include <isc/task.h>
25 #include <isc/time.h>
26 #include <isc/timer.h>
27 #include <isc/util.h>
28
29 typedef enum {
30         isc_ratelimiter_ratelimited,
31         isc_ratelimiter_worklimited,
32         isc_ratelimiter_shuttingdown
33 } isc_ratelimiter_state_t;
34
35 struct isc_ratelimiter {
36         isc_mem_t *             mctx;
37         isc_mutex_t             lock;
38         int                     refs;
39         isc_task_t *            task;
40         isc_timer_t *           timer;
41         isc_interval_t          interval;
42         isc_uint32_t            pertic;
43         isc_ratelimiter_state_t state;
44         isc_event_t             shutdownevent;
45         ISC_LIST(isc_event_t)   pending;
46 };
47
48 #define ISC_RATELIMITEREVENT_SHUTDOWN (ISC_EVENTCLASS_RATELIMITER + 1)
49
50 static void
51 ratelimiter_tick(isc_task_t *task, isc_event_t *event);
52
53 static void
54 ratelimiter_shutdowncomplete(isc_task_t *task, isc_event_t *event);
55
56 isc_result_t
57 isc_ratelimiter_create(isc_mem_t *mctx, isc_timermgr_t *timermgr,
58                        isc_task_t *task, isc_ratelimiter_t **ratelimiterp)
59 {
60         isc_result_t result;
61         isc_ratelimiter_t *rl;
62         INSIST(ratelimiterp != NULL && *ratelimiterp == NULL);
63
64         rl = isc_mem_get(mctx, sizeof(*rl));
65         if (rl == NULL)
66                 return ISC_R_NOMEMORY;
67         rl->mctx = mctx;
68         rl->refs = 1;
69         rl->task = task;
70         isc_interval_set(&rl->interval, 0, 0);
71         rl->timer = NULL;
72         rl->pertic = 1;
73         rl->state = isc_ratelimiter_worklimited;
74         ISC_LIST_INIT(rl->pending);
75
76         result = isc_mutex_init(&rl->lock);
77         if (result != ISC_R_SUCCESS)
78                 goto free_mem;
79         result = isc_timer_create(timermgr, isc_timertype_inactive,
80                                   NULL, NULL, rl->task, ratelimiter_tick,
81                                   rl, &rl->timer);
82         if (result != ISC_R_SUCCESS)
83                 goto free_mutex;
84
85         /*
86          * Increment the reference count to indicate that we may
87          * (soon) have events outstanding.
88          */
89         rl->refs++;
90
91         ISC_EVENT_INIT(&rl->shutdownevent,
92                        sizeof(isc_event_t),
93                        0, NULL, ISC_RATELIMITEREVENT_SHUTDOWN,
94                        ratelimiter_shutdowncomplete, rl, rl, NULL, NULL);
95
96         *ratelimiterp = rl;
97         return (ISC_R_SUCCESS);
98
99 free_mutex:
100         DESTROYLOCK(&rl->lock);
101 free_mem:
102         isc_mem_put(mctx, rl, sizeof(*rl));
103         return (result);
104 }
105
106 isc_result_t
107 isc_ratelimiter_setinterval(isc_ratelimiter_t *rl, isc_interval_t *interval) {
108         isc_result_t result = ISC_R_SUCCESS;
109         LOCK(&rl->lock);
110         rl->interval = *interval;
111         /*
112          * If the timer is currently running, change its rate.
113          */
114         if (rl->state == isc_ratelimiter_ratelimited) {
115                 result = isc_timer_reset(rl->timer, isc_timertype_ticker, NULL,
116                                          &rl->interval, ISC_FALSE);
117         }
118         UNLOCK(&rl->lock);
119         return (result);
120 }
121
122 void
123 isc_ratelimiter_setpertic(isc_ratelimiter_t *rl, isc_uint32_t pertic) {
124         if (pertic == 0)
125                 pertic = 1;
126         rl->pertic = pertic;
127 }
128
129 isc_result_t
130 isc_ratelimiter_enqueue(isc_ratelimiter_t *rl, isc_task_t *task,
131                         isc_event_t **eventp)
132 {
133         isc_result_t result = ISC_R_SUCCESS;
134         isc_event_t *ev;
135
136         REQUIRE(eventp != NULL && *eventp != NULL);
137         REQUIRE(task != NULL);
138         ev = *eventp;
139         REQUIRE(ev->ev_sender == NULL);
140
141         LOCK(&rl->lock);
142         if (rl->state == isc_ratelimiter_ratelimited) {
143                 isc_event_t *ev = *eventp;
144                 ev->ev_sender = task;
145                 ISC_LIST_APPEND(rl->pending, ev, ev_link);
146                 *eventp = NULL;
147         } else if (rl->state == isc_ratelimiter_worklimited) {
148                 result = isc_timer_reset(rl->timer, isc_timertype_ticker, NULL,
149                                          &rl->interval, ISC_FALSE);
150                 if (result == ISC_R_SUCCESS) {
151                         ev->ev_sender = task;
152                         rl->state = isc_ratelimiter_ratelimited;
153                 }
154         } else {
155                 INSIST(rl->state == isc_ratelimiter_shuttingdown);
156                 result = ISC_R_SHUTTINGDOWN;
157         }
158         UNLOCK(&rl->lock);
159         if (*eventp != NULL && result == ISC_R_SUCCESS)
160                 isc_task_send(task, eventp);
161         return (result);
162 }
163
164 static void
165 ratelimiter_tick(isc_task_t *task, isc_event_t *event) {
166         isc_result_t result = ISC_R_SUCCESS;
167         isc_ratelimiter_t *rl = (isc_ratelimiter_t *)event->ev_arg;
168         isc_event_t *p;
169         isc_uint32_t pertic;
170
171         UNUSED(task);
172
173         isc_event_free(&event);
174
175         pertic = rl->pertic;
176         while (pertic != 0) {
177                 pertic--;
178                 LOCK(&rl->lock);
179                 p = ISC_LIST_HEAD(rl->pending);
180                 if (p != NULL) {
181                         /*
182                          * There is work to do.  Let's do it after unlocking.
183                          */
184                         ISC_LIST_UNLINK(rl->pending, p, ev_link);
185                 } else {
186                         /*
187                          * No work left to do.  Stop the timer so that we don't
188                          * waste resources by having it fire periodically.
189                          */
190                         result = isc_timer_reset(rl->timer,
191                                                  isc_timertype_inactive,
192                                                  NULL, NULL, ISC_FALSE);
193                         RUNTIME_CHECK(result == ISC_R_SUCCESS);
194                         rl->state = isc_ratelimiter_worklimited;
195                         pertic = 0;     /* Force the loop to exit. */
196                 }
197                 UNLOCK(&rl->lock);
198                 if (p != NULL) {
199                         isc_task_t *evtask = p->ev_sender;
200                         isc_task_send(evtask, &p);
201                 }
202                 INSIST(p == NULL);
203         }
204 }
205
206 void
207 isc_ratelimiter_shutdown(isc_ratelimiter_t *rl) {
208         isc_event_t *ev;
209         isc_task_t *task;
210         LOCK(&rl->lock);
211         rl->state = isc_ratelimiter_shuttingdown;
212         (void)isc_timer_reset(rl->timer, isc_timertype_inactive,
213                               NULL, NULL, ISC_FALSE);
214         while ((ev = ISC_LIST_HEAD(rl->pending)) != NULL) {
215                 ISC_LIST_UNLINK(rl->pending, ev, ev_link);
216                 ev->ev_attributes |= ISC_EVENTATTR_CANCELED;
217                 task = ev->ev_sender;
218                 isc_task_send(task, &ev);
219         }
220         isc_timer_detach(&rl->timer);
221         /*
222          * Send an event to our task.  The delivery of this event
223          * indicates that no more timer events will be delivered.
224          */
225         ev = &rl->shutdownevent;
226         isc_task_send(rl->task, &ev);
227
228         UNLOCK(&rl->lock);
229 }
230
231 static void
232 ratelimiter_shutdowncomplete(isc_task_t *task, isc_event_t *event) {
233         isc_ratelimiter_t *rl = (isc_ratelimiter_t *)event->ev_arg;
234
235         UNUSED(task);
236
237         isc_ratelimiter_detach(&rl);
238 }
239
240 static void
241 ratelimiter_free(isc_ratelimiter_t *rl) {
242         DESTROYLOCK(&rl->lock);
243         isc_mem_put(rl->mctx, rl, sizeof(*rl));
244 }
245
246 void
247 isc_ratelimiter_attach(isc_ratelimiter_t *source, isc_ratelimiter_t **target) {
248         REQUIRE(source != NULL);
249         REQUIRE(target != NULL && *target == NULL);
250
251         LOCK(&source->lock);
252         REQUIRE(source->refs > 0);
253         source->refs++;
254         INSIST(source->refs > 0);
255         UNLOCK(&source->lock);
256         *target = source;
257 }
258
259 void
260 isc_ratelimiter_detach(isc_ratelimiter_t **rlp) {
261         isc_ratelimiter_t *rl = *rlp;
262         isc_boolean_t free_now = ISC_FALSE;
263
264         LOCK(&rl->lock);
265         REQUIRE(rl->refs > 0);
266         rl->refs--;
267         if (rl->refs == 0)
268                 free_now = ISC_TRUE;
269         UNLOCK(&rl->lock);
270
271         if (free_now)
272                 ratelimiter_free(rl);
273
274         *rlp = NULL;
275 }
276