Roc Toolkit internal modules
Roc Toolkit: real-time audio streaming
rate_limiter.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017 Roc Streaming authors
3  *
4  * This Source Code Form is subject to the terms of the Mozilla Public
5  * License, v. 2.0. If a copy of the MPL was not distributed with this
6  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7  */
8 
9 //! @file roc_core/rate_limiter.h
10 //! @brief Rate limiter.
11 
12 #ifndef ROC_CORE_RATE_LIMITER_H_
13 #define ROC_CORE_RATE_LIMITER_H_
14 
15 #include "roc_core/noncopyable.h"
16 #include "roc_core/ticker.h"
17 
18 namespace roc {
19 namespace core {
20 
21 //! Rate limiter.
22 class RateLimiter : public NonCopyable<> {
23 public:
24  //! Initialize rate limiter.
25  //! @remarks
26  //! @p period is tick duration in nanoseconds.
27  explicit RateLimiter(nanoseconds_t period);
28 
29  //! Check whether allow() would succeed.
30  bool would_allow();
31 
32  //! Check whether an event is allowed to occur now, and if yes, mark it as occurred.
33  bool allow();
34 
35 private:
36  const Ticker::ticks_t period_;
37  Ticker::ticks_t pos_;
38  Ticker ticker_;
39 };
40 
41 } // namespace core
42 } // namespace roc
43 
44 #endif // ROC_CORE_RATE_LIMITER_H_
Base class for non-copyable objects.
Definition: noncopyable.h:23
RateLimiter(nanoseconds_t period)
Initialize rate limiter.
bool allow()
Check whether an event is allowed to occur now, and if yes, mark it as occurred.
bool would_allow()
Check whether allow() would succeed.
Ticker.
Definition: ticker.h:23
uint64_t ticks_t
Number of ticks.
Definition: ticker.h:26
int64_t nanoseconds_t
Nanoseconds.
Definition: time.h:58
Root namespace.
Non-copyable object.
Ticker.