Skip to content

Conversation

D3LL15
Copy link

@D3LL15 D3LL15 commented Feb 9, 2019

No description provided.

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)));
Copy link
Owner

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);
Copy link
Owner

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;
Copy link
Owner

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);
Copy link
Owner

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;
Copy link
Owner

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);
Copy link
Owner

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
Copy link
Owner

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;
Copy link
Owner

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);
Copy link
Owner

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!");
Copy link
Owner

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.

@david-grs david-grs merged commit 316c1b0 into david-grs:master Sep 18, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants