summaryrefslogtreecommitdiffstats
path: root/src/journal/journald-rate-limit.c
diff options
context:
space:
mode:
authorYu Watanabe <watanabe.yu+github@gmail.com>2024-05-11 14:05:56 +0200
committerYu Watanabe <watanabe.yu+github@gmail.com>2024-05-12 03:32:14 +0200
commit8df477675d2b267a14ec946bf7fbaae65b8df40e (patch)
tree3ce61b186aedfbe880e480d02e297fdf2fc8b79e /src/journal/journald-rate-limit.c
parentjournal-rate-limit: introduce cleanup function for JournalRateLimitGroup (diff)
downloadsystemd-8df477675d2b267a14ec946bf7fbaae65b8df40e.tar.xz
systemd-8df477675d2b267a14ec946bf7fbaae65b8df40e.zip
journal-rate-limit: split out journal_ratelimit_group_acquire()
No functional change, just refactoring.
Diffstat (limited to 'src/journal/journald-rate-limit.c')
-rw-r--r--src/journal/journald-rate-limit.c55
1 files changed, 37 insertions, 18 deletions
diff --git a/src/journal/journald-rate-limit.c b/src/journal/journald-rate-limit.c
index e4e3728125..afba668e0f 100644
--- a/src/journal/journald-rate-limit.c
+++ b/src/journal/journald-rate-limit.c
@@ -162,6 +162,38 @@ static int journal_ratelimit_group_new(
return 0;
}
+static int journal_ratelimit_group_acquire(
+ JournalRateLimit *rl,
+ const char *id,
+ usec_t interval,
+ usec_t ts,
+ JournalRateLimitGroup **ret) {
+
+ JournalRateLimitGroup *head, *g = NULL;
+ uint64_t h;
+
+ assert(rl);
+ assert(id);
+ assert(ret);
+
+ h = siphash24_string(id, rl->hash_key);
+ head = rl->buckets[h % BUCKETS_MAX];
+
+ LIST_FOREACH(bucket, i, head)
+ if (streq(i->id, id)) {
+ g = i;
+ break;
+ }
+
+ if (!g)
+ return journal_ratelimit_group_new(rl, id, interval, ts, ret);
+
+ g->interval = interval;
+
+ *ret = g;
+ return 0;
+}
+
static unsigned burst_modulate(unsigned burst, uint64_t available) {
unsigned k;
@@ -198,10 +230,9 @@ int journal_ratelimit_test(
int priority,
uint64_t available) {
- JournalRateLimitGroup *g, *found = NULL;
+ JournalRateLimitGroup *g;
JournalRateLimitPool *p;
unsigned burst;
- uint64_t h;
usec_t ts;
int r;
@@ -219,28 +250,16 @@ int journal_ratelimit_test(
ts = now(CLOCK_MONOTONIC);
- h = siphash24_string(id, rl->hash_key);
- g = rl->buckets[h % BUCKETS_MAX];
-
- LIST_FOREACH(bucket, i, g)
- if (streq(i->id, id)) {
- found = i;
- break;
- }
-
- if (!found) {
- r = journal_ratelimit_group_new(rl, id, rl_interval, ts, &found);
- if (r < 0)
- return r;
- } else
- found->interval = rl_interval;
+ r = journal_ratelimit_group_acquire(rl, id, rl_interval, ts, &g);
+ if (r < 0)
+ return r;
if (rl_interval == 0 || rl_burst == 0)
return 1;
burst = burst_modulate(rl_burst, available);
- p = &found->pools[priority_map[priority]];
+ p = &g->pools[priority_map[priority]];
if (p->begin <= 0) {
p->suppressed = 0;