Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 1 | #pragma once |
2 | |||||
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 3 | #include <boost/circular_buffer.hpp> |
4 | #include <boost/circular_buffer/space_optimized.hpp> | ||||
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 5 | #include <chrono> |
6 | #include <functional> | ||||
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 7 | |
Ed Tanous | c94ad49 | 2019-10-10 15:39:33 -0700 | [diff] [blame] | 8 | #include "logging.h" |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 9 | |
10 | namespace crow | ||||
11 | { | ||||
12 | namespace detail | ||||
13 | { | ||||
James Feist | f0af859 | 2020-03-27 16:28:59 -0700 | [diff] [blame] | 14 | |
15 | constexpr const size_t timerQueueTimeoutSeconds = 5; | ||||
James Feist | cb6cb49 | 2020-04-03 13:36:17 -0700 | [diff] [blame] | 16 | constexpr const size_t maxSize = 100; |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 17 | // fast timer queue for fixed tick value. |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 18 | class TimerQueue |
19 | { | ||||
20 | public: | ||||
21 | TimerQueue() | ||||
22 | { | ||||
James Feist | cb6cb49 | 2020-04-03 13:36:17 -0700 | [diff] [blame] | 23 | dq.set_capacity(maxSize); |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 24 | } |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 25 | |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 26 | void cancel(size_t k) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 27 | { |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 28 | size_t index = k - step; |
Ed Tanous | b01bf29 | 2019-03-25 19:25:26 +0000 | [diff] [blame] | 29 | if (index < dq.size()) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 30 | { |
Ed Tanous | b01bf29 | 2019-03-25 19:25:26 +0000 | [diff] [blame] | 31 | dq[index].second = nullptr; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 32 | } |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 33 | } |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 34 | |
James Feist | cb6cb49 | 2020-04-03 13:36:17 -0700 | [diff] [blame] | 35 | std::optional<size_t> add(std::function<void()> f) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 36 | { |
James Feist | cb6cb49 | 2020-04-03 13:36:17 -0700 | [diff] [blame] | 37 | if (dq.size() == maxSize) |
38 | { | ||||
39 | return std::nullopt; | ||||
40 | } | ||||
41 | |||||
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 42 | dq.push_back( |
43 | std::make_pair(std::chrono::steady_clock::now(), std::move(f))); | ||||
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 44 | size_t ret = step + dq.size() - 1; |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 45 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 46 | BMCWEB_LOG_DEBUG << "timer add inside: " << this << ' ' << ret; |
47 | return ret; | ||||
48 | } | ||||
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 49 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 50 | void process() |
51 | { | ||||
52 | auto now = std::chrono::steady_clock::now(); | ||||
53 | while (!dq.empty()) | ||||
54 | { | ||||
55 | auto& x = dq.front(); | ||||
Jan Sowinski | 2b5e08e | 2020-01-09 17:16:02 +0100 | [diff] [blame] | 56 | // Check expiration time only for active handlers, |
57 | // remove canceled ones immediately | ||||
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 58 | if (x.second) |
59 | { | ||||
James Feist | f0af859 | 2020-03-27 16:28:59 -0700 | [diff] [blame] | 60 | if (now - x.first < |
61 | std::chrono::seconds(timerQueueTimeoutSeconds)) | ||||
Jan Sowinski | 2b5e08e | 2020-01-09 17:16:02 +0100 | [diff] [blame] | 62 | { |
63 | break; | ||||
64 | } | ||||
65 | |||||
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 66 | BMCWEB_LOG_DEBUG << "timer call: " << this << ' ' << step; |
67 | // we know that timer handlers are very simple currenty; call | ||||
68 | // here | ||||
69 | x.second(); | ||||
70 | } | ||||
71 | dq.pop_front(); | ||||
72 | step++; | ||||
73 | } | ||||
74 | } | ||||
75 | |||||
76 | private: | ||||
77 | using storage_type = | ||||
78 | std::pair<std::chrono::time_point<std::chrono::steady_clock>, | ||||
79 | std::function<void()>>; | ||||
80 | |||||
81 | boost::circular_buffer_space_optimized<storage_type, | ||||
82 | std::allocator<storage_type>> | ||||
83 | dq{}; | ||||
84 | |||||
85 | // boost::circular_buffer<storage_type> dq{20}; | ||||
86 | // std::deque<storage_type> dq{}; | ||||
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 87 | size_t step{}; |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 88 | }; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 89 | } // namespace detail |
90 | } // namespace crow |