PipeWire 1.4.10

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
9 extern "C" {
10#endif
11
12#include <inttypes.h>
13#include <stddef.h>
14
15#include <spa/utils/defs.h>
16
17#ifndef SPA_API_RATELIMIT
18 #ifdef SPA_API_IMPL
19 #define SPA_API_RATELIMIT SPA_API_IMPL
20 #else
21 #define SPA_API_RATELIMIT static inline
22 #endif
23#endif
24
25 struct spa_ratelimit {
26 uint64_t interval;
27 uint64_t begin;
28 unsigned burst;
29 unsigned n_printed;
30 unsigned n_suppressed;
31};
32
33SPA_API_RATELIMIT int spa_ratelimit_test(struct spa_ratelimit *r, uint64_t now)
34{
35 unsigned suppressed = 0;
36 if (r->begin + r->interval < now) {
37 suppressed = r->n_suppressed;
38 r->begin = now;
39 r->n_printed = 0;
40 r->n_suppressed = 0;
41 } else if (r->n_printed >= r->burst) {
42 r->n_suppressed++;
43 return -1;
44 }
45 r->n_printed++;
46 return suppressed;
47}
48
49#ifdef __cplusplus
50} /* extern "C" */
51#endif
52
53#endif /* SPA_RATELIMIT_H */
defs.h
spa/utils/defs.h
r
uint32_t int int const char int r
Definition core.h:447
spa_ratelimit
Definition ratelimit.h:31
spa_ratelimit::n_suppressed
unsigned n_suppressed
Definition ratelimit.h:36
spa_ratelimit::interval
uint64_t interval
Definition ratelimit.h:32
spa_ratelimit::begin
uint64_t begin
Definition ratelimit.h:33
spa_ratelimit::burst
unsigned burst
Definition ratelimit.h:34
spa_ratelimit::n_printed
unsigned n_printed
Definition ratelimit.h:35