-
Notifications
You must be signed in to change notification settings - Fork 38
Dan sliding window #42
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
void SlidingWindow<Message>::Send(int clientId, const Message& message) | ||
{ | ||
if (mClientBuffers.count(clientId) == 0) | ||
mClientBuffers.insert(std::pair<int, Buffer<Message> >(clientId, Buffer<Message>(mWindowConfig))); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
emplace
if (mClientBuffers.count(clientId) == 0) | ||
mClientBuffers.insert(std::pair<int, Buffer<Message> >(clientId, Buffer<Message>(mWindowConfig))); | ||
|
||
Buffer<Message>& buffer = mClientBuffers.at(clientId); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are doing the lookup twice
|
||
private: | ||
WindowConfig mWindowConfig; | ||
std::map<int, Buffer<Message> > mClientBuffers; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if it doesnt need to be ordered, its better to use unordered_map
public: | ||
using value_type = Message; | ||
|
||
SlidingWindow(const WindowConfig& conf); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
explicit
|
||
SlidingWindow(const WindowConfig& conf); | ||
void Send(int clientId, const Message& message); | ||
std::vector<Message> DumpMessages(int clientId) const; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is potentially a very expensive operation as you would copy all messages. A better patern is to visit elements (visitor).
public: | ||
using value_type = Message; | ||
|
||
Buffer(const WindowConfig& conf); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
explicit
}; | ||
|
||
template<class Message> | ||
class Buffer |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that this Buffer is effectively a sliding window buffer, and the other Sliding Window class is the per user throttler
bool CanAddMessage(std::chrono::time_point<std::chrono::steady_clock> currentTime) const; | ||
|
||
WindowConfig mWindowConfig; | ||
std::deque<TimestampedMessage<Message> > mTimestampedMessages; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not, although it is not mandatory to copy the messages themselves if you only want to throttle. You only need the timestamps. Depending on the type, the message can be quite big and expensive to copy.
|
||
struct WindowConfig | ||
{ | ||
std::chrono::seconds mDuration = std::chrono::seconds(10); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Glad you didnt use time_t - you can also use chrono literals to write = 10s
mWindowConfig(conf) | ||
{ | ||
if (conf.mMaxNumMessages <= 0) | ||
throw std::runtime_error("Invalid configuration: max number of messages per time window must be positive!"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good that you check this, although a better place should be in WindowConfig's ctor.
No description provided.