PipeWire  1.4.5
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 
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 */
spa/utils/defs.h
uint32_t int int const char int r
Definition: core.h:445
Definition: ratelimit.h:31
unsigned n_suppressed
Definition: ratelimit.h:36
uint64_t interval
Definition: ratelimit.h:32
uint64_t begin
Definition: ratelimit.h:33
unsigned burst
Definition: ratelimit.h:34
unsigned n_printed
Definition: ratelimit.h:35