From ebfaa2b5ed6208689f296d20312d5924c7bd3476 Mon Sep 17 00:00:00 2001 From: Myles Horton Date: Wed, 13 Dec 2023 12:42:08 -0500 Subject: [PATCH 1/2] More robust checks for whether or not to notify listeners about geo lookups (#1341) --- geolookup/geolookup.go | 29 +++++++++++++++++++++++++---- geolookup/geolookup_test.go | 29 +++++++++++++++++++++++++++-- 2 files changed, 52 insertions(+), 6 deletions(-) diff --git a/geolookup/geolookup.go b/geolookup/geolookup.go index 7d7dc2443..ef329eb47 100644 --- a/geolookup/geolookup.go +++ b/geolookup/geolookup.go @@ -153,17 +153,38 @@ func init() { func run() { for range refreshRequest { - gi := lookup() - if gi.IP == GetIP(0) && !gi.FromDisk { - log.Debug("public IP did not change - not notifying watchers") + geoInfo := lookup() + + // Check if the IP has changed and if the old IP is simply cached from + // disk. If it is cached, we should still notify anyone looking for + // a new IP because they won't have been notified of the IP on disk, + // as that is loaded very soon on startup. + if !isNew(geoInfo) { + log.Debug("public IP from network did not change - not notifying watchers") continue } + log.Debug("Setting new geolocation info") mx.Lock() - setGeoInfo(gi, true) + setGeoInfo(geoInfo, true) mx.Unlock() } } +func isNew(newGeoInfo *GeoInfo) bool { + if newGeoInfo == nil { + return false + } + oldGeoInfo, err := GetGeoInfo(0) + if err != nil { + return true + } + if oldGeoInfo == nil { + return true + } + return oldGeoInfo.IP != newGeoInfo.IP || + oldGeoInfo.FromDisk != newGeoInfo.FromDisk +} + func setGeoInfo(gi *GeoInfo, persist bool) { currentGeoInfo.Set(gi) w := watchers diff --git a/geolookup/geolookup_test.go b/geolookup/geolookup_test.go index ca3ea4618..c67d7015a 100644 --- a/geolookup/geolookup_test.go +++ b/geolookup/geolookup_test.go @@ -8,10 +8,9 @@ import ( "testing" "time" - "github.com/stretchr/testify/require" - "github.com/getlantern/eventual/v2" "github.com/getlantern/fronted" + "github.com/stretchr/testify/require" ) const initialInfo = ` @@ -196,3 +195,29 @@ func TestFronted(t *testing.T) { "persisted geolocation information should have changed", ) } + +func TestIsNew(t *testing.T) { + type args struct { + newGeoInfo *GeoInfo + oldGeoInfo *GeoInfo + } + tests := []struct { + name string + args args + want bool + }{ + {"nil new should be not new", args{nil, &GeoInfo{FromDisk: true}}, false}, + {"nil existing should be new", args{&GeoInfo{}, nil}, true}, + {"old from disk should be new", args{&GeoInfo{IP: "1.1.1.1", FromDisk: false}, &GeoInfo{IP: "1.1.1.1", FromDisk: true}}, true}, + {"old not from disk should not be new", args{&GeoInfo{IP: "1.1.1.1", FromDisk: false}, &GeoInfo{IP: "1.1.1.1", FromDisk: false}}, false}, + {"new IP should be new", args{&GeoInfo{IP: "1.1.1.2", FromDisk: false}, &GeoInfo{IP: "1.1.1.1", FromDisk: false}}, true}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + currentGeoInfo.Set(tt.args.oldGeoInfo) + if got := isNew(tt.args.newGeoInfo); got != tt.want { + t.Errorf("isNew() = %v, want %v", got, tt.want) + } + }) + } +} From 4d96d209c574038f237cfe593b0574c106297750 Mon Sep 17 00:00:00 2001 From: Myles Horton Date: Thu, 14 Dec 2023 09:54:12 -0500 Subject: [PATCH 2/2] Update README.md maintainers --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 87cb4e6c3..b126d7f29 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # Lantern [![Go Actions Status](https://github.com/getlantern/flashlight/actions/workflows/go.yml/badge.svg)](https://github.com/getlantern/flashlight/actions) [![Coverage Status](https://coveralls.io/repos/github/getlantern/flashlight/badge.svg?t=C4SaZX)](https://coveralls.io/github/getlantern/flashlight) ## Maintainers -@oxtoacart, @hwh33, @myleshorton +@hwh33, @myleshorton, @reflog This repo contains the core Lantern library as well as the Android and iOS bindings.