diff options
author | Jaime Martinez <jmartinez@gitlab.com> | 2020-07-15 10:47:50 +0300 |
---|---|---|
committer | Jaime Martinez <jmartinez@gitlab.com> | 2020-07-24 07:49:29 +0300 |
commit | 8ae49b3597e2ceaebfe4da66b555db4293a2e00c (patch) | |
tree | 92743acd8dbfa8db149d4b22c8f8f41552acc056 /internal/source/gitlab | |
parent | 11eaa4f759a746ee621f85362bae0512cb96497a (diff) |
Enable polling in the source domains
Enables gitlabClient.Poll in the domain source init.
Diffstat (limited to 'internal/source/gitlab')
-rw-r--r-- | internal/source/gitlab/api/client.go | 3 | ||||
-rw-r--r-- | internal/source/gitlab/api/resolver.go | 5 | ||||
-rw-r--r-- | internal/source/gitlab/cache/cache.go | 4 | ||||
-rw-r--r-- | internal/source/gitlab/cache/cache_test.go | 2 | ||||
-rw-r--r-- | internal/source/gitlab/client/client_poll.go | 10 | ||||
-rw-r--r-- | internal/source/gitlab/client/client_poll_test.go | 2 | ||||
-rw-r--r-- | internal/source/gitlab/client/client_stub.go | 5 | ||||
-rw-r--r-- | internal/source/gitlab/gitlab.go | 5 |
8 files changed, 31 insertions, 5 deletions
diff --git a/internal/source/gitlab/api/client.go b/internal/source/gitlab/api/client.go index 7206e25a..4a6c0893 100644 --- a/internal/source/gitlab/api/client.go +++ b/internal/source/gitlab/api/client.go @@ -2,10 +2,13 @@ package api import ( "context" + "time" ) // Client represents an interface we use to retrieve information from GitLab type Client interface { // GetLookup retrives an VirtualDomain from GitLab API and wraps it into Lookup GetLookup(ctx context.Context, domain string) Lookup + + Poll(retries int, interval time.Duration, errCh chan error) } diff --git a/internal/source/gitlab/api/resolver.go b/internal/source/gitlab/api/resolver.go index 061a1ddd..78cbc20b 100644 --- a/internal/source/gitlab/api/resolver.go +++ b/internal/source/gitlab/api/resolver.go @@ -2,11 +2,14 @@ package api import ( "context" + "time" ) // Resolver represents an interface we use to retrieve information from GitLab // in a more generic way. It can be a concrete API client or cached client. type Resolver interface { - // Resolve retrives an VirtualDomain from GitLab API and wraps it into Lookup + // Resolve retrieves an VirtualDomain from GitLab API and wraps it into Lookup Resolve(ctx context.Context, domain string) *Lookup + // Poll test + Poll(retries int, interval time.Duration, errCh chan error) } diff --git a/internal/source/gitlab/cache/cache.go b/internal/source/gitlab/cache/cache.go index c8d166b5..de91e8b6 100644 --- a/internal/source/gitlab/cache/cache.go +++ b/internal/source/gitlab/cache/cache.go @@ -109,3 +109,7 @@ func (c *Cache) Resolve(ctx context.Context, domain string) *api.Lookup { metrics.DomainsSourceCacheMiss.Inc() return entry.Retrieve(ctx, c.client) } + +func (c *Cache) Poll(retries int, interval time.Duration, errCh chan error) { + c.client.Poll(retries, interval, errCh) +} diff --git a/internal/source/gitlab/cache/cache_test.go b/internal/source/gitlab/cache/cache_test.go index 7a12cd3b..27bd449a 100644 --- a/internal/source/gitlab/cache/cache_test.go +++ b/internal/source/gitlab/cache/cache_test.go @@ -69,6 +69,8 @@ func (c *client) GetLookup(ctx context.Context, _ string) api.Lookup { return lookup } +func (c *client) Poll(int, time.Duration, chan error) {} + func withTestCache(config resolverConfig, cacheConfig *cacheConfig, block func(*Cache, *client)) { var chanSize int diff --git a/internal/source/gitlab/client/client_poll.go b/internal/source/gitlab/client/client_poll.go index bdbfb345..cca7b731 100644 --- a/internal/source/gitlab/client/client_poll.go +++ b/internal/source/gitlab/client/client_poll.go @@ -3,16 +3,18 @@ package client import ( "fmt" "time" + + log "github.com/sirupsen/logrus" ) const ( // DefaultPollingMaxRetries to be used by Poll - DefaultPollingMaxRetries = 30 + DefaultPollingMaxRetries = 10 // DefaultPollingInterval to be used by Poll DefaultPollingInterval = 10 * time.Second ) -// Poll tries to call the /internal/pages/status API endpoint for +// Poll tries to call the /internal/pages/status API endpoint once plus // `retries` every `interval`. // TODO: should we consider using an exponential back-off approach? // https://pkg.go.dev/github.com/cenkalti/backoff/v4?tab=doc#pkg-examples @@ -21,10 +23,14 @@ func (gc *Client) Poll(retries int, interval time.Duration, errCh chan error) { var err error for i := 0; i <= retries; i++ { + log.Info("polling GitLab internal pages status API") err = gc.Status() if err == nil { + log.Info("GitLab internal pages status API connected successfully") + // return as soon as we connect to the API errCh <- nil + return } time.Sleep(interval) diff --git a/internal/source/gitlab/client/client_poll_test.go b/internal/source/gitlab/client/client_poll_test.go index 59da32c5..f92ac998 100644 --- a/internal/source/gitlab/client/client_poll_test.go +++ b/internal/source/gitlab/client/client_poll_test.go @@ -74,7 +74,6 @@ func TestClient_Poll(t *testing.T) { go client.Poll(tt.retries, tt.interval, errCh) - // go func() { select { case err := <-errCh: if tt.wantErr { @@ -88,7 +87,6 @@ func TestClient_Poll(t *testing.T) { t.Logf("%s timed out", tt.name) t.FailNow() } - // }() }) } } diff --git a/internal/source/gitlab/client/client_stub.go b/internal/source/gitlab/client/client_stub.go index 604f127b..c3879a33 100644 --- a/internal/source/gitlab/client/client_stub.go +++ b/internal/source/gitlab/client/client_stub.go @@ -4,6 +4,7 @@ import ( "context" "encoding/json" "os" + "time" "gitlab.com/gitlab-org/gitlab-pages/internal/source/gitlab/api" ) @@ -35,3 +36,7 @@ func (c StubClient) GetLookup(ctx context.Context, host string) api.Lookup { return lookup } + +func (c StubClient) Poll(r int, i time.Duration, errCh chan error) { + errCh <- nil +} diff --git a/internal/source/gitlab/gitlab.go b/internal/source/gitlab/gitlab.go index 12da9af1..c5095fb7 100644 --- a/internal/source/gitlab/gitlab.go +++ b/internal/source/gitlab/gitlab.go @@ -6,6 +6,7 @@ import ( "net/http" "path" "strings" + "time" "gitlab.com/gitlab-org/gitlab-pages/internal/domain" "gitlab.com/gitlab-org/gitlab-pages/internal/request" @@ -94,3 +95,7 @@ func (g *Gitlab) Resolve(r *http.Request) (*serving.Request, error) { return &serving.Request{Serving: defaultServing()}, errors.New("could not match lookup path") } + +func (g *Gitlab) Poll(retries int, interval time.Duration, errCh chan error) { + go g.client.Poll(retries, interval, errCh) +} |