diff options
author | Jaime Martinez <jmartinez@gitlab.com> | 2021-03-03 03:57:59 +0300 |
---|---|---|
committer | Jaime Martinez <jmartinez@gitlab.com> | 2021-03-18 03:24:16 +0300 |
commit | 73503977149a52335c39ec376ff434e37ff23434 (patch) | |
tree | 2aa4bea7930dddf90cd17c603473d49fb20fe9c5 /internal/source | |
parent | 33a0158a5429b1730548c101a4c1c9400a8bae88 (diff) |
Add an refreshedOriginalTimestamp field to entry
Diffstat (limited to 'internal/source')
-rw-r--r-- | internal/source/gitlab/cache/entry.go | 31 | ||||
-rw-r--r-- | internal/source/gitlab/cache/entry_test.go | 4 |
2 files changed, 23 insertions, 12 deletions
diff --git a/internal/source/gitlab/cache/entry.go b/internal/source/gitlab/cache/entry.go index 07f45ea8..638fbf20 100644 --- a/internal/source/gitlab/cache/entry.go +++ b/internal/source/gitlab/cache/entry.go @@ -14,16 +14,17 @@ import ( // holds a pointer to *api.Lookup when the domain lookup has been retrieved // successfully type Entry struct { - domain string - created time.Time - retrieve *sync.Once - refresh *sync.Once - mux *sync.RWMutex - retrieved chan struct{} - response *api.Lookup - refreshTimeout time.Duration - expirationTimeout time.Duration - retriever *Retriever + domain string + created time.Time + refreshedOriginalTimestamp time.Time + retrieve *sync.Once + refresh *sync.Once + mux *sync.RWMutex + retrieved chan struct{} + response *api.Lookup + refreshTimeout time.Duration + expirationTimeout time.Duration + retriever *Retriever } func newCacheEntry(domain string, refreshTimeout, entryExpirationTimeout time.Duration, retriever *Retriever) *Entry { @@ -98,7 +99,7 @@ func (e *Entry) refreshFunc(store Store) { // and `e` has not expired. See https://gitlab.com/gitlab-org/gitlab-pages/-/issues/281. if entry.hasTemporaryError() && !e.isExpired() { entry.response = e.response - entry.created = e.created + entry.refreshedOriginalTimestamp = e.created } store.ReplaceOrCreate(e.domain, entry) @@ -113,6 +114,10 @@ func (e *Entry) setResponse(lookup api.Lookup) { } func (e *Entry) isOutdated() bool { + if !e.refreshedOriginalTimestamp.IsZero() { + return time.Since(e.refreshedOriginalTimestamp) > e.refreshTimeout + } + return time.Since(e.created) > e.refreshTimeout } @@ -121,6 +126,10 @@ func (e *Entry) isResolved() bool { } func (e *Entry) isExpired() bool { + if !e.refreshedOriginalTimestamp.IsZero() { + return time.Since(e.refreshedOriginalTimestamp) > e.expirationTimeout + } + return time.Since(e.created) > e.expirationTimeout } diff --git a/internal/source/gitlab/cache/entry_test.go b/internal/source/gitlab/cache/entry_test.go index ddf48a48..4e7c4a14 100644 --- a/internal/source/gitlab/cache/entry_test.go +++ b/internal/source/gitlab/cache/entry_test.go @@ -95,6 +95,7 @@ func TestEntryRefresh(t *testing.T) { t.Run("entry is the same after refreshed lookup has error", func(t *testing.T) { entry := newCacheEntry("test.gitlab.io", cc.entryRefreshTimeout, cc.cacheExpiry, store.(*memstore).retriever) + originalEntryCreated := entry.created ctx, cancel := context.WithTimeout(context.Background(), cc.retrievalTimeout) defer cancel() @@ -111,7 +112,8 @@ func TestEntryRefresh(t *testing.T) { storedEntry := loadEntry(t, "test.gitlab.io", store) require.NoError(t, storedEntry.Lookup().Error, "resolving failed but lookup should still be valid") - require.Equal(t, storedEntry.created.UnixNano(), entry.created.UnixNano(), "refreshed entry should be the same") + require.Equal(t, storedEntry.refreshedOriginalTimestamp.UnixNano(), originalEntryCreated.UnixNano(), + "refreshed entry timestamp should be the same as the original entry created timestamp") require.Equal(t, storedEntry.Lookup(), entry.Lookup(), "lookup should be the same") }) |