blob: 24a4ab4c12852b4cc67d8fe50d5dacac320ae2cf [file] [log] [blame]
Ed Tanouse0d918b2018-03-27 17:41:04 -07001#pragma once
2
Ed Tanous04e438c2020-10-03 08:06:26 -07003#include "logging.hpp"
Gunnar Mills1214b7e2020-06-04 10:11:30 -05004
Ed Tanouse0d918b2018-03-27 17:41:04 -07005#include <boost/circular_buffer.hpp>
6#include <boost/circular_buffer/space_optimized.hpp>
Gunnar Mills1214b7e2020-06-04 10:11:30 -05007
Ed Tanous1abe55e2018-09-05 08:30:59 -07008#include <chrono>
9#include <functional>
Ed Tanouse0d918b2018-03-27 17:41:04 -070010
Ed Tanous1abe55e2018-09-05 08:30:59 -070011namespace crow
12{
James Feist3909dc82020-04-03 10:58:55 -070013
14constexpr const size_t timerQueueTimeoutSeconds = 5;
Ed Tanous1abe55e2018-09-05 08:30:59 -070015namespace detail
16{
James Feistf0af8592020-03-27 16:28:59 -070017
James Feistcb6cb492020-04-03 13:36:17 -070018constexpr const size_t maxSize = 100;
Ed Tanouse0d918b2018-03-27 17:41:04 -070019// fast timer queue for fixed tick value.
Ed Tanous1abe55e2018-09-05 08:30:59 -070020class TimerQueue
21{
22 public:
23 TimerQueue()
24 {
James Feistcb6cb492020-04-03 13:36:17 -070025 dq.set_capacity(maxSize);
Ed Tanouse0d918b2018-03-27 17:41:04 -070026 }
Ed Tanouse0d918b2018-03-27 17:41:04 -070027
Ed Tanous271584a2019-07-09 16:24:22 -070028 void cancel(size_t k)
Ed Tanous1abe55e2018-09-05 08:30:59 -070029 {
Ed Tanous271584a2019-07-09 16:24:22 -070030 size_t index = k - step;
Ed Tanousb01bf292019-03-25 19:25:26 +000031 if (index < dq.size())
Ed Tanous1abe55e2018-09-05 08:30:59 -070032 {
Ed Tanousb01bf292019-03-25 19:25:26 +000033 dq[index].second = nullptr;
Ed Tanous1abe55e2018-09-05 08:30:59 -070034 }
Karthick Sundarrajan8ba7ae52020-12-07 14:22:46 -080035 while (dq.begin() != dq.end() && dq.front().second == nullptr)
36 {
37 dq.pop_front();
38 step++;
39 }
Ed Tanouse0d918b2018-03-27 17:41:04 -070040 }
Ed Tanouse0d918b2018-03-27 17:41:04 -070041
James Feistcb6cb492020-04-03 13:36:17 -070042 std::optional<size_t> add(std::function<void()> f)
Ed Tanous1abe55e2018-09-05 08:30:59 -070043 {
James Feistcb6cb492020-04-03 13:36:17 -070044 if (dq.size() == maxSize)
45 {
46 return std::nullopt;
47 }
48
Ed Tanous1abe55e2018-09-05 08:30:59 -070049 dq.push_back(
50 std::make_pair(std::chrono::steady_clock::now(), std::move(f)));
Ed Tanous271584a2019-07-09 16:24:22 -070051 size_t ret = step + dq.size() - 1;
Ed Tanouse0d918b2018-03-27 17:41:04 -070052
Ed Tanous1abe55e2018-09-05 08:30:59 -070053 BMCWEB_LOG_DEBUG << "timer add inside: " << this << ' ' << ret;
54 return ret;
55 }
Ed Tanouse0d918b2018-03-27 17:41:04 -070056
Ed Tanous1abe55e2018-09-05 08:30:59 -070057 void process()
58 {
59 auto now = std::chrono::steady_clock::now();
60 while (!dq.empty())
61 {
62 auto& x = dq.front();
Jan Sowinski2b5e08e2020-01-09 17:16:02 +010063 // Check expiration time only for active handlers,
64 // remove canceled ones immediately
Ed Tanous1abe55e2018-09-05 08:30:59 -070065 if (x.second)
66 {
James Feistf0af8592020-03-27 16:28:59 -070067 if (now - x.first <
68 std::chrono::seconds(timerQueueTimeoutSeconds))
Jan Sowinski2b5e08e2020-01-09 17:16:02 +010069 {
70 break;
71 }
72
Ed Tanous1abe55e2018-09-05 08:30:59 -070073 BMCWEB_LOG_DEBUG << "timer call: " << this << ' ' << step;
Gunnar Millscaa3ce32020-07-08 14:46:53 -050074 // we know that timer handlers are very simple currently; call
Ed Tanous1abe55e2018-09-05 08:30:59 -070075 // here
76 x.second();
77 }
78 dq.pop_front();
79 step++;
80 }
81 }
82
83 private:
84 using storage_type =
85 std::pair<std::chrono::time_point<std::chrono::steady_clock>,
86 std::function<void()>>;
87
88 boost::circular_buffer_space_optimized<storage_type,
89 std::allocator<storage_type>>
90 dq{};
91
92 // boost::circular_buffer<storage_type> dq{20};
93 // std::deque<storage_type> dq{};
Ed Tanous271584a2019-07-09 16:24:22 -070094 size_t step{};
Ed Tanouse0d918b2018-03-27 17:41:04 -070095};
Ed Tanous1abe55e2018-09-05 08:30:59 -070096} // namespace detail
97} // namespace crow