Mercurial > routeflapper
comparison 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 |
comparison
equal
deleted
inserted
replaced
2:bb3f804f13a0 | 3:4a81cc2da570 |
---|---|
46 | 46 |
47 | 47 |
48 //////////////////////////////////////////////// | 48 //////////////////////////////////////////////// |
49 // syslog a message | 49 // syslog a message |
50 // | 50 // |
51 void my_syslog(char *text) { | 51 void my_syslog(char *text) |
52 { | |
52 if (use_syslog) { | 53 if (use_syslog) { |
53 pthread_mutex_lock(&syslog_mutex); | 54 pthread_mutex_lock(&syslog_mutex); |
54 if (!syslog_opened) { | 55 if (!syslog_opened) { |
55 openlog("routeflapper", LOG_PID, LOG_AUTHPRIV); | 56 openlog("routeflapper", LOG_PID, LOG_AUTHPRIV); |
56 syslog_opened = true; | 57 syslog_opened = true; |
61 else { | 62 else { |
62 printf("%s\n", text); | 63 printf("%s\n", text); |
63 } | 64 } |
64 } | 65 } |
65 | 66 |
67 | |
66 //////////////////////////////////////////////// | 68 //////////////////////////////////////////////// |
67 // reload the config | 69 // reload the config |
68 // | 70 // |
69 CONFIG* new_conf(); | 71 CONFIG* new_conf(); |
70 CONFIG* new_conf() { | 72 CONFIG* new_conf() |
73 { | |
71 CONFIG *newc = new CONFIG; | 74 CONFIG *newc = new CONFIG; |
72 pthread_mutex_lock(&config_mutex); | 75 pthread_mutex_lock(&config_mutex); |
73 newc->generation = generation++; | 76 newc->generation = generation++; |
74 pthread_mutex_unlock(&config_mutex); | 77 pthread_mutex_unlock(&config_mutex); |
75 if (debug_syslog) { | 78 if (debug_syslog) { |
88 | 91 |
89 //////////////////////////////////////////////// | 92 //////////////////////////////////////////////// |
90 // thread to update hourly statistics | 93 // thread to update hourly statistics |
91 // | 94 // |
92 void* hourly_update(void *arg); | 95 void* hourly_update(void *arg); |
93 void* hourly_update(void *arg) { | 96 void* hourly_update(void *arg) |
97 { | |
98 unsigned seed = (unsigned)time(NULL); | |
94 while (loader_run) { | 99 while (loader_run) { |
95 int ran = (rand() % 600) - 300; // random(-5min,+5min) | 100 int ran = (rand_r(&seed) % 600) - 300; // random(-5min,+5min) |
96 int hour = 3600 - 60 + ran; // 59 minutes +- random up to 5 minutes. | 101 int hour = 3600 - 60 + ran; // 59 minutes +- random up to 5 minutes. |
97 int period = (hour + 10) / 20; // twenty periods ... | 102 int period = (hour + 10) / 20; // twenty periods ... |
98 int count = 0; | 103 for (int count = 0; loader_run && (count < 20); count++) { |
99 while (loader_run) { | |
100 sleep(period); | 104 sleep(period); |
101 if (!loader_run) break; // ... so we only wait about 3 minutes to terminate | 105 } |
102 count++; | 106 if (!loader_run) break; // ... so we only wait about 3 minutes to terminate |
103 if (count == 20) { | 107 routing_hourly_update(); // roughly an hour |
104 routing_hourly_update(); // roughly an hour | |
105 count = 0; | |
106 }; | |
107 } | |
108 } | 108 } |
109 } | 109 } |
110 | 110 |
111 | 111 |
112 //////////////////////////////////////////////// | 112 //////////////////////////////////////////////// |
113 // thread to watch the old config files for changes | 113 // thread to watch the old config files for changes |
114 // and reload when needed. | 114 // and reload when needed. |
115 // | 115 // |
116 void* config_loader(void *arg); | 116 void* config_loader(void *arg); |
117 void* config_loader(void *arg) { | 117 void* config_loader(void *arg) |
118 { | |
118 typedef set<CONFIG *> configp_set; | 119 typedef set<CONFIG *> configp_set; |
119 while (loader_run) { | 120 while (loader_run) { |
120 sleep(180); // look for modifications every 3 minutes | 121 sleep(180); // look for modifications every 3 minutes |
121 if (!loader_run) break; | 122 if (!loader_run) break; |
122 CONFIG &dc = *config; | 123 CONFIG &dc = *config; |