Drop RATELIMIT macros

Using plain structure initialization is both shorter _and_ more clearer.
We get type safety for free.
This commit is contained in:
Zbigniew Jędrzejewski-Szmek 2019-09-19 17:41:20 +02:00
parent 90b059b608
commit 8c227e7f2b
11 changed files with 20 additions and 45 deletions

View File

@ -7,34 +7,14 @@
#include "util.h"
typedef struct RateLimit {
usec_t interval;
usec_t begin;
unsigned burst;
usec_t interval; /* Keep those two fields first so they can be initialized easily: */
unsigned burst; /* RateLimit rl = { INTERVAL, BURST }; */
unsigned num;
usec_t begin;
} RateLimit;
#define RATELIMIT_DEFINE(_name, _interval, _burst) \
RateLimit _name = { \
.interval = (_interval), \
.burst = (_burst), \
.num = 0, \
.begin = 0 \
}
#define RATELIMIT_INIT(v, _interval, _burst) \
do { \
RateLimit *_r = &(v); \
_r->interval = (_interval); \
_r->burst = (_burst); \
_r->num = 0; \
_r->begin = 0; \
} while (false)
#define RATELIMIT_RESET(v) \
do { \
RateLimit *_r = &(v); \
_r->num = 0; \
_r->begin = 0; \
} while (false)
static inline void ratelimit_reset(RateLimit *rl) {
rl->num = rl->begin = 0;
}
bool ratelimit_below(RateLimit *r);

View File

@ -815,7 +815,7 @@ int manager_new(UnitFileScope scope, ManagerTestRunFlags test_run_flags, Manager
}
/* Reboot immediately if the user hits C-A-D more often than 7x per 2s */
RATELIMIT_INIT(m->ctrl_alt_del_ratelimit, 2 * USEC_PER_SEC, 7);
m->ctrl_alt_del_ratelimit = (RateLimit) { .interval = 2 * USEC_PER_SEC, .burst = 7 };
r = manager_default_environment(m);
if (r < 0)
@ -2855,10 +2855,9 @@ static int manager_dispatch_jobs_in_progress(sd_event_source *source, usec_t use
}
int manager_loop(Manager *m) {
RateLimit rl = { .interval = 1*USEC_PER_SEC, .burst = 50000 };
int r;
RATELIMIT_DEFINE(rl, 1*USEC_PER_SEC, 50000);
assert(m);
assert(m->objective == MANAGER_OK); /* Ensure manager_startup() has been called */

View File

@ -123,8 +123,8 @@ Unit *unit_new(Manager *m, size_t size) {
u->last_section_private = -1;
RATELIMIT_INIT(u->start_limit, m->default_start_limit_interval, m->default_start_limit_burst);
RATELIMIT_INIT(u->auto_stop_ratelimit, 10 * USEC_PER_SEC, 16);
u->start_limit = (RateLimit) { m->default_start_limit_interval, m->default_start_limit_burst };
u->auto_stop_ratelimit = (RateLimit) { 10 * USEC_PER_SEC, 16 };
for (CGroupIOAccountingMetric i = 0; i < _CGROUP_IO_ACCOUNTING_METRIC_MAX; i++)
u->io_accounting_last[i] = UINT64_MAX;
@ -4000,7 +4000,7 @@ void unit_reset_failed(Unit *u) {
if (UNIT_VTABLE(u)->reset_failed)
UNIT_VTABLE(u)->reset_failed(u);
RATELIMIT_RESET(u->start_limit);
ratelimit_reset(&u->start_limit);
u->start_limit_hit = false;
}

View File

@ -96,10 +96,9 @@ int raw_export_new(
.on_finished = on_finished,
.userdata = userdata,
.last_percent = (unsigned) -1,
.progress_rate_limit = { 100 * USEC_PER_MSEC, 1 },
};
RATELIMIT_INIT(e->progress_rate_limit, 100 * USEC_PER_MSEC, 1);
if (event)
e->event = sd_event_ref(event);
else {

View File

@ -99,10 +99,9 @@ int tar_export_new(
.userdata = userdata,
.quota_referenced = (uint64_t) -1,
.last_percent = (unsigned) -1,
.progress_rate_limit = { 100 * USEC_PER_MSEC, 1 },
};
RATELIMIT_INIT(e->progress_rate_limit, 100 * USEC_PER_MSEC, 1);
if (event)
e->event = sd_event_ref(event);
else {

View File

@ -169,7 +169,7 @@ static int import_fs(int argc, char *argv[], void *userdata) {
(void) mkdir_parents_label(temp_path, 0700);
RATELIMIT_INIT(progress.limit, 200*USEC_PER_MSEC, 1);
progress.limit = (RateLimit) { 200*USEC_PER_MSEC, 1 };
/* Hook into SIGINT/SIGTERM, so that we can cancel things then */
assert(sigaction(SIGINT, &sa, &old_sigint_sa) >= 0);

View File

@ -111,10 +111,9 @@ int raw_import_new(
.userdata = userdata,
.last_percent = (unsigned) -1,
.image_root = TAKE_PTR(root),
.progress_rate_limit = { 100 * USEC_PER_MSEC, 1 },
};
RATELIMIT_INIT(i->progress_rate_limit, 100 * USEC_PER_MSEC, 1);
if (event)
i->event = sd_event_ref(event);
else {

View File

@ -119,10 +119,9 @@ int tar_import_new(
.userdata = userdata,
.last_percent = (unsigned) -1,
.image_root = TAKE_PTR(root),
.progress_rate_limit = { 100 * USEC_PER_MSEC, 1 },
};
RATELIMIT_INIT(i->progress_rate_limit, 100 * USEC_PER_MSEC, 1);
if (event)
i->event = sd_event_ref(event);
else {

View File

@ -70,7 +70,7 @@ int dns_scope_new(Manager *m, DnsScope **ret, Link *l, DnsProtocol protocol, int
log_debug("New scope on link %s, protocol %s, family %s", l ? l->ifname : "*", dns_protocol_to_string(protocol), family == AF_UNSPEC ? "*" : af_to_name(family));
/* Enforce ratelimiting for the multicast protocols */
RATELIMIT_INIT(s->ratelimit, MULTICAST_RATELIMIT_INTERVAL_USEC, MULTICAST_RATELIMIT_BURST);
s->ratelimit = (RateLimit) { MULTICAST_RATELIMIT_INTERVAL_USEC, MULTICAST_RATELIMIT_BURST };
*ret = s;
return 0;

View File

@ -8,7 +8,7 @@
static void test_ratelimit_below(void) {
int i;
RATELIMIT_DEFINE(ratelimit, 1 * USEC_PER_SEC, 10);
RateLimit ratelimit = { 1 * USEC_PER_SEC, 10 };
for (i = 0; i < 10; i++)
assert_se(ratelimit_below(&ratelimit));
@ -17,7 +17,7 @@ static void test_ratelimit_below(void) {
for (i = 0; i < 10; i++)
assert_se(ratelimit_below(&ratelimit));
RATELIMIT_INIT(ratelimit, 0, 10);
ratelimit = (RateLimit) { 0, 10 };
for (i = 0; i < 10000; i++)
assert_se(ratelimit_below(&ratelimit));
}

View File

@ -1094,7 +1094,7 @@ int manager_new(Manager **ret) {
m->server_socket = m->clock_watch_fd = -1;
RATELIMIT_INIT(m->ratelimit, RATELIMIT_INTERVAL_USEC, RATELIMIT_BURST);
m->ratelimit = (RateLimit) { RATELIMIT_INTERVAL_USEC, RATELIMIT_BURST };
r = sd_event_default(&m->event);
if (r < 0)