pipewire/spa/include/spa/utils/ratelimit.h

44 lines
796 B
C
Raw Normal View History

2023-07-11 19:25:13 +02:00
/* Ratelimit */
/* SPDX-FileCopyrightText: Copyright © 2023 Wim Taymans */
/* SPDX-License-Identifier: MIT */
#ifndef SPA_RATELIMIT_H
#define SPA_RATELIMIT_H
#ifdef __cplusplus
extern "C" {
#endif
2023-07-11 19:56:44 +02:00
#include <inttypes.h>
2023-07-11 19:25:13 +02:00
#include <stddef.h>
struct spa_ratelimit {
uint64_t interval;
uint64_t begin;
unsigned burst;
unsigned n_printed;
unsigned n_suppressed;
2023-07-11 19:25:13 +02:00
};
static inline int spa_ratelimit_test(struct spa_ratelimit *r, uint64_t now)
{
unsigned suppressed = 0;
2023-07-11 19:25:13 +02:00
if (r->begin + r->interval < now) {
suppressed = r->n_suppressed;
2023-07-11 19:25:13 +02:00
r->begin = now;
r->n_printed = 0;
r->n_suppressed = 0;
2023-07-11 19:25:13 +02:00
} else if (r->n_printed >= r->burst) {
r->n_suppressed++;
2023-07-11 19:25:13 +02:00
return -1;
}
r->n_printed++;
return suppressed;
2023-07-11 19:25:13 +02:00
}
#ifdef __cplusplus
} /* extern "C" */
#endif
#endif /* SPA_RATELIMIT_H */