diff src/routeflapper.cpp @ 3:4a81cc2da570

fix hourly randomization for unsynch
author Carl Byington <carl@five-ten-sg.com>
date Thu, 22 May 2008 20:39:52 -0700
parents bb3f804f13a0
children 180d26aa2a17
line wrap: on
line diff
--- a/src/routeflapper.cpp	Mon May 19 21:45:45 2008 -0700
+++ b/src/routeflapper.cpp	Thu May 22 20:39:52 2008 -0700
@@ -48,7 +48,8 @@
 ////////////////////////////////////////////////
 // syslog a message
 //
-void my_syslog(char *text) {
+void my_syslog(char *text)
+{
     if (use_syslog) {
         pthread_mutex_lock(&syslog_mutex);
             if (!syslog_opened) {
@@ -67,7 +68,8 @@
 //  reload the config
 //
 CONFIG* new_conf();
-CONFIG* new_conf() {
+CONFIG* new_conf()
+{
     CONFIG *newc = new CONFIG;
     pthread_mutex_lock(&config_mutex);
         newc->generation = generation++;
@@ -90,21 +92,18 @@
 //  thread to update hourly statistics
 //
 void* hourly_update(void *arg);
-void* hourly_update(void *arg) {
+void* hourly_update(void *arg)
+{
+    unsigned seed = (unsigned)time(NULL);
     while (loader_run) {
-        int ran    = (rand() % 600) - 300;  // random(-5min,+5min)
+        int ran    = (rand_r(&seed) % 600) - 300;   // random(-5min,+5min)
         int hour   = 3600 - 60 + ran;       // 59 minutes +- random up to 5 minutes.
         int period = (hour + 10) / 20;      // twenty periods ...
-        int count = 0;
-        while (loader_run) {
+        for (int count = 0; loader_run && (count < 20); count++) {
             sleep(period);
+        }
             if (!loader_run) break;         // ... so we only wait about 3 minutes to terminate
-            count++;
-            if (count == 20) {
                 routing_hourly_update();    // roughly an hour
-                count = 0;
-            };
-        }
     }
 }
 
@@ -114,7 +113,8 @@
 //  and reload when needed.
 //
 void* config_loader(void *arg);
-void* config_loader(void *arg) {
+void* config_loader(void *arg)
+{
     typedef set<CONFIG *> configp_set;
     while (loader_run) {
         sleep(180);  // look for modifications every 3 minutes