Skip to content
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

fix: coro timers ez pz lemon squeezy #1351

Merged
merged 5 commits into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions include/dpp/cluster.h
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,33 @@ class DPP_EXPORT cluster {
*/
timer start_timer(timer_callback_t on_tick, uint64_t frequency, timer_callback_t on_stop = {});

#ifdef DPP_CORO
/**
* @brief Start a coroutine timer. Every `frequency` seconds, the callback is called.
*
* @param on_tick The callback lambda to call for this timer when ticked
* @param on_stop The callback lambda to call for this timer when it is stopped
* @param frequency How often to tick the timer in seconds
* @return timer A handle to the timer, used to remove that timer later
*/
template <std::invocable<timer> T, std::invocable<timer> U = std::function<void(timer)>>
requires (dpp::awaitable_type<typename std::invoke_result<T, timer>::type>)
timer start_timer(T&& on_tick, uint64_t frequency, U&& on_stop = {}) {
std::function<void(timer)> ticker = [fun = std::forward<T>(on_tick)](timer t) mutable -> dpp::job {
co_await std::invoke(fun, t);
};
std::function<void(timer)> stopper;
if constexpr (dpp::awaitable_type<typename std::invoke_result<U, timer>::type>) {
stopper = [fun = std::forward<U>(on_stop)](timer t) mutable -> dpp::job {
co_await std::invoke(fun, t);
};
} else {
stopper = std::forward<U>(on_stop);
}
return start_timer(std::move(ticker), frequency, std::move(stopper));
}
#endif

/**
* @brief Stop a ticking timer
*
Expand Down
2 changes: 1 addition & 1 deletion include/dpp/zlibcontext.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ constexpr size_t DECOMP_BUFFER_SIZE = 512 * 1024;
* This wraps the C pointers needed for zlib with unique_ptr and gives us a nice
* buffer abstraction so we don't need to wrestle with raw pointers.
*/
class zlibcontext {
class DPP_EXPORT zlibcontext {
public:
/**
* @brief Zlib stream struct. The actual type is defined in zlib.h
Expand Down
11 changes: 6 additions & 5 deletions src/dpp/cluster/timer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ timer cluster::start_timer(timer_callback_t on_tick, uint64_t frequency, timer_c

new_timer.handle = next_handle++;
new_timer.next_tick = time(nullptr) + frequency;
new_timer.on_tick = on_tick;
new_timer.on_stop = on_stop;
new_timer.on_tick = std::move(on_tick);
new_timer.on_stop = std::move(on_stop);
new_timer.frequency = frequency;

std::lock_guard<std::mutex> l(timer_guard);
Expand Down Expand Up @@ -64,16 +64,17 @@ void cluster::tick_timers() {
timer_t cur_timer;
{
std::lock_guard<std::mutex> l(timer_guard);
cur_timer = next_timer.top();
if (cur_timer.next_tick > now) {
if (next_timer.top().next_tick > now) {
/* Nothing to do */
break;
}
cur_timer = std::move(next_timer.top());
next_timer.pop();
}
timers_deleted_t::iterator deleted_iter{};
bool deleted{};
{
std::lock_guard<std::mutex> l(timer_guard);
deleted_iter = deleted_timers.find(cur_timer.handle);
deleted = deleted_iter != deleted_timers.end();
}
Expand All @@ -83,7 +84,7 @@ void cluster::tick_timers() {
cur_timer.next_tick += cur_timer.frequency;
{
std::lock_guard<std::mutex> l(timer_guard);
next_timer.emplace(cur_timer);
next_timer.emplace(std::move(cur_timer));
}
} else {
/* Deleted timers are not reinserted into the priority queue and their on_stop is called */
Expand Down
1 change: 1 addition & 0 deletions src/dpp/socketengines/poll.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
************************************************************************************/

#include <dpp/socketengine.h>
#include <dpp/exception.h>
#include <vector>
#include <shared_mutex>
#ifdef _WIN32
Expand Down
3 changes: 1 addition & 2 deletions src/unittest/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2341,8 +2341,7 @@ Markdown lol \\|\\|spoiler\\|\\| \\~\\~strikethrough\\~\\~ \\`small \\*code\\* b
}

set_test(TIMERSTART, false);
static uint32_t ticks = 0;
dpp::timer th = bot.start_timer([](dpp::timer timer_handle) {
dpp::timer th = bot.start_timer([ticks = 0](dpp::timer timer_handle) mutable {
if (ticks == 2) {
/* The simple test timer ticks every second.
* If we get to 2 seconds, we know the timer is working.
Expand Down
Loading