PipeWire 0.3.77
Loading...
Searching...
No Matches
ratelimit.h
1/* Ratelimit */
2/* SPDX-FileCopyrightText: Copyright © 2023 Wim Taymans */
3/* SPDX-License-Identifier: MIT */
4
5#ifndef SPA_RATELIMIT_H
6#define SPA_RATELIMIT_H
7
8#ifdef __cplusplus
9extern "C" {
10#endif
11
12#include <inttypes.h>
13#include <stddef.h>
14
15struct spa_ratelimit {
16 uint64_t interval;
17 uint64_t begin;
18 unsigned burst;
19 unsigned n_printed;
20 unsigned n_missed;
21};
23static inline int spa_ratelimit_test(struct spa_ratelimit *r, uint64_t now)
25 unsigned missed = 0;
26 if (r->begin + r->interval < now) {
27 missed = r->n_missed;
28 r->begin = now;
29 r->n_printed = 0;
30 r->n_missed = 0;
31 } else if (r->n_printed >= r->burst) {
32 r->n_missed++;
33 return -1;
34 }
35 r->n_printed++;
36 return missed;
37}
38
39#ifdef __cplusplus
40} /* extern "C" */
41#endif
42
43#endif /* SPA_RATELIMIT_H */
Definition: ratelimit.h:19
unsigned n_missed
Definition: ratelimit.h:24
uint64_t interval
Definition: ratelimit.h:20
uint64_t begin
Definition: ratelimit.h:21
unsigned burst
Definition: ratelimit.h:22
unsigned n_printed
Definition: ratelimit.h:23