campaignd: Use a POSIX monotonic clock for scheduling config commits
Only available on Linux and probably some BSD systems. For other systems we issue a #warning and use time() instead (which will cause the compiler to quit with -Werror, but that's the whole point).
This commit is contained in:
parent
5e80fb1a38
commit
64c71161f1
1 changed files with 15 additions and 2 deletions
|
@ -35,6 +35,7 @@
|
|||
#include "util.hpp"
|
||||
|
||||
#include <csignal>
|
||||
#include <ctime>
|
||||
|
||||
#include <boost/foreach.hpp>
|
||||
#include <boost/iostreams/filter/gzip.hpp>
|
||||
|
@ -81,6 +82,18 @@ void exit_sigterm(int signal)
|
|||
exit(128 + SIGTERM);
|
||||
}
|
||||
|
||||
time_t monotonic_clock()
|
||||
{
|
||||
#ifdef _POSIX_MONOTONIC_CLOCK
|
||||
timespec ts;
|
||||
clock_gettime(CLOCK_MONOTONIC, &ts);
|
||||
return ts.tv_sec;
|
||||
#else
|
||||
#warning monotonic_clock() is not truly monotonic!
|
||||
return time(NULL);
|
||||
#endif
|
||||
}
|
||||
|
||||
} // end anonymous namespace
|
||||
|
||||
namespace campaignd {
|
||||
|
@ -249,7 +262,7 @@ void server::run()
|
|||
{
|
||||
network::connection sock = 0;
|
||||
|
||||
time_t last_ts = time(NULL);
|
||||
time_t last_ts = monotonic_clock();
|
||||
|
||||
for(;;)
|
||||
{
|
||||
|
@ -263,7 +276,7 @@ void server::run()
|
|||
}
|
||||
}
|
||||
|
||||
const time_t cur_ts = time(NULL);
|
||||
const time_t cur_ts = monotonic_clock();
|
||||
// Write config to disk every ten minutes.
|
||||
if(labs(cur_ts - last_ts) >= 10*60) {
|
||||
write_config();
|
||||
|
|
Loading…
Add table
Reference in a new issue