1999-11-17 01:29:44 +00:00
|
|
|
/*
|
2000-02-03 23:08:31 +00:00
|
|
|
* Copyright (C) 1999, 2000 Internet Software Consortium.
|
1999-11-17 01:29:44 +00:00
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
2000-07-27 09:55:03 +00:00
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM
|
|
|
|
* DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
|
|
|
|
* INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
|
|
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
|
|
|
|
* FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
|
|
|
|
* NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
|
|
|
|
* WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
1999-11-17 01:29:44 +00:00
|
|
|
*/
|
|
|
|
|
2000-07-27 09:55:03 +00:00
|
|
|
/* $Id: ratelimiter.c,v 1.15 2000/07/27 09:51:08 tale Exp $ */
|
2000-06-22 22:00:42 +00:00
|
|
|
|
1999-11-17 01:29:44 +00:00
|
|
|
#include <config.h>
|
|
|
|
|
103. [func] libisc buffer API changes for <isc/buffer.h>:
Added:
isc_buffer_base(b) (pointer)
isc_buffer_current(b) (pointer)
isc_buffer_active(b) (pointer)
isc_buffer_used(b) (pointer)
isc_buffer_length(b) (int)
isc_buffer_usedlength(b) (int)
isc_buffer_consumedlength(b) (int)
isc_buffer_remaininglength(b) (int)
isc_buffer_activelength(b) (int)
isc_buffer_availablelength(b) (int)
Removed:
ISC_BUFFER_USEDCOUNT(b)
ISC_BUFFER_AVAILABLECOUNT(b)
isc_buffer_type(b)
Changed names:
isc_buffer_used(b, r) ->
isc_buffer_usedregion(b, r)
isc_buffer_available(b, r) ->
isc_buffer_available_region(b, r)
isc_buffer_consumed(b, r) ->
isc_buffer_consumedregion(b, r)
isc_buffer_active(b, r) ->
isc_buffer_activeregion(b, r)
isc_buffer_remaining(b, r) ->
isc_buffer_remainingregion(b, r)
Buffer types were removed, so the ISC_BUFFERTYPE_*
macros are no more, and the type argument to
isc_buffer_init and isc_buffer_allocate were removed.
isc_buffer_putstr is now void (instead of isc_result_t)
and requires that the caller ensure that there
is enough available buffer space for the string.
2000-04-27 00:03:12 +00:00
|
|
|
#include <isc/mem.h>
|
1999-11-17 01:29:44 +00:00
|
|
|
#include <isc/ratelimiter.h>
|
103. [func] libisc buffer API changes for <isc/buffer.h>:
Added:
isc_buffer_base(b) (pointer)
isc_buffer_current(b) (pointer)
isc_buffer_active(b) (pointer)
isc_buffer_used(b) (pointer)
isc_buffer_length(b) (int)
isc_buffer_usedlength(b) (int)
isc_buffer_consumedlength(b) (int)
isc_buffer_remaininglength(b) (int)
isc_buffer_activelength(b) (int)
isc_buffer_availablelength(b) (int)
Removed:
ISC_BUFFER_USEDCOUNT(b)
ISC_BUFFER_AVAILABLECOUNT(b)
isc_buffer_type(b)
Changed names:
isc_buffer_used(b, r) ->
isc_buffer_usedregion(b, r)
isc_buffer_available(b, r) ->
isc_buffer_available_region(b, r)
isc_buffer_consumed(b, r) ->
isc_buffer_consumedregion(b, r)
isc_buffer_active(b, r) ->
isc_buffer_activeregion(b, r)
isc_buffer_remaining(b, r) ->
isc_buffer_remainingregion(b, r)
Buffer types were removed, so the ISC_BUFFERTYPE_*
macros are no more, and the type argument to
isc_buffer_init and isc_buffer_allocate were removed.
isc_buffer_putstr is now void (instead of isc_result_t)
and requires that the caller ensure that there
is enough available buffer space for the string.
2000-04-27 00:03:12 +00:00
|
|
|
#include <isc/task.h>
|
2000-04-25 19:32:11 +00:00
|
|
|
#include <isc/time.h>
|
103. [func] libisc buffer API changes for <isc/buffer.h>:
Added:
isc_buffer_base(b) (pointer)
isc_buffer_current(b) (pointer)
isc_buffer_active(b) (pointer)
isc_buffer_used(b) (pointer)
isc_buffer_length(b) (int)
isc_buffer_usedlength(b) (int)
isc_buffer_consumedlength(b) (int)
isc_buffer_remaininglength(b) (int)
isc_buffer_activelength(b) (int)
isc_buffer_availablelength(b) (int)
Removed:
ISC_BUFFER_USEDCOUNT(b)
ISC_BUFFER_AVAILABLECOUNT(b)
isc_buffer_type(b)
Changed names:
isc_buffer_used(b, r) ->
isc_buffer_usedregion(b, r)
isc_buffer_available(b, r) ->
isc_buffer_available_region(b, r)
isc_buffer_consumed(b, r) ->
isc_buffer_consumedregion(b, r)
isc_buffer_active(b, r) ->
isc_buffer_activeregion(b, r)
isc_buffer_remaining(b, r) ->
isc_buffer_remainingregion(b, r)
Buffer types were removed, so the ISC_BUFFERTYPE_*
macros are no more, and the type argument to
isc_buffer_init and isc_buffer_allocate were removed.
isc_buffer_putstr is now void (instead of isc_result_t)
and requires that the caller ensure that there
is enough available buffer space for the string.
2000-04-27 00:03:12 +00:00
|
|
|
#include <isc/timer.h>
|
1999-12-16 22:24:22 +00:00
|
|
|
#include <isc/util.h>
|
1999-11-17 01:29:44 +00:00
|
|
|
|
2000-04-26 17:10:32 +00:00
|
|
|
typedef enum {
|
|
|
|
isc_ratelimiter_ratelimited,
|
|
|
|
isc_ratelimiter_worklimited,
|
|
|
|
isc_ratelimiter_shuttingdown
|
|
|
|
} isc_ratelimiter_state_t;
|
|
|
|
|
1999-11-17 01:29:44 +00:00
|
|
|
struct isc_ratelimiter {
|
|
|
|
isc_mem_t * mctx;
|
|
|
|
isc_mutex_t lock;
|
2000-06-15 17:40:22 +00:00
|
|
|
int refs;
|
1999-11-17 01:29:44 +00:00
|
|
|
isc_task_t * task;
|
|
|
|
isc_timer_t * timer;
|
|
|
|
isc_interval_t interval;
|
2000-05-11 09:23:29 +00:00
|
|
|
isc_uint32_t pertic;
|
1999-11-17 01:29:44 +00:00
|
|
|
isc_ratelimiter_state_t state;
|
2000-04-26 17:10:32 +00:00
|
|
|
isc_event_t shutdownevent;
|
1999-11-17 01:29:44 +00:00
|
|
|
ISC_LIST(isc_event_t) pending;
|
|
|
|
};
|
|
|
|
|
2000-04-26 17:10:32 +00:00
|
|
|
#define ISC_RATELIMITEREVENT_SHUTDOWN (ISC_EVENTCLASS_RATELIMITER + 1)
|
|
|
|
|
103. [func] libisc buffer API changes for <isc/buffer.h>:
Added:
isc_buffer_base(b) (pointer)
isc_buffer_current(b) (pointer)
isc_buffer_active(b) (pointer)
isc_buffer_used(b) (pointer)
isc_buffer_length(b) (int)
isc_buffer_usedlength(b) (int)
isc_buffer_consumedlength(b) (int)
isc_buffer_remaininglength(b) (int)
isc_buffer_activelength(b) (int)
isc_buffer_availablelength(b) (int)
Removed:
ISC_BUFFER_USEDCOUNT(b)
ISC_BUFFER_AVAILABLECOUNT(b)
isc_buffer_type(b)
Changed names:
isc_buffer_used(b, r) ->
isc_buffer_usedregion(b, r)
isc_buffer_available(b, r) ->
isc_buffer_available_region(b, r)
isc_buffer_consumed(b, r) ->
isc_buffer_consumedregion(b, r)
isc_buffer_active(b, r) ->
isc_buffer_activeregion(b, r)
isc_buffer_remaining(b, r) ->
isc_buffer_remainingregion(b, r)
Buffer types were removed, so the ISC_BUFFERTYPE_*
macros are no more, and the type argument to
isc_buffer_init and isc_buffer_allocate were removed.
isc_buffer_putstr is now void (instead of isc_result_t)
and requires that the caller ensure that there
is enough available buffer space for the string.
2000-04-27 00:03:12 +00:00
|
|
|
static void
|
|
|
|
ratelimiter_tick(isc_task_t *task, isc_event_t *event);
|
1999-11-17 01:29:44 +00:00
|
|
|
|
2000-04-26 17:10:32 +00:00
|
|
|
static void
|
2000-06-15 17:40:22 +00:00
|
|
|
ratelimiter_shutdowncomplete(isc_task_t *task, isc_event_t *event);
|
2000-04-26 17:10:32 +00:00
|
|
|
|
1999-11-17 01:29:44 +00:00
|
|
|
isc_result_t
|
|
|
|
isc_ratelimiter_create(isc_mem_t *mctx, isc_timermgr_t *timermgr,
|
|
|
|
isc_task_t *task, isc_ratelimiter_t **ratelimiterp)
|
|
|
|
{
|
|
|
|
isc_result_t result;
|
|
|
|
isc_ratelimiter_t *rl;
|
|
|
|
INSIST(ratelimiterp != NULL && *ratelimiterp == NULL);
|
|
|
|
|
|
|
|
rl = isc_mem_get(mctx, sizeof(*rl));
|
|
|
|
if (rl == NULL)
|
|
|
|
return ISC_R_NOMEMORY;
|
|
|
|
rl->mctx = mctx;
|
2000-06-15 17:40:22 +00:00
|
|
|
rl->refs = 1;
|
1999-11-17 01:29:44 +00:00
|
|
|
rl->task = task;
|
|
|
|
isc_interval_set(&rl->interval, 0, 0);
|
|
|
|
rl->timer = NULL;
|
2000-05-11 09:23:29 +00:00
|
|
|
rl->pertic = 1;
|
1999-11-17 01:29:44 +00:00
|
|
|
rl->state = isc_ratelimiter_worklimited;
|
|
|
|
ISC_LIST_INIT(rl->pending);
|
|
|
|
|
|
|
|
result = isc_mutex_init(&rl->lock);
|
|
|
|
if (result != ISC_R_SUCCESS)
|
|
|
|
goto free_mem;
|
|
|
|
result = isc_timer_create(timermgr, isc_timertype_inactive,
|
|
|
|
NULL, NULL, rl->task, ratelimiter_tick,
|
|
|
|
rl, &rl->timer);
|
|
|
|
if (result != ISC_R_SUCCESS)
|
|
|
|
goto free_mutex;
|
|
|
|
|
2000-06-15 17:40:22 +00:00
|
|
|
/*
|
|
|
|
* Increment the reference count to indicate that we may
|
|
|
|
* (soon) have events outstanding.
|
|
|
|
*/
|
|
|
|
rl->refs++;
|
|
|
|
|
2000-04-26 17:10:32 +00:00
|
|
|
ISC_EVENT_INIT(&rl->shutdownevent,
|
|
|
|
sizeof(isc_event_t),
|
|
|
|
0, NULL, ISC_RATELIMITEREVENT_SHUTDOWN,
|
|
|
|
ratelimiter_shutdowncomplete, rl, rl, NULL, NULL);
|
|
|
|
|
1999-11-17 01:29:44 +00:00
|
|
|
*ratelimiterp = rl;
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
|
|
|
|
free_mutex:
|
|
|
|
isc_mutex_destroy(&rl->lock);
|
|
|
|
free_mem:
|
|
|
|
isc_mem_put(mctx, rl, sizeof(*rl));
|
|
|
|
return (result);
|
|
|
|
}
|
|
|
|
|
|
|
|
isc_result_t
|
2000-04-25 19:32:11 +00:00
|
|
|
isc_ratelimiter_setinterval(isc_ratelimiter_t *rl, isc_interval_t *interval) {
|
1999-11-17 01:29:44 +00:00
|
|
|
isc_result_t result = ISC_R_SUCCESS;
|
|
|
|
LOCK(&rl->lock);
|
|
|
|
rl->interval = *interval;
|
|
|
|
/*
|
|
|
|
* If the timer is currently running, change its rate.
|
|
|
|
*/
|
|
|
|
if (rl->state == isc_ratelimiter_ratelimited) {
|
|
|
|
result = isc_timer_reset(rl->timer, isc_timertype_ticker, NULL,
|
|
|
|
&rl->interval, ISC_FALSE);
|
|
|
|
}
|
|
|
|
UNLOCK(&rl->lock);
|
|
|
|
return (result);
|
|
|
|
}
|
2000-05-11 09:23:29 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
isc_ratelimiter_setpertic(isc_ratelimiter_t *rl, isc_uint32_t pertic) {
|
|
|
|
if (pertic == 0)
|
|
|
|
pertic = 1;
|
|
|
|
rl->pertic = pertic;
|
|
|
|
}
|
1999-11-17 01:29:44 +00:00
|
|
|
|
|
|
|
isc_result_t
|
2000-05-18 04:43:00 +00:00
|
|
|
isc_ratelimiter_enqueue(isc_ratelimiter_t *rl, isc_task_t *task,
|
|
|
|
isc_event_t **eventp)
|
|
|
|
{
|
1999-11-17 01:29:44 +00:00
|
|
|
isc_result_t result = ISC_R_SUCCESS;
|
2000-05-18 04:43:00 +00:00
|
|
|
isc_event_t *ev;
|
|
|
|
|
|
|
|
REQUIRE(eventp != NULL && *eventp != NULL);
|
|
|
|
REQUIRE(task != NULL);
|
|
|
|
ev = *eventp;
|
|
|
|
REQUIRE(ev->ev_sender == NULL);
|
|
|
|
|
1999-11-17 01:29:44 +00:00
|
|
|
LOCK(&rl->lock);
|
|
|
|
if (rl->state == isc_ratelimiter_ratelimited) {
|
|
|
|
isc_event_t *ev = *eventp;
|
2000-05-18 04:43:00 +00:00
|
|
|
ev->ev_sender = task;
|
2000-04-17 19:22:44 +00:00
|
|
|
ISC_LIST_APPEND(rl->pending, ev, ev_link);
|
1999-11-17 01:29:44 +00:00
|
|
|
*eventp = NULL;
|
2000-04-26 17:10:32 +00:00
|
|
|
} else if (rl->state == isc_ratelimiter_worklimited) {
|
1999-11-17 01:29:44 +00:00
|
|
|
result = isc_timer_reset(rl->timer, isc_timertype_ticker, NULL,
|
|
|
|
&rl->interval, ISC_FALSE);
|
2000-05-18 04:43:00 +00:00
|
|
|
if (result == ISC_R_SUCCESS) {
|
|
|
|
ev->ev_sender = task;
|
1999-11-17 01:29:44 +00:00
|
|
|
rl->state = isc_ratelimiter_ratelimited;
|
2000-05-18 04:43:00 +00:00
|
|
|
}
|
2000-04-26 17:10:32 +00:00
|
|
|
} else {
|
|
|
|
INSIST(rl->state == isc_ratelimiter_shuttingdown);
|
|
|
|
result = ISC_R_SHUTTINGDOWN;
|
1999-11-17 01:29:44 +00:00
|
|
|
}
|
|
|
|
UNLOCK(&rl->lock);
|
2000-05-18 04:43:00 +00:00
|
|
|
if (*eventp != NULL && result == ISC_R_SUCCESS)
|
|
|
|
isc_task_send(task, eventp);
|
1999-11-17 01:29:44 +00:00
|
|
|
return (result);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2000-04-25 19:32:11 +00:00
|
|
|
ratelimiter_tick(isc_task_t *task, isc_event_t *event) {
|
1999-11-17 01:29:44 +00:00
|
|
|
isc_result_t result = ISC_R_SUCCESS;
|
2000-04-17 19:22:44 +00:00
|
|
|
isc_ratelimiter_t *rl = (isc_ratelimiter_t *)event->ev_arg;
|
1999-11-17 01:29:44 +00:00
|
|
|
isc_event_t *p;
|
2000-05-11 09:23:29 +00:00
|
|
|
isc_uint32_t pertic;
|
|
|
|
|
2000-04-26 17:10:32 +00:00
|
|
|
UNUSED(task);
|
2000-05-11 09:23:29 +00:00
|
|
|
|
1999-11-17 01:29:44 +00:00
|
|
|
isc_event_free(&event);
|
2000-05-11 09:23:29 +00:00
|
|
|
|
|
|
|
pertic = rl->pertic;
|
|
|
|
while (pertic != 0) {
|
|
|
|
pertic--;
|
|
|
|
LOCK(&rl->lock);
|
|
|
|
p = ISC_LIST_HEAD(rl->pending);
|
|
|
|
if (p != NULL) {
|
|
|
|
/*
|
|
|
|
* There is work to do. Let's do it after unlocking.
|
|
|
|
*/
|
|
|
|
ISC_LIST_UNLINK(rl->pending, p, ev_link);
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* No work left to do. Stop the timer so that we don't
|
|
|
|
* waste resources by having it fire periodically.
|
|
|
|
*/
|
2000-05-24 05:10:00 +00:00
|
|
|
result = isc_timer_reset(rl->timer,
|
|
|
|
isc_timertype_inactive,
|
2000-05-11 09:23:29 +00:00
|
|
|
NULL, NULL, ISC_FALSE);
|
|
|
|
RUNTIME_CHECK(result == ISC_R_SUCCESS);
|
|
|
|
rl->state = isc_ratelimiter_worklimited;
|
|
|
|
pertic = 0; /* Force the loop to exit. */
|
|
|
|
}
|
|
|
|
UNLOCK(&rl->lock);
|
2000-05-18 04:43:00 +00:00
|
|
|
if (p != NULL) {
|
|
|
|
isc_task_t *evtask = p->ev_sender;
|
|
|
|
isc_task_send(evtask, &p);
|
|
|
|
}
|
2000-05-11 09:23:29 +00:00
|
|
|
INSIST(p == NULL);
|
|
|
|
}
|
1999-11-17 01:29:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2000-04-26 17:10:32 +00:00
|
|
|
isc_ratelimiter_shutdown(isc_ratelimiter_t *rl) {
|
|
|
|
isc_event_t *ev;
|
2000-05-18 04:43:00 +00:00
|
|
|
isc_task_t *task;
|
2000-04-26 17:10:32 +00:00
|
|
|
LOCK(&rl->lock);
|
|
|
|
rl->state = isc_ratelimiter_shuttingdown;
|
2000-06-15 17:40:22 +00:00
|
|
|
(void)isc_timer_reset(rl->timer, isc_timertype_inactive,
|
|
|
|
NULL, NULL, ISC_FALSE);
|
2000-04-26 17:10:32 +00:00
|
|
|
while ((ev = ISC_LIST_HEAD(rl->pending)) != NULL) {
|
|
|
|
ISC_LIST_UNLINK(rl->pending, ev, ev_link);
|
|
|
|
ev->ev_attributes |= ISC_EVENTATTR_CANCELED;
|
2000-05-18 04:43:00 +00:00
|
|
|
task = ev->ev_sender;
|
|
|
|
isc_task_send(task, &ev);
|
1999-11-17 01:29:44 +00:00
|
|
|
}
|
2000-04-26 17:10:32 +00:00
|
|
|
isc_timer_detach(&rl->timer);
|
|
|
|
/*
|
2000-06-15 17:40:22 +00:00
|
|
|
* Send an event to our task. The delivery of this event
|
|
|
|
* indicates that no more timer events will be delivered.
|
2000-04-26 17:10:32 +00:00
|
|
|
*/
|
2000-06-15 17:40:22 +00:00
|
|
|
ev = &rl->shutdownevent;
|
2000-04-26 17:10:32 +00:00
|
|
|
isc_task_send(rl->task, &ev);
|
2000-06-15 17:40:22 +00:00
|
|
|
|
|
|
|
UNLOCK(&rl->lock);
|
1999-11-17 01:29:44 +00:00
|
|
|
}
|
2000-06-15 17:40:22 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
ratelimiter_shutdowncomplete(isc_task_t *task, isc_event_t *event) {
|
|
|
|
isc_ratelimiter_t *rl = (isc_ratelimiter_t *)event->ev_arg;
|
|
|
|
|
|
|
|
UNUSED(task);
|
|
|
|
|
|
|
|
isc_ratelimiter_detach(&rl);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
ratelimiter_free(isc_ratelimiter_t *rl) {
|
|
|
|
isc_mutex_destroy(&rl->lock);
|
|
|
|
isc_mem_put(rl->mctx, rl, sizeof(*rl));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
isc_ratelimiter_attach(isc_ratelimiter_t *source, isc_ratelimiter_t **target) {
|
2000-06-15 17:42:16 +00:00
|
|
|
REQUIRE(source != NULL);
|
2000-06-15 17:40:22 +00:00
|
|
|
REQUIRE(target != NULL && *target == NULL);
|
|
|
|
|
|
|
|
LOCK(&source->lock);
|
|
|
|
REQUIRE(source->refs > 0);
|
|
|
|
source->refs++;
|
|
|
|
INSIST(source->refs > 0);
|
|
|
|
UNLOCK(&source->lock);
|
|
|
|
*target = source;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
isc_ratelimiter_detach(isc_ratelimiter_t **rlp) {
|
|
|
|
isc_ratelimiter_t *rl = *rlp;
|
|
|
|
isc_boolean_t free_now = ISC_FALSE;
|
|
|
|
|
|
|
|
LOCK(&rl->lock);
|
|
|
|
REQUIRE(rl->refs > 0);
|
|
|
|
rl->refs--;
|
|
|
|
if (rl->refs == 0)
|
|
|
|
free_now = ISC_TRUE;
|
|
|
|
UNLOCK(&rl->lock);
|
|
|
|
|
|
|
|
if (free_now)
|
|
|
|
ratelimiter_free(rl);
|
|
|
|
|
|
|
|
*rlp = NULL;
|
|
|
|
}
|
|
|
|
|