oxy icon indicating copy to clipboard operation
oxy copied to clipboard

Feature request: rate limit on HTTP query parameter

Open negz opened this issue 7 years ago • 1 comments

Hello,

At Planet Labs we're considering replacing an internally developed edge router with Traefik. Our edge router is very similar to Traefik, including its use of oxy. We have one rate limiting use case that's not supported by oxy out of the box; we'd like to rate limit on HTTP URL query parameter. We need to rate limit on query parameters because some of our legacy clients provide their API key as a query param.

We'd effectively want to add the following:

// NewExtractor creates a new SourceExtractor
func NewExtractor(variable string) (SourceExtractor, error) {
	if variable == "client.ip" {
		return ExtractorFunc(extractClientIP), nil
	}
	if variable == "request.host" {
		return ExtractorFunc(extractHost), nil
	}
	if strings.HasPrefix(variable, "request.header.") {
		header := strings.TrimPrefix(variable, "request.header.")
		if len(header) == 0 {
			return nil, fmt.Errorf("wrong header: %s", header)
		}
		return makeHeaderExtractor(header), nil
	}
	if strings.HasPrefix(variable, "request.queryparam.") {
		param := strings.TrimPrefix(variable, "request.queryparam.")
		if len(param) == 0 {
			return nil, fmt.Errorf("wrong param: %s", header)
		}
		return makeQueryParamExtractor(param), nil
	}
	return nil, fmt.Errorf("unsupported limiting variable: '%s'", variable)
}

func makeQueryParamExtractor(param string) SourceExtractor {
	return ExtractorFunc(func(req *http.Request) (string, int64, error) {
		return req.URL.Query().Get(param), 1, nil
	})
}

Would you accept a PR with such functionality?

negz avatar Sep 29 '18 00:09 negz

Ping. Any thoughts on this?

negz avatar Nov 08 '18 03:11 negz