Skip to content

Commit

Permalink
Merge branch 'master' into rhall-limit-error-msgs
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanhall07 authored May 25, 2021
2 parents aa7b455 + 3e96cb7 commit 52cb1b4
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,20 @@ func ParseLimit(req *http.Request, header, formValue string, defaultLimit int) (
return defaultLimit, nil
}

// ParseInstanceMultiple parses request instance multiple from header.
func ParseInstanceMultiple(req *http.Request, defaultValue float32) (float32, error) {
if str := req.Header.Get(headers.LimitInstanceMultipleHeader); str != "" {
v, err := strconv.ParseFloat(str, 32)
if err != nil {
err = fmt.Errorf(
"could not parse instance multiple: input=%s, err=%w", str, err)
return 0, err
}
return float32(v), nil
}
return defaultValue, nil
}

// ParseRequireExhaustive parses request limit require exhaustive from header or
// query string.
func ParseRequireExhaustive(req *http.Request, defaultValue bool) (bool, error) {
Expand Down Expand Up @@ -214,9 +228,13 @@ func (b fetchOptionsBuilder) newFetchOptions(
if err != nil {
return nil, nil, err
}

fetchOpts.SeriesLimit = seriesLimit
fetchOpts.InstanceMultiple = b.opts.Limits.InstanceMultiple

instanceMultiple, err := ParseInstanceMultiple(req, b.opts.Limits.InstanceMultiple)
if err != nil {
return nil, nil, err
}
fetchOpts.InstanceMultiple = instanceMultiple

docsLimit, err := ParseLimit(req, headers.LimitMaxDocsHeader,
"docsLimit", b.opts.Limits.DocsLimit)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -475,3 +475,20 @@ func TestTimeoutParseWithGetRequestParam(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, timeout, time.Millisecond)
}

func TestInstanceMultiple(t *testing.T) {
req := httptest.NewRequest("GET", "/", nil)
m, err := ParseInstanceMultiple(req, 2.0)
require.NoError(t, err)
require.Equal(t, float32(2.0), m)

req.Header.Set(headers.LimitInstanceMultipleHeader, "3.0")
m, err = ParseInstanceMultiple(req, 2.0)
require.NoError(t, err)
require.Equal(t, float32(3.0), m)

req.Header.Set(headers.LimitInstanceMultipleHeader, "blah")
_, err = ParseInstanceMultiple(req, 2.0)
require.Error(t, err)
require.Contains(t, err.Error(), "could not parse instance multiple")
}
3 changes: 3 additions & 0 deletions src/x/headers/headers.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ const (
// the number of time series returned by each storage node.
LimitMaxSeriesHeader = M3HeaderPrefix + "Limit-Max-Series"

// LimitInstanceMultipleHeader overrides the PerQueryLimitsConfiguration.InstanceMultiple for the request.
LimitInstanceMultipleHeader = M3HeaderPrefix + "Limit-Instance-Multiple"

// LimitMaxDocsHeader is the M3 limit docs header that limits
// the number of docs returned by each storage node.
LimitMaxDocsHeader = M3HeaderPrefix + "Limit-Max-Docs"
Expand Down

0 comments on commit 52cb1b4

Please sign in to comment.