Fix my networks cache validation

This commit is contained in:
Daniel 2021-04-19 14:06:00 +02:00
parent 6f9d17bba2
commit edd0d6e834
2 changed files with 13 additions and 8 deletions

View file

@ -51,8 +51,9 @@ func GetAssignedGlobalAddresses() (ipv4 []net.IP, ipv6 []net.IP, err error) {
} }
var ( var (
myNetworks []*net.IPNet myNetworks []*net.IPNet
myNetworksLock sync.Mutex myNetworksLock sync.Mutex
myNetworksNetworkChangedFlag = GetNetworkChangedFlag()
) )
// IsMyIP returns whether the given unicast IP is currently configured on the local host. // IsMyIP returns whether the given unicast IP is currently configured on the local host.
@ -69,8 +70,12 @@ func IsMyIP(ip net.IP) (yes bool, err error) {
myNetworksLock.Lock() myNetworksLock.Lock()
defer myNetworksLock.Unlock() defer myNetworksLock.Unlock()
// Check for match. // Check if the network changed.
if mine, matched := checkIfMyIP(ip); matched { if myNetworksNetworkChangedFlag.IsSet() {
// Reset changed flag.
myNetworksNetworkChangedFlag.Refresh()
} else if mine, matched := checkIfMyIP(ip); matched {
// If the network did not change, check for match immediately.
return mine, nil return mine, nil
} }

View file

@ -13,7 +13,7 @@ func init() {
flag.BoolVar(&privileged, "privileged", false, "run tests that require root/admin privileges") flag.BoolVar(&privileged, "privileged", false, "run tests that require root/admin privileges")
} }
func TestGetApproximateInternetLocation(t *testing.T) { func TestGetInternetLocation(t *testing.T) {
if testing.Short() { if testing.Short() {
t.Skip() t.Skip()
} }
@ -21,9 +21,9 @@ func TestGetApproximateInternetLocation(t *testing.T) {
t.Skip("skipping privileged test, active with -privileged argument") t.Skip("skipping privileged test, active with -privileged argument")
} }
loc, err := GetInternetLocation() loc, ok := GetInternetLocation()
if err != nil { if !ok {
t.Fatalf("GetApproximateInternetLocation failed: %s", err) t.Fatal("GetApproximateInternetLocation failed")
} }
t.Logf("GetApproximateInternetLocation: %+v", loc) t.Logf("GetApproximateInternetLocation: %+v", loc)
} }