feat(core): Throttle writes to stdout

This commit is contained in:
Michael Carlberg 2016-06-01 01:07:23 +02:00
parent f033e0e7e5
commit a6ec504513
6 changed files with 73 additions and 2 deletions

View File

@ -160,6 +160,17 @@ the resulting output might not be award-winning.
`🟊 = module is still flagged as work in progress` `🟊 = 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 <throttle_limit> updates within <throttle_ms> of time"
; Default values:
;throttle_limit = 5
;throttle_ms = 50
~~~
### Module `internal/backlight` ### Module `internal/backlight`
~~~ ini ~~~ ini
[module/backlight] [module/backlight]

7
config
View File

@ -1,3 +1,10 @@
[settings]
; Limit the amount of events sent to lemonbar within a set timeframe:
; - "Allow <throttle_limit> updates within <throttle_ms> of time"
; Default values:
throttle_limit = 5
throttle_ms = 50
; ;
; Bar configurations ; Bar configurations
; --------------------------------------- ; ---------------------------------------

View File

@ -1,3 +1,10 @@
[settings]
; Limit the amount of events sent to lemonbar within a set timeframe:
; - "Allow <throttle_limit> updates within <throttle_ms> of time"
; Default values:
;throttle_limit = 5
;throttle_ms = 50
[bar/example] [bar/example]
width = 100% width = 100%
height = 25 height = 25

View File

@ -1,3 +1,10 @@
[settings]
; Limit the amount of events sent to lemonbar within a set timeframe:
; - "Allow <throttle_limit> updates within <throttle_ms> of time"
; Default values:
;throttle_limit = 5
;throttle_ms = 50
[bar/example] [bar/example]
width = 100% width = 100%
height = 25 height = 25

View File

@ -1,3 +1,10 @@
[settings]
; Limit the amount of events sent to lemonbar within a set timeframe:
; - "Allow <throttle_limit> updates within <throttle_ms> of time"
; Default values:
;throttle_limit = 5
;throttle_ms = 50
[bar/example] [bar/example]
width = 100% width = 100%
height = 25 height = 25

View File

@ -3,6 +3,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <fcntl.h> #include <fcntl.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <deque>
#include "eventloop.hpp" #include "eventloop.hpp"
#include "services/command.hpp" #include "services/command.hpp"
@ -98,9 +99,40 @@ void EventLoop::add_stdin_subscriber(const std::string& module_name)
void EventLoop::loop_write() void EventLoop::loop_write()
{ {
std::deque<std::chrono::high_resolution_clock::time_point> ticks;
// Allow <throttle_limit> ticks within <throttle_ms> timeframe
const auto throttle_limit = config::get<unsigned int>("settings", "throttle_limit", 5);
const auto throttle_ms = std::chrono::duration<double, std::milli>(config::get<unsigned int>("settings", "throttle_ms", 50));
while (this->running()) { while (this->running()) {
try { try {
if (this->registry->wait()) 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(); this->write_stdout();
} catch (Exception &e) { } catch (Exception &e) {
this->logger->error(e.what()); this->logger->error(e.what());