1999-11-17 01:29:44 +00:00
|
|
|
/*
|
2018-02-23 09:53:12 +01:00
|
|
|
* Copyright (C) Internet Systems Consortium, Inc. ("ISC")
|
2000-08-01 01:33:37 +00:00
|
|
|
*
|
2021-06-03 08:37:05 +02:00
|
|
|
* SPDX-License-Identifier: MPL-2.0
|
|
|
|
*
|
2016-06-27 14:56:38 +10:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
2020-09-14 16:20:40 -07:00
|
|
|
* file, you can obtain one at https://mozilla.org/MPL/2.0/.
|
2018-02-23 09:53:12 +01:00
|
|
|
*
|
|
|
|
* See the COPYRIGHT file distributed with this work for additional
|
|
|
|
* information regarding copyright ownership.
|
1999-11-17 01:29:44 +00:00
|
|
|
*/
|
|
|
|
|
2005-04-27 04:57:32 +00:00
|
|
|
/*! \file */
|
2000-06-22 22:00:42 +00:00
|
|
|
|
2018-03-28 14:19:37 +02:00
|
|
|
#include <inttypes.h>
|
2018-04-17 08:29:14 -07:00
|
|
|
#include <stdbool.h>
|
2018-03-28 14:19:37 +02:00
|
|
|
|
2022-07-26 13:03:40 +02:00
|
|
|
#include <isc/async.h>
|
|
|
|
#include <isc/event.h>
|
|
|
|
#include <isc/loop.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>
|
2019-07-15 10:27:11 +02:00
|
|
|
#include <isc/refcount.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 {
|
2022-07-26 13:03:40 +02:00
|
|
|
isc_ratelimiter_ratelimited = 0,
|
|
|
|
isc_ratelimiter_idle = 1,
|
|
|
|
isc_ratelimiter_shuttingdown = 2
|
2000-04-26 17:10:32 +00:00
|
|
|
} isc_ratelimiter_state_t;
|
|
|
|
|
1999-11-17 01:29:44 +00:00
|
|
|
struct isc_ratelimiter {
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_mem_t *mctx;
|
|
|
|
isc_mutex_t lock;
|
2022-07-26 13:03:40 +02:00
|
|
|
isc_loopmgr_t *loopmgr;
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_timer_t *timer;
|
|
|
|
isc_interval_t interval;
|
|
|
|
uint32_t pertic;
|
|
|
|
bool pushpop;
|
2020-02-12 13:59:18 +01:00
|
|
|
isc_ratelimiter_state_t state;
|
|
|
|
ISC_LIST(isc_event_t) pending;
|
1999-11-17 01:29:44 +00:00
|
|
|
};
|
|
|
|
|
2020-02-14 08:14:03 +01:00
|
|
|
static void
|
2022-07-26 13:03:40 +02:00
|
|
|
ratelimiter_tick(void *arg);
|
2000-04-26 17:10:32 +00:00
|
|
|
|
1999-11-17 01:29:44 +00:00
|
|
|
isc_result_t
|
2022-07-26 13:03:40 +02:00
|
|
|
isc_ratelimiter_create(isc_loop_t *loop, isc_ratelimiter_t **ratelimiterp) {
|
|
|
|
isc_ratelimiter_t *rl = NULL;
|
|
|
|
isc_mem_t *mctx = isc_loop_getmctx(loop);
|
|
|
|
|
1999-11-17 01:29:44 +00:00
|
|
|
INSIST(ratelimiterp != NULL && *ratelimiterp == NULL);
|
2000-08-01 01:33:37 +00:00
|
|
|
|
1999-11-17 01:29:44 +00:00
|
|
|
rl = isc_mem_get(mctx, sizeof(*rl));
|
2019-07-15 10:27:11 +02:00
|
|
|
*rl = (isc_ratelimiter_t){
|
|
|
|
.pertic = 1,
|
|
|
|
.state = isc_ratelimiter_idle,
|
|
|
|
};
|
|
|
|
|
2022-07-26 13:03:40 +02:00
|
|
|
isc_mem_attach(mctx, &rl->mctx);
|
1999-11-17 01:29:44 +00:00
|
|
|
isc_interval_set(&rl->interval, 0, 0);
|
|
|
|
ISC_LIST_INIT(rl->pending);
|
|
|
|
|
2018-11-16 15:33:22 +01:00
|
|
|
isc_mutex_init(&rl->lock);
|
2014-09-29 10:01:08 +10:00
|
|
|
|
2022-07-26 13:03:40 +02:00
|
|
|
isc_timer_create(loop, ratelimiter_tick, rl, &rl->timer);
|
2000-04-26 17:10:32 +00:00
|
|
|
|
1999-11-17 01:29:44 +00:00
|
|
|
*ratelimiterp = rl;
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
2022-07-26 13:03:40 +02:00
|
|
|
void
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_ratelimiter_setinterval(isc_ratelimiter_t *rl, isc_interval_t *interval) {
|
2014-09-29 10:01:08 +10:00
|
|
|
REQUIRE(rl != NULL);
|
|
|
|
REQUIRE(interval != NULL);
|
|
|
|
|
1999-11-17 01:29:44 +00:00
|
|
|
LOCK(&rl->lock);
|
|
|
|
rl->interval = *interval;
|
|
|
|
/*
|
|
|
|
* If the timer is currently running, change its rate.
|
|
|
|
*/
|
2012-10-24 23:46:51 +00:00
|
|
|
if (rl->state == isc_ratelimiter_ratelimited) {
|
2022-07-26 13:03:40 +02:00
|
|
|
isc_timer_start(rl->timer, isc_timertype_ticker, &rl->interval);
|
1999-11-17 01:29:44 +00:00
|
|
|
}
|
|
|
|
UNLOCK(&rl->lock);
|
|
|
|
}
|
2000-05-11 09:23:29 +00:00
|
|
|
|
|
|
|
void
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_ratelimiter_setpertic(isc_ratelimiter_t *rl, uint32_t pertic) {
|
2014-09-29 10:01:08 +10:00
|
|
|
REQUIRE(rl != NULL);
|
2022-07-26 13:03:40 +02:00
|
|
|
REQUIRE(pertic > 0);
|
2014-09-29 10:01:08 +10:00
|
|
|
|
2022-07-26 13:03:40 +02:00
|
|
|
LOCK(&rl->lock);
|
2000-05-11 09:23:29 +00:00
|
|
|
rl->pertic = pertic;
|
2022-07-26 13:03:40 +02:00
|
|
|
UNLOCK(&rl->lock);
|
2000-05-11 09:23:29 +00:00
|
|
|
}
|
2000-08-01 01:33:37 +00:00
|
|
|
|
2016-08-12 11:33:48 +10:00
|
|
|
void
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_ratelimiter_setpushpop(isc_ratelimiter_t *rl, bool pushpop) {
|
2016-08-12 11:33:48 +10:00
|
|
|
REQUIRE(rl != NULL);
|
|
|
|
|
2022-07-26 13:03:40 +02:00
|
|
|
LOCK(&rl->lock);
|
2016-08-12 11:33:48 +10:00
|
|
|
rl->pushpop = pushpop;
|
2022-07-26 13:03:40 +02:00
|
|
|
UNLOCK(&rl->lock);
|
2016-08-12 11:33:48 +10:00
|
|
|
}
|
|
|
|
|
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,
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_event_t **eventp) {
|
1999-11-17 01:29:44 +00:00
|
|
|
isc_result_t result = ISC_R_SUCCESS;
|
2022-07-26 13:03:40 +02:00
|
|
|
isc_event_t *event;
|
2000-05-18 04:43:00 +00:00
|
|
|
|
2014-09-29 10:01:08 +10:00
|
|
|
REQUIRE(rl != NULL);
|
2000-05-18 04:43:00 +00:00
|
|
|
REQUIRE(task != NULL);
|
2014-09-29 10:01:08 +10:00
|
|
|
REQUIRE(eventp != NULL && *eventp != NULL);
|
2022-07-26 13:03:40 +02:00
|
|
|
event = *eventp;
|
|
|
|
REQUIRE(event->ev_sender == NULL);
|
2000-05-18 04:43:00 +00:00
|
|
|
|
1999-11-17 01:29:44 +00:00
|
|
|
LOCK(&rl->lock);
|
2022-07-26 13:03:40 +02:00
|
|
|
switch (rl->state) {
|
|
|
|
case isc_ratelimiter_shuttingdown:
|
|
|
|
result = ISC_R_SHUTTINGDOWN;
|
|
|
|
break;
|
|
|
|
case isc_ratelimiter_ratelimited:
|
|
|
|
event->ev_sender = task;
|
1999-11-17 01:29:44 +00:00
|
|
|
*eventp = NULL;
|
2020-02-13 21:48:23 +01:00
|
|
|
if (rl->pushpop) {
|
2022-07-26 13:03:40 +02:00
|
|
|
ISC_LIST_PREPEND(rl->pending, event, ev_ratelink);
|
2020-02-13 21:48:23 +01:00
|
|
|
} else {
|
2022-07-26 13:03:40 +02:00
|
|
|
ISC_LIST_APPEND(rl->pending, event, ev_ratelink);
|
2000-05-18 04:43:00 +00:00
|
|
|
}
|
2022-07-26 13:03:40 +02:00
|
|
|
break;
|
|
|
|
case isc_ratelimiter_idle:
|
|
|
|
isc_timer_start(rl->timer, isc_timertype_ticker, &rl->interval);
|
|
|
|
event->ev_sender = task;
|
|
|
|
rl->state = isc_ratelimiter_ratelimited;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
1999-11-17 01:29:44 +00:00
|
|
|
}
|
|
|
|
UNLOCK(&rl->lock);
|
2020-02-13 21:48:23 +01:00
|
|
|
if (*eventp != NULL && result == ISC_R_SUCCESS) {
|
2000-05-18 04:43:00 +00:00
|
|
|
isc_task_send(task, eventp);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
1999-11-17 01:29:44 +00:00
|
|
|
return (result);
|
|
|
|
}
|
|
|
|
|
2014-09-29 10:01:08 +10:00
|
|
|
isc_result_t
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_ratelimiter_dequeue(isc_ratelimiter_t *rl, isc_event_t *event) {
|
2014-09-29 10:01:08 +10:00
|
|
|
isc_result_t result = ISC_R_SUCCESS;
|
|
|
|
|
|
|
|
REQUIRE(rl != NULL);
|
|
|
|
REQUIRE(event != NULL);
|
|
|
|
|
|
|
|
LOCK(&rl->lock);
|
2017-01-12 14:12:05 +11:00
|
|
|
if (ISC_LINK_LINKED(event, ev_ratelink)) {
|
|
|
|
ISC_LIST_UNLINK(rl->pending, event, ev_ratelink);
|
2015-01-22 10:38:40 +11:00
|
|
|
event->ev_sender = NULL;
|
2020-02-13 21:48:23 +01:00
|
|
|
} else {
|
2014-09-29 10:01:08 +10:00
|
|
|
result = ISC_R_NOTFOUND;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2022-07-26 13:03:40 +02:00
|
|
|
|
|
|
|
if (ISC_LIST_EMPTY(rl->pending)) {
|
|
|
|
/* No work left to do. Stop the timer. */
|
|
|
|
isc_timer_stop(rl->timer);
|
|
|
|
rl->state = isc_ratelimiter_idle;
|
|
|
|
}
|
2014-09-29 10:01:08 +10:00
|
|
|
UNLOCK(&rl->lock);
|
2022-07-26 13:03:40 +02:00
|
|
|
|
2014-09-29 10:01:08 +10:00
|
|
|
return (result);
|
|
|
|
}
|
|
|
|
|
1999-11-17 01:29:44 +00:00
|
|
|
static void
|
2022-07-26 13:03:40 +02:00
|
|
|
ratelimiter_tick(void *arg) {
|
|
|
|
isc_ratelimiter_t *rl = (isc_ratelimiter_t *)arg;
|
|
|
|
isc_event_t *event;
|
2020-02-13 14:44:37 -08:00
|
|
|
uint32_t pertic;
|
2000-05-11 09:23:29 +00:00
|
|
|
|
|
|
|
pertic = rl->pertic;
|
2012-10-24 23:46:51 +00:00
|
|
|
while (pertic != 0) {
|
2000-05-11 09:23:29 +00:00
|
|
|
pertic--;
|
|
|
|
LOCK(&rl->lock);
|
2022-07-26 13:03:40 +02:00
|
|
|
event = ISC_LIST_HEAD(rl->pending);
|
|
|
|
if (event != NULL) {
|
2000-05-11 09:23:29 +00:00
|
|
|
/*
|
|
|
|
* There is work to do. Let's do it after unlocking.
|
|
|
|
*/
|
2022-07-26 13:03:40 +02:00
|
|
|
ISC_LIST_UNLINK(rl->pending, event, ev_ratelink);
|
2000-05-11 09:23:29 +00:00
|
|
|
} else {
|
2022-07-26 13:03:40 +02:00
|
|
|
/* No work left to do. Stop the timer. */
|
|
|
|
isc_timer_stop(rl->timer);
|
2002-09-08 18:35:20 +00:00
|
|
|
rl->state = isc_ratelimiter_idle;
|
2020-02-12 13:59:18 +01:00
|
|
|
pertic = 0; /* Force the loop to exit. */
|
2000-05-11 09:23:29 +00:00
|
|
|
}
|
|
|
|
UNLOCK(&rl->lock);
|
2022-07-26 13:03:40 +02:00
|
|
|
if (event != NULL) {
|
|
|
|
isc_task_send(event->ev_sender, &event);
|
2000-05-18 04:43:00 +00:00
|
|
|
}
|
2022-07-26 13:03:40 +02:00
|
|
|
INSIST(event == NULL);
|
2000-05-11 09:23:29 +00:00
|
|
|
}
|
1999-11-17 01:29:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_ratelimiter_shutdown(isc_ratelimiter_t *rl) {
|
2022-07-26 13:03:40 +02:00
|
|
|
isc_event_t *event;
|
2014-09-29 10:01:08 +10:00
|
|
|
|
|
|
|
REQUIRE(rl != NULL);
|
|
|
|
|
2000-04-26 17:10:32 +00:00
|
|
|
LOCK(&rl->lock);
|
|
|
|
rl->state = isc_ratelimiter_shuttingdown;
|
2022-07-26 13:03:40 +02:00
|
|
|
isc_timer_stop(rl->timer);
|
2022-04-02 00:42:20 +02:00
|
|
|
isc_timer_destroy(&rl->timer);
|
2014-09-29 10:01:08 +10:00
|
|
|
|
2022-07-26 13:03:40 +02:00
|
|
|
while ((event = ISC_LIST_HEAD(rl->pending)) != NULL) {
|
|
|
|
ISC_LIST_UNLINK(rl->pending, event, ev_ratelink);
|
|
|
|
UNLOCK(&rl->lock);
|
2000-06-15 17:40:22 +00:00
|
|
|
|
2022-07-26 13:03:40 +02:00
|
|
|
event->ev_attributes |= ISC_EVENTATTR_CANCELED;
|
|
|
|
isc_task_send(event->ev_sender, &event);
|
2000-06-15 17:40:22 +00:00
|
|
|
|
2022-07-26 13:03:40 +02:00
|
|
|
LOCK(&rl->lock);
|
|
|
|
}
|
2019-07-15 10:27:11 +02:00
|
|
|
|
2022-07-26 13:03:40 +02:00
|
|
|
UNLOCK(&rl->lock);
|
2000-06-15 17:40:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2022-07-26 13:03:40 +02:00
|
|
|
isc_ratelimiter_destroy(isc_ratelimiter_t **rlp) {
|
2014-09-29 10:01:08 +10:00
|
|
|
isc_ratelimiter_t *rl;
|
2000-06-15 17:40:22 +00:00
|
|
|
|
2014-09-29 10:01:08 +10:00
|
|
|
REQUIRE(rlp != NULL && *rlp != NULL);
|
|
|
|
|
|
|
|
rl = *rlp;
|
2020-02-08 04:37:54 -08:00
|
|
|
*rlp = NULL;
|
2014-09-29 23:45:24 +00:00
|
|
|
|
2002-07-26 06:42:02 +00:00
|
|
|
LOCK(&rl->lock);
|
2022-07-26 13:03:40 +02:00
|
|
|
REQUIRE(rl->state == isc_ratelimiter_shuttingdown);
|
2002-07-26 06:42:02 +00:00
|
|
|
UNLOCK(&rl->lock);
|
|
|
|
|
2022-07-26 13:03:40 +02:00
|
|
|
isc_mutex_destroy(&rl->lock);
|
|
|
|
isc_mem_putanddetach(&rl->mctx, rl, sizeof(*rl));
|
2002-07-26 06:42:02 +00:00
|
|
|
}
|