-
Notifications
You must be signed in to change notification settings - Fork 37
Use queue based scheduling in Delay #305
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+229
−76
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
0586dc9
make queue-based scheduling to delay
AlexInLog 78f2221
fix tests
AlexInLog e185974
compile fix
AlexInLog 0b2ba04
Cover with tests
AlexInLog b5aa4e2
compile fix
AlexInLog 5af4225
remove test
AlexInLog 434e148
compile fix
AlexInLog File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -16,66 +16,138 @@ | |||||
| #include <rpp/operators/details/subscriber_with_state.hpp> // create_subscriber_with_state | ||||||
| #include <rpp/operators/fwd/delay.hpp> // own forwarding | ||||||
| #include <rpp/subscribers/constraints.hpp> // constraint::subscriber_of_type | ||||||
| #include <rpp/utils/overloaded.hpp> | ||||||
|
|
||||||
| #include <variant> | ||||||
|
|
||||||
| IMPLEMENTATION_FILE(delay_tag); | ||||||
|
|
||||||
| namespace rpp::details | ||||||
| { | ||||||
| /** | ||||||
| * Functor (type-erasure) of "delay" for on_next operator. | ||||||
| */ | ||||||
| struct delay_on_next | ||||||
| struct completion {}; | ||||||
|
|
||||||
| template<typename T, typename Subscriber, typename Worker> | ||||||
| class queue_based_worker final : public std::enable_shared_from_this<queue_based_worker<T, Subscriber, Worker>> | ||||||
| { | ||||||
| schedulers::duration delay; | ||||||
| public: | ||||||
| queue_based_worker(schedulers::duration delay, Worker&& worker, const Subscriber& subscriber) | ||||||
| : m_delay{delay} | ||||||
| , m_worker{std::move(worker)} | ||||||
| , m_subscriber{subscriber} {} | ||||||
|
|
||||||
| void operator()(auto&& value, const auto& subscriber, const auto& worker) const | ||||||
| queue_based_worker(schedulers::duration delay, Worker&& worker, Subscriber&& subscriber) | ||||||
| : m_delay{delay} | ||||||
| , m_worker{std::move(worker)} | ||||||
| , m_subscriber{std::move(subscriber)} {} | ||||||
|
|
||||||
| struct on_next | ||||||
| { | ||||||
| worker.schedule(delay, | ||||||
| [value = std::forward<decltype(value)>(value), subscriber]() | ||||||
| { | ||||||
| subscriber.on_next(std::move(value)); | ||||||
| return schedulers::optional_duration{}; | ||||||
| }); | ||||||
| } | ||||||
| }; | ||||||
| void operator()(auto&& value, const std::shared_ptr<queue_based_worker<T, Subscriber, Worker>>& state) const | ||||||
| { | ||||||
| state->emplace(std::forward<decltype(value)>(value)); | ||||||
| } | ||||||
| }; | ||||||
|
|
||||||
| /** | ||||||
| * Functor (type-erasure) of "delay" for on_error operator. | ||||||
| */ | ||||||
| struct delay_on_error | ||||||
| { | ||||||
| void operator()(const std::exception_ptr& err, const auto& subscriber, const auto& worker) const | ||||||
| struct on_error | ||||||
| { | ||||||
| // on-error must be delivered as soon as possible | ||||||
| worker.schedule([err, subscriber]() | ||||||
| { | ||||||
| subscriber.on_error(err); | ||||||
| return schedulers::optional_duration{}; | ||||||
| }); | ||||||
| void operator()(const std::exception_ptr& err, const std::shared_ptr<queue_based_worker<T, Subscriber, Worker>>& state) const | ||||||
| { | ||||||
| state->emplace(err); | ||||||
| } | ||||||
| }; | ||||||
|
|
||||||
| struct on_completed | ||||||
| { | ||||||
| void operator()(const std::shared_ptr<queue_based_worker<T, Subscriber, Worker>>& state) const | ||||||
| { | ||||||
| state->emplace(completion{}); | ||||||
| } | ||||||
| }; | ||||||
|
|
||||||
| private: | ||||||
| template<typename TT> | ||||||
| void emplace(TT&& item) | ||||||
| { | ||||||
| if (const auto timepoint = emplace_safe(std::forward<TT>(item))) | ||||||
| { | ||||||
| m_worker.schedule(timepoint.value(), | ||||||
| [state = this->shared_from_this()]()-> schedulers::optional_duration | ||||||
| { | ||||||
| return state->drain_queue(); | ||||||
| }); | ||||||
| } | ||||||
| } | ||||||
| }; | ||||||
|
|
||||||
| /** | ||||||
| * Functor (type-erasure) of "delay" for on_completed operator. | ||||||
| */ | ||||||
| struct delay_on_completed | ||||||
| { | ||||||
| schedulers::duration delay; | ||||||
| template<typename TT> | ||||||
| std::optional<schedulers::time_point> emplace_safe(TT&& item) | ||||||
| { | ||||||
| std::lock_guard lock{m_mutex}; | ||||||
| const auto delay = std::is_same_v<std::exception_ptr, std::decay_t<TT>> ? schedulers::duration{0} : m_delay; | ||||||
| m_queue.emplace(++m_current_id, m_worker.now()+delay, std::forward<TT>(item)); | ||||||
| if (!m_active && m_queue.size() == 1) | ||||||
| { | ||||||
| m_active = true; | ||||||
| return m_queue.top().time; | ||||||
| } | ||||||
| return {}; | ||||||
| } | ||||||
|
|
||||||
| void operator()(const auto& subscriber, const auto& worker) const | ||||||
| schedulers::optional_duration drain_queue() | ||||||
| { | ||||||
| worker.schedule(delay, | ||||||
| [subscriber]() | ||||||
| { | ||||||
| subscriber.on_completed(); | ||||||
| return schedulers::optional_duration{}; | ||||||
| }); | ||||||
| while (true) | ||||||
| { | ||||||
| std::unique_lock lock{m_mutex}; | ||||||
| if (m_queue.empty()) | ||||||
| { | ||||||
| m_active = false; | ||||||
| return {}; | ||||||
| } | ||||||
|
|
||||||
| auto& top = m_queue.top(); | ||||||
| const auto now = m_worker.now(); | ||||||
| if (top.time > now) | ||||||
| return top.time - now; | ||||||
|
|
||||||
| auto item = std::move(top.item); | ||||||
| m_queue.pop(); | ||||||
| lock.unlock(); | ||||||
|
|
||||||
| std::visit(utils::overloaded | ||||||
| { | ||||||
| [&](T&& v) { m_subscriber.on_next(std::move(v)); }, | ||||||
| [&](const std::exception_ptr& err) { m_subscriber.on_error(err); }, | ||||||
| [&](completion) { m_subscriber.on_completed(); } | ||||||
| }, | ||||||
| std::move(item)); | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| private: | ||||||
| struct emission | ||||||
| { | ||||||
| template<typename TT> | ||||||
| emission(size_t id, schedulers::time_point time, TT&& item) | ||||||
| : id{id} | ||||||
| , time{std::move(time)} | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| , item{std::forward<TT>(item)} {} | ||||||
|
|
||||||
| size_t id{}; | ||||||
| schedulers::time_point time{}; | ||||||
| std::variant<T, std::exception_ptr, completion> item{}; | ||||||
|
|
||||||
| bool operator<(const emission& other) const { return std::tie(time, id) >= std::tie(other.time, other.id); } | ||||||
| }; | ||||||
|
|
||||||
| schedulers::duration m_delay; | ||||||
| Worker m_worker; | ||||||
| Subscriber m_subscriber; | ||||||
| std::mutex m_mutex{}; | ||||||
| size_t m_current_id{}; | ||||||
| std::priority_queue<emission> m_queue{}; | ||||||
| bool m_active{}; | ||||||
| }; | ||||||
|
|
||||||
| /** | ||||||
| * \brief Functor of OperatorFn for "combine_latest" operator (used by "lift"). | ||||||
| */ | ||||||
|
|
||||||
| template<constraint::decayed_type Type, schedulers::constraint::scheduler TScheduler> | ||||||
| struct delay_impl | ||||||
| { | ||||||
|
|
@@ -86,14 +158,16 @@ struct delay_impl | |||||
| auto operator()(TSub&& subscriber) const | ||||||
| { | ||||||
| auto worker = scheduler.create_worker(subscriber.get_subscription()); | ||||||
|
|
||||||
| auto subscription = subscriber.get_subscription().make_child(); | ||||||
|
|
||||||
| using state_t = queue_based_worker<Type, std::decay_t<TSub>, std::decay_t<decltype(worker)>>; | ||||||
| auto state = std::make_shared<state_t>(delay, std::move(worker), std::forward<TSub>(subscriber)); | ||||||
|
|
||||||
| return create_subscriber_with_state<Type>(std::move(subscription), | ||||||
| delay_on_next{delay}, | ||||||
| delay_on_error{}, | ||||||
| delay_on_completed{delay}, | ||||||
| std::forward<TSub>(subscriber), | ||||||
| std::move(worker)); | ||||||
| typename state_t::on_next{}, | ||||||
| typename state_t::on_error{}, | ||||||
| typename state_t::on_completed{}, | ||||||
| std::move(state)); | ||||||
| } | ||||||
| }; | ||||||
| } // namespace rpp::details | ||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
std::move of the variable
timeof the trivially-copyable typeschedulers::time_point(akatime_point<std::chrono::steady_clock, duration<long, ratio<1, 1000000000>>>) has no effect; remove std::move()