#include "Governor.h" namespace bts::orderman { Governor::Governor( uint32_t max_msgs_window, uint32_t window_duration_msecs, TimeStamp now) : _max_msgs_window(max_msgs_window), _window_duration_msecs(window_duration_msecs), _start_of_window(now), _msgs_in_window(0) {} bool Governor::can_send(TimeStamp now) const { if (in_window_(now)) return _msgs_in_window < _max_msgs_window; else return true; } void Governor::record_sent(TimeStamp now) { if (!in_window_(now)) { _start_of_window = now; _msgs_in_window = 0; } _msgs_in_window++; } bool Governor::in_window_(TimeStamp now) const { return ms_diff(now, _start_of_window) <= _window_duration_msecs; } } // namespace bts::orderman