Skip to content

Commit

Permalink
Merge pull request #6048 from mysteriumnetwork/fix-location-cache
Browse files Browse the repository at this point in the history
Fix location cache gets never updated sometimes
  • Loading branch information
soffokl authored May 16, 2024
2 parents c97221a + bc3b56e commit 365054f
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
4 changes: 3 additions & 1 deletion core/location/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func NewCache(resolver Resolver, pub publisher, expiry time.Duration) *Cache {
}

func (c *Cache) needsRefresh() bool {
return c.lastFetched.IsZero() || c.lastFetched.After(time.Now().Add(-c.expiry))
return c.lastFetched.IsZero() || c.lastFetched.Before(time.Now().Add(-c.expiry))
}

func (c *Cache) fetchAndSave() (locationstate.Location, error) {
Expand All @@ -72,6 +72,7 @@ func (c *Cache) fetchAndSave() (locationstate.Location, error) {
c.location = loc
c.lastFetched = time.Now()
}

return loc, err
}

Expand All @@ -90,6 +91,7 @@ func (c *Cache) DetectLocation() (locationstate.Location, error) {
if !c.needsRefresh() {
return c.location, nil
}

return c.fetchAndSave()
}

Expand Down
4 changes: 2 additions & 2 deletions core/location/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,15 @@ func TestCache_needsRefresh(t *testing.T) {
name: "returns true if expired",
want: true,
fields: fields{
lastFetched: time.Now().Add(time.Second * -59),
lastFetched: time.Now().Add(time.Second * -69),
expiry: time.Minute * 1,
},
},
{
name: "returns false if updated recently",
want: false,
fields: fields{
lastFetched: time.Now().Add(time.Second * -61),
lastFetched: time.Now().Add(time.Second * -50),
expiry: time.Minute * 1,
},
},
Expand Down

0 comments on commit 365054f

Please sign in to comment.