From a6ec504513d751dff2b4b986b01891f94718477a Mon Sep 17 00:00:00 2001 From: Michael Carlberg Date: Wed, 1 Jun 2016 01:07:23 +0200 Subject: [PATCH] feat(core): Throttle writes to stdout --- README.md | 11 +++++++++++ config | 7 +++++++ examples/config | 7 +++++++ examples/config.bspwm | 7 +++++++ examples/config.i3 | 7 +++++++ src/eventloop.cpp | 36 ++++++++++++++++++++++++++++++++++-- 6 files changed, 73 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 1fecca61..2ae2a50f 100644 --- a/README.md +++ b/README.md @@ -160,6 +160,17 @@ the resulting output might not be award-winning. `🟊 = module is still flagged as work in progress` +### Application settings + ~~~ ini + [settings] + ; Limit the amount of events sent to lemonbar within a set timeframe: + ; - "Allow updates within of time" + ; Default values: + ;throttle_limit = 5 + ;throttle_ms = 50 + ~~~ + + ### Module `internal/backlight` ~~~ ini [module/backlight] diff --git a/config b/config index a37b014e..a23a08e1 100644 --- a/config +++ b/config @@ -1,3 +1,10 @@ +[settings] +; Limit the amount of events sent to lemonbar within a set timeframe: +; - "Allow updates within of time" +; Default values: +throttle_limit = 5 +throttle_ms = 50 + ; ; Bar configurations ; --------------------------------------- diff --git a/examples/config b/examples/config index 214ec69f..bd5adcab 100644 --- a/examples/config +++ b/examples/config @@ -1,3 +1,10 @@ +[settings] +; Limit the amount of events sent to lemonbar within a set timeframe: +; - "Allow updates within of time" +; Default values: +;throttle_limit = 5 +;throttle_ms = 50 + [bar/example] width = 100% height = 25 diff --git a/examples/config.bspwm b/examples/config.bspwm index 0c7a915b..019611a6 100644 --- a/examples/config.bspwm +++ b/examples/config.bspwm @@ -1,3 +1,10 @@ +[settings] +; Limit the amount of events sent to lemonbar within a set timeframe: +; - "Allow updates within of time" +; Default values: +;throttle_limit = 5 +;throttle_ms = 50 + [bar/example] width = 100% height = 25 diff --git a/examples/config.i3 b/examples/config.i3 index 0043936a..7aa3d5f0 100644 --- a/examples/config.i3 +++ b/examples/config.i3 @@ -1,3 +1,10 @@ +[settings] +; Limit the amount of events sent to lemonbar within a set timeframe: +; - "Allow updates within of time" +; Default values: +;throttle_limit = 5 +;throttle_ms = 50 + [bar/example] width = 100% height = 25 diff --git a/src/eventloop.cpp b/src/eventloop.cpp index 92f538cc..291a569e 100644 --- a/src/eventloop.cpp +++ b/src/eventloop.cpp @@ -3,6 +3,7 @@ #include #include #include +#include #include "eventloop.hpp" #include "services/command.hpp" @@ -98,10 +99,41 @@ void EventLoop::add_stdin_subscriber(const std::string& module_name) void EventLoop::loop_write() { + std::deque ticks; + + // Allow ticks within timeframe + const auto throttle_limit = config::get("settings", "throttle_limit", 5); + const auto throttle_ms = std::chrono::duration(config::get("settings", "throttle_ms", 50)); + while (this->running()) { try { - if (this->registry->wait()) - this->write_stdout(); + if (!this->registry->wait()) + continue; + + auto now = std::chrono::high_resolution_clock::now(); + + // Expire previous ticks + while (ticks.size() > 0) { + if ((now - ticks.front()) < throttle_ms) + break; + + ticks.pop_front(); + } + + // Place the new tick in the bottom of the deck + ticks.emplace_back(std::chrono::high_resolution_clock::now()); + + // Have we reached the limit? + if (ticks.size() >= throttle_limit) { + log_debug("Throttling write to stdout"); + + std::this_thread::sleep_for(throttle_ms * ticks.size()); + + if (ticks.size() - 1 >= throttle_limit) + continue; + } + + this->write_stdout(); } catch (Exception &e) { this->logger->error(e.what()); return;