func write() { consecutiveFailures := 0 for { // Wait a random amount of time (to avoid looking too suspicious) // Note - rand was seeded with the startup time in flashlight.go n := rand.Intn(publishSecondsVariance) wait := time.Duration(basePublishSeconds-publishSecondsVariance/2+n) * time.Second oldIp := GetIp() oldCountry := GetCountry() newCountry, newIp, err := lookupIp(client.Load().(*http.Client)) if err == nil { consecutiveFailures = 0 if newIp != oldIp { log.Debugf("IP changed") ip.Store(newIp) } // Always publish location, even if unchanged pubsub.Pub(pubsub.IP, newIp) service.Out <- newCountry } else { msg := fmt.Sprintf("Unable to get current location: %s", err) // When retrying after a failure, wait a different amount of time retryWait := time.Duration(math.Pow(2, float64(consecutiveFailures))*float64(retryWaitMillis)) * time.Millisecond if retryWait < wait { log.Debug(msg) wait = retryWait } else { log.Error(msg) } log.Debugf("Waiting %v before retrying", wait) consecutiveFailures += 1 // If available, publish last known location if oldCountry != "" { service.Out <- oldCountry } } time.Sleep(wait) } }
func write() { consecutiveFailures := 0 for { // Wait a random amount of time (to avoid looking too suspicious) // Note - rand was seeded with the startup time in flashlight.go n := rand.Intn(publishSecondsVariance) wait := time.Duration(basePublishSeconds-publishSecondsVariance/2+n) * time.Second oldLocation := GetLocation() newLocation, err := geolookup.LookupIPWithClient("", client.Load().(*http.Client)) if err == nil { consecutiveFailures = 0 if !reflect.DeepEqual(newLocation, oldLocation) { log.Debugf("Location changed") location.Store(newLocation) pubsub.Pub(pubsub.Location, newLocation) } // Always publish location, even if unchanged service.Out <- newLocation } else { msg := fmt.Sprintf("Unable to get current location: %s", err) // When retrying after a failure, wait a different amount of time retryWait := time.Duration(math.Pow(2, float64(consecutiveFailures))*float64(retryWaitMillis)) * time.Millisecond if retryWait < wait { log.Debug(msg) wait = retryWait } else { log.Error(msg) } log.Debugf("Waiting %v before retrying", wait) consecutiveFailures += 1 // If available, publish last known location if oldLocation != nil { service.Out <- oldLocation } } time.Sleep(wait) } }