https://github.com/tomaszrewak/simple-throttler
A simple and fast C++ throttler
https://github.com/tomaszrewak/simple-throttler
cpp fast throttler
Last synced: 15 days ago
JSON representation
A simple and fast C++ throttler
- Host: GitHub
- URL: https://github.com/tomaszrewak/simple-throttler
- Owner: TomaszRewak
- License: mit
- Created: 2019-03-02T20:02:31.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2019-03-03T06:45:14.000Z (over 7 years ago)
- Last Synced: 2025-02-23T16:53:21.695Z (over 1 year ago)
- Topics: cpp, fast, throttler
- Language: C++
- Size: 16.6 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Simple-Throttler
A simple C++ throttler implementation.
This throttler supports sending messages from multiple users. Each user can send only a certain number of messages within the period defined by the sliding window.
Basic usage:
```cpp
auto throttler = make_message_throttler(
max_number_of_messages_per_user,
sliding_window_width,
message_consumer
);
throttler.from(1).send("Message 1");
throttler.from(1).send("Message 2");
throttler.from(2).send("Message 3");
```
or
```cpp
auto throttler = make_message_throttler(
max_number_of_messages_per_user,
sliding_window_width,
message_consumer
);
auto& first_client_interface = throttler.from(1);
first_client_interface.send("Message 1")
first_client_interface.send("Message 2");
auto& second_client_interface = throttler.from(2);
second_client_interface.send("Message 3")
second_client_interface.send("Message 4");
```
To be more specific:
```cpp
#include "message_throttler_commons.hpp"
...
auto throttler = make_message_throttler(
4,
std::chrono::milliseconds{ 1000 },
[&](const std::string& message) { std::cout << "Consumed: " << message << endl; }
[&](const std::string& message) { std::cout << "Disposed: " << message << endl; }
);
throttler.from(1).send("Hello");
```
Properties:
- I've separated single client interface from the main throttler. Thanks to this if one client wants to send multiple messages only one map lookup is required.
- Throttler supports custom message types and client id types.
- Complexity of the `send` method (of the client interface) is guaranteed to be O(1) no matter how long the sliding window is and how many messages are already registered in it. Also, after sending N (= sliding window size) messages the list of instructions performed to send [dispose] a message is always the same (which should make it easy for branch prediction algorithm).
- `send` method (of the client interface) never allocates.
- User can define his own data consumers (any callable functor will do). He can also listen for discarded messages.
- By default (defined in `message_throttler_commons.hpp`) throttler uses chrono for timestamping. This behavior can be overwritten with any custom clock.
- Calling the throttler's `from` method hashes the clientId only once. Map lookup is also performed only once, even if given client has not been registered yet.
This is a header only component. Apart from standard libs, it depends on boost's circular_buffer.