Skip to content

Commit

Permalink
Merge pull request #184 from RomainBelorgey/sourceip
Browse files Browse the repository at this point in the history
Always return the source ip even if no limits
  • Loading branch information
silvin-lubecki authored Sep 15, 2021
2 parents 133c07e + a4de1d9 commit a01a01b
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 a01a01b

Please sign in to comment.