2 * Copyright (C) 2004 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 1999-2002 Internet Software Consortium.
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.
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.
18 /* $Id: ratelimiter.c,v 1.18.14.4 2004/03/08 09:04:50 marka Exp $ */
23 #include <isc/ratelimiter.h>
26 #include <isc/timer.h>
30 isc_ratelimiter_stalled = 0,
31 isc_ratelimiter_ratelimited = 1,
32 isc_ratelimiter_idle = 2,
33 isc_ratelimiter_shuttingdown = 3
34 } isc_ratelimiter_state_t;
36 struct isc_ratelimiter {
42 isc_interval_t interval;
44 isc_ratelimiter_state_t state;
45 isc_event_t shutdownevent;
46 ISC_LIST(isc_event_t) pending;
49 #define ISC_RATELIMITEREVENT_SHUTDOWN (ISC_EVENTCLASS_RATELIMITER + 1)
52 ratelimiter_tick(isc_task_t *task, isc_event_t *event);
55 ratelimiter_shutdowncomplete(isc_task_t *task, isc_event_t *event);
58 isc_ratelimiter_create(isc_mem_t *mctx, isc_timermgr_t *timermgr,
59 isc_task_t *task, isc_ratelimiter_t **ratelimiterp)
62 isc_ratelimiter_t *rl;
63 INSIST(ratelimiterp != NULL && *ratelimiterp == NULL);
65 rl = isc_mem_get(mctx, sizeof(*rl));
67 return ISC_R_NOMEMORY;
71 isc_interval_set(&rl->interval, 0, 0);
74 rl->state = isc_ratelimiter_idle;
75 ISC_LIST_INIT(rl->pending);
77 result = isc_mutex_init(&rl->lock);
78 if (result != ISC_R_SUCCESS)
80 result = isc_timer_create(timermgr, isc_timertype_inactive,
81 NULL, NULL, rl->task, ratelimiter_tick,
83 if (result != ISC_R_SUCCESS)
87 * Increment the reference count to indicate that we may
88 * (soon) have events outstanding.
92 ISC_EVENT_INIT(&rl->shutdownevent,
94 0, NULL, ISC_RATELIMITEREVENT_SHUTDOWN,
95 ratelimiter_shutdowncomplete, rl, rl, NULL, NULL);
98 return (ISC_R_SUCCESS);
101 DESTROYLOCK(&rl->lock);
103 isc_mem_put(mctx, rl, sizeof(*rl));
108 isc_ratelimiter_setinterval(isc_ratelimiter_t *rl, isc_interval_t *interval) {
109 isc_result_t result = ISC_R_SUCCESS;
111 rl->interval = *interval;
113 * If the timer is currently running, change its rate.
115 if (rl->state == isc_ratelimiter_ratelimited) {
116 result = isc_timer_reset(rl->timer, isc_timertype_ticker, NULL,
117 &rl->interval, ISC_FALSE);
124 isc_ratelimiter_setpertic(isc_ratelimiter_t *rl, isc_uint32_t pertic) {
131 isc_ratelimiter_enqueue(isc_ratelimiter_t *rl, isc_task_t *task,
132 isc_event_t **eventp)
134 isc_result_t result = ISC_R_SUCCESS;
137 REQUIRE(eventp != NULL && *eventp != NULL);
138 REQUIRE(task != NULL);
140 REQUIRE(ev->ev_sender == NULL);
143 if (rl->state == isc_ratelimiter_ratelimited ||
144 rl->state == isc_ratelimiter_stalled) {
145 isc_event_t *ev = *eventp;
146 ev->ev_sender = task;
147 ISC_LIST_APPEND(rl->pending, ev, ev_link);
149 } else if (rl->state == isc_ratelimiter_idle) {
150 result = isc_timer_reset(rl->timer, isc_timertype_ticker, NULL,
151 &rl->interval, ISC_FALSE);
152 if (result == ISC_R_SUCCESS) {
153 ev->ev_sender = task;
154 rl->state = isc_ratelimiter_ratelimited;
157 INSIST(rl->state == isc_ratelimiter_shuttingdown);
158 result = ISC_R_SHUTTINGDOWN;
161 if (*eventp != NULL && result == ISC_R_SUCCESS)
162 isc_task_send(task, eventp);
167 ratelimiter_tick(isc_task_t *task, isc_event_t *event) {
168 isc_result_t result = ISC_R_SUCCESS;
169 isc_ratelimiter_t *rl = (isc_ratelimiter_t *)event->ev_arg;
175 isc_event_free(&event);
178 while (pertic != 0) {
181 p = ISC_LIST_HEAD(rl->pending);
184 * There is work to do. Let's do it after unlocking.
186 ISC_LIST_UNLINK(rl->pending, p, ev_link);
189 * No work left to do. Stop the timer so that we don't
190 * waste resources by having it fire periodically.
192 result = isc_timer_reset(rl->timer,
193 isc_timertype_inactive,
194 NULL, NULL, ISC_FALSE);
195 RUNTIME_CHECK(result == ISC_R_SUCCESS);
196 rl->state = isc_ratelimiter_idle;
197 pertic = 0; /* Force the loop to exit. */
201 isc_task_t *evtask = p->ev_sender;
202 isc_task_send(evtask, &p);
209 isc_ratelimiter_shutdown(isc_ratelimiter_t *rl) {
213 rl->state = isc_ratelimiter_shuttingdown;
214 (void)isc_timer_reset(rl->timer, isc_timertype_inactive,
215 NULL, NULL, ISC_FALSE);
216 while ((ev = ISC_LIST_HEAD(rl->pending)) != NULL) {
217 ISC_LIST_UNLINK(rl->pending, ev, ev_link);
218 ev->ev_attributes |= ISC_EVENTATTR_CANCELED;
219 task = ev->ev_sender;
220 isc_task_send(task, &ev);
222 isc_timer_detach(&rl->timer);
224 * Send an event to our task. The delivery of this event
225 * indicates that no more timer events will be delivered.
227 ev = &rl->shutdownevent;
228 isc_task_send(rl->task, &ev);
234 ratelimiter_shutdowncomplete(isc_task_t *task, isc_event_t *event) {
235 isc_ratelimiter_t *rl = (isc_ratelimiter_t *)event->ev_arg;
239 isc_ratelimiter_detach(&rl);
243 ratelimiter_free(isc_ratelimiter_t *rl) {
244 DESTROYLOCK(&rl->lock);
245 isc_mem_put(rl->mctx, rl, sizeof(*rl));
249 isc_ratelimiter_attach(isc_ratelimiter_t *source, isc_ratelimiter_t **target) {
250 REQUIRE(source != NULL);
251 REQUIRE(target != NULL && *target == NULL);
254 REQUIRE(source->refs > 0);
256 INSIST(source->refs > 0);
257 UNLOCK(&source->lock);
262 isc_ratelimiter_detach(isc_ratelimiter_t **rlp) {
263 isc_ratelimiter_t *rl = *rlp;
264 isc_boolean_t free_now = ISC_FALSE;
267 REQUIRE(rl->refs > 0);
274 ratelimiter_free(rl);
280 isc_ratelimiter_stall(isc_ratelimiter_t *rl) {
281 isc_result_t result = ISC_R_SUCCESS;
285 case isc_ratelimiter_shuttingdown:
286 result = ISC_R_SHUTTINGDOWN;
288 case isc_ratelimiter_ratelimited:
289 result = isc_timer_reset(rl->timer, isc_timertype_inactive,
290 NULL, NULL, ISC_FALSE);
291 RUNTIME_CHECK(result == ISC_R_SUCCESS);
292 case isc_ratelimiter_idle:
293 case isc_ratelimiter_stalled:
294 rl->state = isc_ratelimiter_stalled;
302 isc_ratelimiter_release(isc_ratelimiter_t *rl) {
303 isc_result_t result = ISC_R_SUCCESS;
307 case isc_ratelimiter_shuttingdown:
308 result = ISC_R_SHUTTINGDOWN;
310 case isc_ratelimiter_stalled:
311 if (!ISC_LIST_EMPTY(rl->pending)) {
312 result = isc_timer_reset(rl->timer,
313 isc_timertype_ticker, NULL,
314 &rl->interval, ISC_FALSE);
315 if (result == ISC_R_SUCCESS)
316 rl->state = isc_ratelimiter_ratelimited;
318 rl->state = isc_ratelimiter_idle;
320 case isc_ratelimiter_ratelimited:
321 case isc_ratelimiter_idle: