From a4de1d91430617c9335063347bece68a26d5f24a Mon Sep 17 00:00:00 2001 From: Romain BELORGEY Date: Wed, 15 Sep 2021 11:26:22 +0200 Subject: [PATCH] Always return the source ip even if no limits Signed-off-by: Romain BELORGEY --- pkg/hub/ratelimiting.go | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/pkg/hub/ratelimiting.go b/pkg/hub/ratelimiting.go index ec3e639..211ba70 100644 --- a/pkg/hub/ratelimiting.go +++ b/pkg/hub/ratelimiting.go @@ -37,8 +37,9 @@ type RateLimits struct { } var ( - first = "https://auth.docker.io/token?service=registry.docker.io&scope=repository:ratelimitpreview/test:pull" - second = "https://registry-1.docker.io/v2/ratelimitpreview/test/manifests/latest" + first = "https://auth.docker.io/token?service=registry.docker.io&scope=repository:ratelimitpreview/test:pull" + second = "https://registry-1.docker.io/v2/ratelimitpreview/test/manifests/latest" + defaultValue = -1 ) // SetURLs change the base urls used to check ratelimiting values @@ -47,7 +48,7 @@ func SetURLs(newFirst, newSecond string) { second = newSecond } -// GetRateLimits returns the rate limits for the authenticated user +// GetRateLimits returns the rate limits for the user func (c *Client) GetRateLimits() (*RateLimits, error) { token, err := tryGetToken(c) if err != nil { @@ -66,9 +67,16 @@ func (c *Client) GetRateLimits() (*RateLimits, error) { limitHeader := resp.Header.Get("Ratelimit-Limit") remainingHeader := resp.Header.Get("Ratelimit-Remaining") + source := resp.Header.Get("docker-Ratelimit-Source") if limitHeader == "" || remainingHeader == "" { - return nil, nil + return &RateLimits{ + Limit: &defaultValue, + LimitWindow: &defaultValue, + Remaining: &defaultValue, + RemainingWindow: &defaultValue, + Source: &source, + }, nil } limit, limitWindow, err := parseLimitHeader(limitHeader) @@ -81,8 +89,6 @@ func (c *Client) GetRateLimits() (*RateLimits, error) { return nil, err } - source := resp.Header.Get("docker-Ratelimit-Source") - return &RateLimits{ Limit: &limit, LimitWindow: &limitWindow,