From d4770b2a37b5b0b77e9240b267141ccc2bf998e0 Mon Sep 17 00:00:00 2001 From: aman bansal Date: Mon, 10 May 2021 12:25:01 +0530 Subject: [PATCH] chore(graphql): fixing query timeouts for graphql queries too (#7796) * fixing query timeouts for graphql queries too * fixing online restore test (cherry picked from commit bb0358e6db5e0731023af4d7a1163d1227959133) --- dgraph/cmd/alpha/run.go | 2 +- edgraph/server.go | 13 +++++++++++-- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/dgraph/cmd/alpha/run.go b/dgraph/cmd/alpha/run.go index ab8ad292a71..5c91d1d467a 100644 --- a/dgraph/cmd/alpha/run.go +++ b/dgraph/cmd/alpha/run.go @@ -207,7 +207,7 @@ they form a Raft group and provide synchronous replication. Flag("txn-abort-after", "Abort any pending transactions older than this duration."+ " The liveness of a transaction is determined by its last mutation."). Flag("shared-instance", "When set to true, it disables ACLs for non-galaxy users. "+ - "It expects the access JWT to be contructed outside dgraph for those users as even "+ + "It expects the access JWT to be constructed outside dgraph for those users as even "+ "login is denied to them. Additionally, this disables access to environment variables"+ "for minio, aws, etc."). String()) diff --git a/edgraph/server.go b/edgraph/server.go index 0ad38916f2b..e3d654adcc8 100644 --- a/edgraph/server.go +++ b/edgraph/server.go @@ -1256,6 +1256,16 @@ func getAuthMode(ctx context.Context) AuthMode { // QueryGraphQL handles only GraphQL queries, neither mutations nor DQL. func (s *Server) QueryGraphQL(ctx context.Context, req *api.Request, field gqlSchema.Field) (*api.Response, error) { + // Add a timeout for queries which don't have a deadline set. We don't want to + // apply a timeout if it's a mutation, that's currently handled by flag + // "txn-abort-after". + if req.GetMutations() == nil && x.Config.QueryTimeout != 0 { + if d, _ := ctx.Deadline(); d.IsZero() { + var cancel context.CancelFunc + ctx, cancel = context.WithTimeout(ctx, x.Config.QueryTimeout) + defer cancel() + } + } // no need to attach namespace here, it is already done by GraphQL layer return s.doQuery(ctx, &Request{req: req, gqlField: field, doAuth: getAuthMode(ctx)}) } @@ -1294,8 +1304,7 @@ func Init() { maxPendingQueries = x.Config.Limit.GetInt64("max-pending-queries") } -func (s *Server) doQuery(ctx context.Context, req *Request) ( - resp *api.Response, rerr error) { +func (s *Server) doQuery(ctx context.Context, req *Request) (resp *api.Response, rerr error) { if ctx.Err() != nil { return nil, ctx.Err() }