Support Custom Timeouts for Queries

This commit is contained in:
Goutham Veeramachaneni 2017-03-06 23:02:21 +05:30
parent 6634984a38
commit c6b329c55b
No known key found for this signature in database
GPG Key ID: F1C217E8E9023CAD

View File

@ -169,12 +169,24 @@ func (api *API) query(r *http.Request) (interface{}, *apiError) {
ts = api.now()
}
ctx := api.context(r)
if to := r.FormValue("timeout"); to != "" {
var cancel context.CancelFunc
timeout, err := parseDuration(to)
if err != nil {
return nil, &apiError{errorBadData, err}
}
ctx, cancel = context.WithTimeout(ctx, timeout)
defer cancel()
}
qry, err := api.QueryEngine.NewInstantQuery(r.FormValue("query"), ts)
if err != nil {
return nil, &apiError{errorBadData, err}
}
res := qry.Exec(api.context(r))
res := qry.Exec(ctx)
if res.Err != nil {
switch res.Err.(type) {
case promql.ErrQueryCanceled:
@ -221,12 +233,24 @@ func (api *API) queryRange(r *http.Request) (interface{}, *apiError) {
return nil, &apiError{errorBadData, err}
}
ctx := api.context(r)
if to := r.FormValue("timeout"); to != "" {
var cancel context.CancelFunc
timeout, err := parseDuration(to)
if err != nil {
return nil, &apiError{errorBadData, err}
}
ctx, cancel = context.WithTimeout(ctx, timeout)
defer cancel()
}
qry, err := api.QueryEngine.NewRangeQuery(r.FormValue("query"), start, end, step)
if err != nil {
return nil, &apiError{errorBadData, err}
}
res := qry.Exec(api.context(r))
res := qry.Exec(ctx)
if res.Err != nil {
switch res.Err.(type) {
case promql.ErrQueryCanceled: