polybar/include/utils/throttle.hpp

93 lines
2.0 KiB
C++
Raw Normal View History

2016-06-15 03:32:35 +00:00
#pragma once
2016-11-20 22:04:31 +00:00
#include <chrono>
2016-11-25 07:42:31 +00:00
#include <deque>
2016-06-15 03:32:35 +00:00
#include "common.hpp"
#include "components/logger.hpp"
2016-12-09 08:02:47 +00:00
#include "utils/factory.hpp"
2016-06-15 03:32:35 +00:00
2016-11-19 05:22:44 +00:00
POLYBAR_NS
2016-06-15 03:32:35 +00:00
2016-11-20 22:04:31 +00:00
namespace chrono = std::chrono;
2016-06-15 03:32:35 +00:00
namespace throttle_util {
using timewindow = chrono::duration<double, std::milli>;
using timepoint_clock = chrono::high_resolution_clock;
using timepoint = timepoint_clock::time_point;
using queue = std::deque<timepoint>;
using limit = size_t;
namespace strategy {
struct try_once_or_leave_yolo {
2016-11-02 19:22:45 +00:00
bool operator()(queue& q, limit l, timewindow);
2016-06-15 03:32:35 +00:00
};
struct wait_patiently_by_the_door {
2016-11-02 19:22:45 +00:00
bool operator()(queue& q, limit l, timewindow);
2016-06-15 03:32:35 +00:00
};
}
/**
* Throttle events within a set window of time
*
* Example usage:
* @code cpp
* auto t = throttle_util::make_throttler(2, 1s);
* if (t->passthrough())
* ...
* @endcode
*/
class event_throttler {
public:
/**
* Construct throttler
*/
2016-11-25 07:42:31 +00:00
explicit event_throttler(int limit, timewindow timewindow) : m_limit(limit), m_timewindow(timewindow) {}
2016-06-15 03:32:35 +00:00
/**
* Check if event is allowed to pass
* using specified strategy
*/
template <typename Strategy>
bool passthrough(Strategy wait_strategy) {
expire_timestamps();
return wait_strategy(m_queue, m_limit, m_timewindow);
}
/**
* Check if event is allowed to pass
* using default strategy
*/
bool passthrough() {
return passthrough(strategy::try_once_or_leave_yolo{});
}
protected:
/**
* Expire old timestamps
*/
void expire_timestamps() {
auto now = timepoint_clock::now();
while (m_queue.size() > 0) {
if ((now - m_queue.front()) < m_timewindow)
break;
m_queue.pop_front();
}
}
private:
2016-12-15 02:30:41 +00:00
queue m_queue{};
limit m_limit{};
timewindow m_timewindow{};
2016-06-15 03:32:35 +00:00
};
using throttle_t = unique_ptr<event_throttler>;
2016-06-15 03:32:35 +00:00
template <typename... Args>
throttle_t make_throttler(Args&&... args) {
2016-12-09 08:02:47 +00:00
return factory_util::unique<event_throttler>(forward<Args>(args)...);
2016-06-15 03:32:35 +00:00
}
}
2016-11-19 05:22:44 +00:00
POLYBAR_NS_END