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