Browse Source

Generate sync interval exactly once

rules-rpn
Max Prokhorov 5 years ago
parent
commit
ad2ecc45f3
1 changed files with 6 additions and 14 deletions
  1. +6
    -14
      code/espurna/ntp.ino

+ 6
- 14
code/espurna/ntp.ino View File

@ -50,22 +50,16 @@ void _ntpWantSync() {
_ntp_want_sync = true; _ntp_want_sync = true;
} }
int _ntpSyncInterval() {
// Randomized in time to avoid clogging the server with simultaious requests from multiple devices
// (for example, when multiple devices start up at the same time)
int inline _ntpSyncInterval() {
return secureRandom(NTP_SYNC_INTERVAL, NTP_SYNC_INTERVAL * 2); return secureRandom(NTP_SYNC_INTERVAL, NTP_SYNC_INTERVAL * 2);
} }
int _ntpUpdateInterval() {
int inline _ntpUpdateInterval() {
return secureRandom(NTP_UPDATE_INTERVAL, NTP_UPDATE_INTERVAL * 2); return secureRandom(NTP_UPDATE_INTERVAL, NTP_UPDATE_INTERVAL * 2);
} }
void inline _ntpSetSyncInterval(bool shortInterval = false) {
if (shortInterval) {
setSyncInterval(_ntpSyncInterval());
} else {
setSyncInterval(_ntpUpdateInterval());
}
}
void _ntpStart() { void _ntpStart() {
_ntpConfigure(); _ntpConfigure();
@ -79,7 +73,8 @@ void _ntpStart() {
// Avoid triggering sync immediatly by canceling sync provider flag and resetting sync interval again // Avoid triggering sync immediatly by canceling sync provider flag and resetting sync interval again
setSyncProvider(_ntpSyncProvider); setSyncProvider(_ntpSyncProvider);
_ntp_want_sync = false; _ntp_want_sync = false;
_ntpSetSyncInterval(true);
setSyncInterval(NTPw.getShortInterval());
} }
@ -111,9 +106,6 @@ void _ntpConfigure() {
uint8_t dst_region = getSetting("ntpRegion", NTP_DST_REGION).toInt(); uint8_t dst_region = getSetting("ntpRegion", NTP_DST_REGION).toInt();
NTPw.setDSTZone(dst_region); NTPw.setDSTZone(dst_region);
// Set short interval to sync again shortly, randomized in time to avoid clogging the server with simultaious requests from multiple devices (i.e. on power surge, when devices start up at the same time)
setSyncInterval(true);
// Some remote servers can be slow to respond, increase accordingly // Some remote servers can be slow to respond, increase accordingly
// TODO does this need upper constrain? // TODO does this need upper constrain?
NTPw.setNTPTimeout(getSetting("ntpTimeout", NTP_TIMEOUT).toInt()); NTPw.setNTPTimeout(getSetting("ntpTimeout", NTP_TIMEOUT).toInt());


Loading…
Cancel
Save