Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add timeout option while running queries over HTTP #3238

Merged
merged 1 commit into from
Apr 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions dgraph/cmd/alpha/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,20 @@ func queryHandler(w http.ResponseWriter, r *http.Request) {
ctx := context.WithValue(context.Background(), query.DebugKey, d)
ctx = attachAccessJwt(ctx, r)

// Timeout is expected to be in millisecond
paramTimeout := r.URL.Query().Get("timeout")
if paramTimeout != "" {
timeout, err := time.ParseDuration(paramTimeout)
if err != nil {
x.SetStatusWithData(w, x.Error, err.Error())
return
}

var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, timeout)
defer cancel()
}

if req.StartTs == 0 {
// If be is set, run this as a best-effort query.
be, _ := strconv.ParseBool(r.URL.Query().Get("be"))
Expand Down
36 changes: 31 additions & 5 deletions dgraph/cmd/alpha/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,24 @@ import (
"github.com/dgraph-io/dgraph/z"
)

type respError struct {
Code string `json:"code"`
Message string `json:"message"`
}

type res struct {
Data json.RawMessage `json:"data"`
Extensions *query.Extensions `json:"extensions,omitempty"`
Errors []respError `json:"errors,omitempty"`
}

func queryWithGz(q string, gzReq bool, gzResp bool) (string, *http.Response, error) {
func queryWithGz(q string, gzReq bool, gzResp bool, timeout string) (
string, *http.Response, error) {

url := addr + "/query"
if timeout != "" {
url = url + fmt.Sprintf("?timeout=%v", timeout)
}

var buf *bytes.Buffer
if gzReq {
Expand Down Expand Up @@ -99,6 +110,11 @@ func queryWithGz(q string, gzReq bool, gzResp bool) (string, *http.Response, err
var r res
x.Check(json.Unmarshal(body, &r))

// Check for errors
if len(r.Errors) != 0 {
return "", nil, errors.New(r.Errors[0].Message)
}

// Remove the extensions.
r2 := res{
Data: r.Data,
Expand Down Expand Up @@ -463,23 +479,33 @@ func TestHttpCompressionSupport(t *testing.T) {
err := runMutation(m1)
require.NoError(t, err)

data, resp, err := queryWithGz(q1, false, false)
data, resp, err := queryWithGz(q1, false, false, "")
require.NoError(t, err)
require.Equal(t, r1, data)
require.Empty(t, resp.Header.Get("Content-Encoding"))

data, resp, err = queryWithGz(q1, false, true)
data, resp, err = queryWithGz(q1, false, true, "")
require.NoError(t, err)
require.Equal(t, r1, data)
require.Equal(t, "gzip", resp.Header.Get("Content-Encoding"))

data, resp, err = queryWithGz(q1, true, false)
data, resp, err = queryWithGz(q1, true, false, "")
require.NoError(t, err)
require.Equal(t, r1, data)
require.Empty(t, resp.Header.Get("Content-Encoding"))

data, resp, err = queryWithGz(q1, true, true)
data, resp, err = queryWithGz(q1, true, true, "")
require.NoError(t, err)
require.Equal(t, r1, data)
require.Equal(t, "gzip", resp.Header.Get("Content-Encoding"))

// query with timeout
data, resp, err = queryWithGz(q1, false, false, "1ms")
require.EqualError(t, err, ": context deadline exceeded")
require.Equal(t, "", data)

data, resp, err = queryWithGz(q1, false, false, "1s")
require.NoError(t, err)
require.Equal(t, r1, data)
require.Empty(t, resp.Header.Get("Content-Encoding"))
}