Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix location cache gets never updated sometimes #6048

Merged
merged 1 commit into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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