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