turn on “unreliable” mock server for better coverage, rework periodic change backoff
diff --git a/apigeeSync_suite_test.go b/apigeeSync_suite_test.go index 51a40b3..960f0a4 100644 --- a/apigeeSync_suite_test.go +++ b/apigeeSync_suite_test.go
@@ -38,6 +38,7 @@ config.Set(configSnapServerBaseURI, testServer.URL) config.Set(configChangeServerBaseURI, testServer.URL) config.Set(configSnapshotProtocol, "json") + config.Set(configPollInterval, 10*time.Millisecond) config.Set(configName, "testhost") config.Set(configApidClusterId, "bootstrap") @@ -48,7 +49,7 @@ // set up mock server mockParms := MockParms{ - ReliableAPI: true, + ReliableAPI: false, ClusterID: config.GetString(configApidClusterId), TokenKey: config.GetString(configConsumerKey), TokenSecret: config.GetString(configConsumerSecret),
diff --git a/apigee_sync.go b/apigee_sync.go index cb1242a..9a30b99 100644 --- a/apigee_sync.go +++ b/apigee_sync.go
@@ -52,41 +52,31 @@ } /* - * Helper function that sleeps for N seconds if comm with change agent - * fails. The retry interval gradually is incremented each time it fails - * till it reaches the Polling Int time, and after which it constantly - * retries at the polling time interval + * Polls change agent for changes. In event of errors, uses a doubling + * backoff from 200ms up to a max delay of the configPollInterval value. */ func updatePeriodicChanges() { - times := 1 - pollInterval := config.GetInt(configPollInterval) + var backOffFunc func() + pollInterval := config.GetDuration(configPollInterval) for { - startTime := time.Second err := pollChangeAgent() if err != nil { log.Debugf("Error connecting to changeserver: %v", err) - } - endTime := time.Second - // Gradually increase retry interval, and max at some level - if endTime-startTime <= 1 { - if times < pollInterval { - times++ - } else { - times = pollInterval + if backOffFunc == nil { + log.Error("creating backoff") + backOffFunc = createBackOff(200*time.Millisecond, pollInterval) } - log.Debugf("Connecting to changeserver...") - time.Sleep(time.Duration(times) * 200 * time.Millisecond) + backOffFunc() } else { - // Reset sleep interval - times = 1 + log.Error("resetting backoff") + backOffFunc = nil } - } } /* - * Long polls every 45 seconds the change agent. Parses the response from + * Long polls the change agent with a 45 second block. Parses the response from * change agent and raises an event. */ func pollChangeAgent() error { @@ -209,12 +199,12 @@ // simple doubling back-off func createBackOff(retryIn, maxBackOff time.Duration) func() { return func() { - log.Debugf("backoff called. will retry in %s.", retryIn) - time.Sleep(retryIn) - retryIn = retryIn * time.Duration(2) if retryIn > maxBackOff { retryIn = maxBackOff } + log.Debugf("backoff called. will retry in %s.", retryIn) + time.Sleep(retryIn) + retryIn = retryIn * time.Duration(2) } }
diff --git a/init.go b/init.go index b96f681..5f3c6cb 100644 --- a/init.go +++ b/init.go
@@ -6,6 +6,7 @@ "os" "github.com/30x/apid-core" + "time" ) const ( @@ -51,7 +52,7 @@ } func initDefaults() { - config.SetDefault(configPollInterval, 120) + config.SetDefault(configPollInterval, 120 * time.Second) config.SetDefault(configSnapshotProtocol, "json") name, errh := os.Hostname() if (errh != nil) && (len(config.GetString(configName)) == 0) {