Skip to content

Commit

Permalink
Always return the source ip even if no limits
Browse files Browse the repository at this point in the history
Signed-off-by: Romain BELORGEY <romain.belorgey@gmail.com>
  • Loading branch information
RomainBelorgey committed Sep 15, 2021
1 parent 133c07e commit a4de1d9
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions pkg/hub/ratelimiting.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand All @@ -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)
Expand All @@ -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,
Expand Down

0 comments on commit a4de1d9

Please sign in to comment.