From 8df477675d2b267a14ec946bf7fbaae65b8df40e Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Sat, 11 May 2024 21:05:56 +0900 Subject: journal-rate-limit: split out journal_ratelimit_group_acquire() No functional change, just refactoring. --- src/journal/journald-rate-limit.c | 55 ++++++++++++++++++++++++++------------- 1 file changed, 37 insertions(+), 18 deletions(-) (limited to 'src') 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; -- cgit v1.2.3