diff options
author | Grzegorz Bizon <grzesiek.bizon@gmail.com> | 2019-12-05 19:16:19 +0300 |
---|---|---|
committer | Grzegorz Bizon <grzesiek.bizon@gmail.com> | 2019-12-05 19:16:19 +0300 |
commit | 0e115c3f3f99e95aa6778c25381c85e4a3f7ad36 (patch) | |
tree | 8dfc661f7b36dc4bee20831a4757eba02b30fafb /internal/source | |
parent | 07ac526ac57e71b6cfd6b810709b1255b65dd113 (diff) |
Extract all configurable caching values to const.go
Diffstat (limited to 'internal/source')
-rw-r--r-- | internal/source/gitlab/cache/cache.go | 42 | ||||
-rw-r--r-- | internal/source/gitlab/cache/const.go | 11 | ||||
-rw-r--r-- | internal/source/gitlab/cache/entry.go | 5 | ||||
-rw-r--r-- | internal/source/gitlab/cache/memstore.go | 4 | ||||
-rw-r--r-- | internal/source/gitlab/cache/retriever.go | 3 |
5 files changed, 35 insertions, 30 deletions
diff --git a/internal/source/gitlab/cache/cache.go b/internal/source/gitlab/cache/cache.go index c538419f..85f44e11 100644 --- a/internal/source/gitlab/cache/cache.go +++ b/internal/source/gitlab/cache/cache.go @@ -23,25 +23,29 @@ func NewCache(client api.Client) *Cache { // Resolve is going to return a Lookup based on a domain name. The caching // algorithm works as follows: // - We first check if the cache entry exists, and if it is up-to-date. If it -// is fresh we return the &Lookup entry from cache and it is a cache hit. -// - If entry is not up-to-date, we schedule an asynchronous retrieval of the -// latest configuration we are going to obtain through the API, and we -// immediately return an old value, to avoid blocking clients. In this case -// it is also a cache hit. -// - If cache entry has not been populated with a Lookup information yet, we -// block all the clients and make them wait until we retrieve the Lookup from -// the GitLab API. +// is fresh we return the lookup entry from cache and it is a cache hit. +// - If entry is not up-to-date, what means that it has been created in a cache +// more than `shortCacheExpiry` duration ago, we schedule an asynchronous +// retrieval of the latest configuration we are going to obtain through the +// API, and we immediately return an old value, to avoid blocking clients. In +// this case it is also a cache hit. +// - If cache entry has not been populated with a lookup information yet, we +// block all the clients and make them wait until we retrieve the lookup from +// the GitLab API. Clients should not wait for longer than +// `retrievalTimeout`. It is a cache miss. // -// We are going to retrieve a Lookup from GitLab API using Retriever type. In +// We are going to retrieve a lookup from GitLab API using a retriever type. In // case of failures (when GitLab API client returns an error) we will retry the -// operation a few times. In case of an erroneous response, we will cache it, -// and it get recycled as every other cache entry. +// operation a few times, waiting `maxRetrievalInterval` in between, total +// amount of requests is defined as `maxRetrievalRetries`. In case of an +// erroneous response, we will cache it, and it get recycled as every other +// cache entry. // // Examples: // 1. Everything works // - a client opens pages // - we create a new cache entry -// - cache entry needs warm up +// - cache entry needs a warm up // - a client waits until we retrieve a lookup // - we successfuly retrieve a lookup // - we cache this response @@ -49,19 +53,19 @@ func NewCache(client api.Client) *Cache { // 2. A domain does not exist // - a client opens pages // - we create a new cache entry -// - cache entry needs warm up +// - cache entry needs a warm up // - a client waits until we retrieve a lookup -// - GitLab responded with a lookup that contains information about domain -// not being found -// - we cache this response -// - we pass this lookup upstream to all theclient +// - GitLab responded with a lookup and 204 HTTP status +// - we cache this response with domain being `nil` +// - we pass this lookup upstream to all the clients // 3. GitLab is not responding // - a client opens pages // - we create a new cache entry -// - cache entry needs warm up +// - cache entry needs a warm up // - a client waits until we retrieve a lookup // - GitLab does not respond or responds with an error -// - we retry this information a few times +// - we retry this retrieval every `maxRetrievalInterval` +// - we retry this retrieval `maxRetrievalRetries` in total // - we create a lookup that contains information about an error // - we cache this response // - we pass this lookup upstream to all the clients diff --git a/internal/source/gitlab/cache/const.go b/internal/source/gitlab/cache/const.go new file mode 100644 index 00000000..5b11dc23 --- /dev/null +++ b/internal/source/gitlab/cache/const.go @@ -0,0 +1,11 @@ +package cache + +import "time" + +var ( + shortCacheExpiry = 30 * time.Second + longCacheExpiry = 10 * time.Minute + retrievalTimeout = 5 * time.Second + maxRetrievalRetries = 3 + maxRetrievalInterval = time.Second +) diff --git a/internal/source/gitlab/cache/entry.go b/internal/source/gitlab/cache/entry.go index 45a89db5..9d81cc85 100644 --- a/internal/source/gitlab/cache/entry.go +++ b/internal/source/gitlab/cache/entry.go @@ -9,11 +9,6 @@ import ( "gitlab.com/gitlab-org/gitlab-pages/internal/source/gitlab/api" ) -var ( - retrievalTimeout = 5 * time.Second - shortCacheExpiry = 30 * time.Second -) - // Entry represents a cache object that can be retrieved asynchronously and // holds a pointer to *api.Lookup when the domain lookup has been retrieved // successfully diff --git a/internal/source/gitlab/cache/memstore.go b/internal/source/gitlab/cache/memstore.go index 8b22bde0..49e920ae 100644 --- a/internal/source/gitlab/cache/memstore.go +++ b/internal/source/gitlab/cache/memstore.go @@ -12,11 +12,9 @@ type memstore struct { mux *sync.Mutex } -var expiration = 10 * time.Minute - func newMemStore() Store { return &memstore{ - store: cache.New(expiration, time.Minute), + store: cache.New(longCacheExpiry, time.Minute), mux: &sync.Mutex{}, } } diff --git a/internal/source/gitlab/cache/retriever.go b/internal/source/gitlab/cache/retriever.go index 65d8249b..2db2eca8 100644 --- a/internal/source/gitlab/cache/retriever.go +++ b/internal/source/gitlab/cache/retriever.go @@ -9,9 +9,6 @@ import ( "gitlab.com/gitlab-org/gitlab-pages/internal/source/gitlab/api" ) -var maxRetrievalInterval = time.Second -var maxRetrievalRetries = 3 - // Retriever is an utility type that performs an HTTP request with backoff in // case of errors type Retriever struct { |