diff options
author | Krasimir Angelov <kangelov@gitlab.com> | 2020-02-14 05:47:02 +0300 |
---|---|---|
committer | Krasimir Angelov <kangelov@gitlab.com> | 2020-02-20 04:59:22 +0300 |
commit | 52c07444e3d93e16f268ae304df5c5f9153c9c42 (patch) | |
tree | 1a41a5e1c47bf0a5e33e0833394aef0771dc83a9 | |
parent | 1fe3f552049912c98063d67ca6a0c2da626cb324 (diff) |
Do not lookup domain for healthcheck requets350-do-not-lookup-domain-for-healthcheck
There is none anyway and with the gitlab source this is causing
redundant API requests.
Related to https://gitlab.com/gitlab-org/gitlab-pages/issues/350.
-rw-r--r-- | app.go | 13 | ||||
-rw-r--r-- | app_test.go | 44 |
2 files changed, 56 insertions, 1 deletions
@@ -88,6 +88,13 @@ func (a *theApp) redirectToHTTPS(w http.ResponseWriter, r *http.Request, statusC func (a *theApp) getHostAndDomain(r *http.Request) (string, *domain.Domain, error) { host := request.GetHostWithoutPort(r) + + // For healthcheck check requests we + // do not need to lookup the domain + if a.isHealthCheckRequest(r) { + return host, nil, nil + } + domain, err := a.domain(host) return host, domain, err @@ -125,7 +132,7 @@ func (a *theApp) checkAuthenticationIfNotExists(domain *domain.Domain, w http.Re func (a *theApp) tryAuxiliaryHandlers(w http.ResponseWriter, r *http.Request, https bool, host string, domain *domain.Domain) bool { // short circuit content serving to check for a status page - if r.RequestURI == a.appConfig.StatusPath { + if a.isHealthCheckRequest(r) { a.healthCheck(w, r, https) return true } @@ -468,3 +475,7 @@ func runApp(config appConfig) { func fatal(err error, message string) { log.WithError(err).Fatal(message) } + +func (a *theApp) isHealthCheckRequest(r *http.Request) bool { + return r.RequestURI == a.appConfig.StatusPath +} diff --git a/app_test.go b/app_test.go index d78d6737..a98d30a7 100644 --- a/app_test.go +++ b/app_test.go @@ -4,6 +4,7 @@ import ( "crypto/tls" "fmt" "net/http" + "net/http/httptest" "testing" "github.com/stretchr/testify/require" @@ -58,3 +59,46 @@ func newGetRequestWithScheme(t *testing.T, scheme string, withTLS bool) *http.Re return req } + +func TestIsHealthCheckRequest(t *testing.T) { + tests := []struct { + name string + path string + isHealthcheck bool + }{ + { + name: "Not a healthcheck request", + path: "/foo/bar", + isHealthcheck: false, + }, + { + name: "Healthcheck request", + path: "/-/healthcheck", + isHealthcheck: true, + }, + } + + app := theApp{} + app.appConfig.StatusPath = "/-/healthcheck" + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + r := httptest.NewRequest("GET", tc.path, nil) + require.Equal(t, tc.isHealthcheck, app.isHealthCheckRequest(r)) + }) + } +} + +func TestGetHostAndDomainDoNotLookupDomainForHealthCheckRequest(t *testing.T) { + app := theApp{} + app.appConfig.StatusPath = "/-/healthcheck" + + r := httptest.NewRequest("GET", "/-/healthcheck", nil) + r.Host = "gitlab.com" + + host, domain, err := app.getHostAndDomain(r) + + require.Equal(t, "gitlab.com", host) + require.Nil(t, domain) + require.NoError(t, err) +} |