+ +
2da5fe7 OpenAVX v0.2.0 - High-performance open-standard IP audio and video transport suite
saifmukhtar · 2026-11-24
c · 53 lines · 982 B · SPDX: MIT
main
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 #include <inttypes.h>
9 #include <stddef.h>
10  
11 #include <spa/utils/defs.h>
12  
13 #ifdef __cplusplus
14 extern "C" {
15 #endif
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  
33 SPA_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 */