From ce0c6ed1e595d3c609367ab1d3f8c05ae2f6c8a2 Mon Sep 17 00:00:00 2001 From: Ox Cart Date: Tue, 24 Feb 2015 08:38:32 -0600 Subject: [PATCH] Fixed nil masquerades issue and problem with calculating wait time --- .../getlantern/flashlight/geolookup/geolookup.go | 12 ++++++------ src/github.com/getlantern/fronted/dialer.go | 5 ++++- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/src/github.com/getlantern/flashlight/geolookup/geolookup.go b/src/github.com/getlantern/flashlight/geolookup/geolookup.go index 92b5f49e3..84fbc95ff 100644 --- a/src/github.com/getlantern/flashlight/geolookup/geolookup.go +++ b/src/github.com/getlantern/flashlight/geolookup/geolookup.go @@ -17,8 +17,9 @@ import ( const ( messageType = `GeoLookup` - basePublishInterval = 30 * time.Second - publishIntervalVariance = basePublishInterval - 10*time.Second + basePublishSeconds = 30 + publishSecondsVariance = basePublishSeconds - 10 + retryWaitMillis = 100 ) var ( @@ -69,14 +70,13 @@ func registerService() error { } func write() { - retryWaitTime := 100 * time.Millisecond 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.Int63n(int64(publishIntervalVariance)) - wait := basePublishInterval - publishIntervalVariance/2 + time.Duration(n) + n := rand.Intn(publishSecondsVariance) + wait := time.Duration(basePublishSeconds-publishSecondsVariance/2+n) * time.Second oldLocation := lastKnownLocation.Load() location, err := geolookup.LookupIPWithClient("", client.Load().(*http.Client)) @@ -92,7 +92,7 @@ func write() { } else { log.Errorf("Unable to get current location: %v", err) // When retrying after a failure, wait a different amount of time - retryWait := time.Duration(math.Pow(2, float64(consecutiveFailures)) * float64(retryWaitTime)) + retryWait := time.Duration(math.Pow(2, float64(consecutiveFailures))*float64(retryWaitMillis)) * time.Millisecond if retryWait < wait { wait = retryWait } diff --git a/src/github.com/getlantern/fronted/dialer.go b/src/github.com/getlantern/fronted/dialer.go index 2b88d6b0d..e224ecd37 100644 --- a/src/github.com/getlantern/fronted/dialer.go +++ b/src/github.com/getlantern/fronted/dialer.go @@ -200,7 +200,10 @@ func (d *dialer) HttpClientUsing(masquerade *Masquerade) *http.Client { } func (d *dialer) DirectHttpClient() *http.Client { - masquerade := d.masquerades.nextVerified() + var masquerade *Masquerade + if d.masquerades != nil { + masquerade = d.masquerades.nextVerified() + } return &http.Client{ Transport: &http.Transport{ Dial: func(network, addr string) (net.Conn, error) {