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

Improve CreateOperationContext() in Executor by using sync.Pool #3370

Closed
wants to merge 4 commits into from
Closed
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
44 changes: 34 additions & 10 deletions graphql/executor/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package executor

import (
"context"
"sync"

"github.com/vektah/gqlparser/v2/ast"
"github.com/vektah/gqlparser/v2/gqlerror"
Expand All @@ -19,6 +20,7 @@ type Executor struct {
es graphql.ExecutableSchema
extensions []graphql.HandlerExtension
ext extensions
pool sync.Pool

errorPresenter graphql.ErrorPresenterFunc
recoverFunc graphql.RecoverFunc
Expand All @@ -32,12 +34,23 @@ var _ graphql.GraphExecutor = &Executor{}
// New creates a new Executor with the given schema, and a default error and
// recovery callbacks, and no query cache or extensions.
func New(es graphql.ExecutableSchema) *Executor {
ext := processExtensions(nil)
e := &Executor{
pool: sync.Pool{
New: func() any {
return &graphql.OperationContext{
DisableIntrospection: true,
RecoverFunc: graphql.DefaultRecover,
ResolverMiddleware: ext.fieldMiddleware,
RootResolverMiddleware: ext.rootFieldMiddleware,
}
},
},
es: es,
errorPresenter: graphql.DefaultErrorPresenter,
recoverFunc: graphql.DefaultRecover,
queryCache: graphql.NoCache[*ast.QueryDocument]{},
ext: processExtensions(nil),
ext: ext,
parserTokenLimit: parserTokenNoLimit,
}
return e
Expand All @@ -47,16 +60,27 @@ func (e *Executor) CreateOperationContext(
ctx context.Context,
params *graphql.RawParams,
) (*graphql.OperationContext, gqlerror.List) {
opCtx := &graphql.OperationContext{
DisableIntrospection: true,
RecoverFunc: e.recoverFunc,
ResolverMiddleware: e.ext.fieldMiddleware,
RootResolverMiddleware: e.ext.rootFieldMiddleware,
Stats: graphql.Stats{
Read: params.ReadTime,
OperationStart: graphql.GetStartTime(ctx),
},
opCtx := e.pool.Get().(*graphql.OperationContext)
defer e.pool.Put(opCtx)

if params.Headers.Get("Connection") == "Upgrade" && params.Headers.Get("Upgrade") == "websocket" {
copyCtx := *opCtx
copyCtx.DisableIntrospection = true
copyCtx.RecoverFunc = e.recoverFunc
copyCtx.ResolverMiddleware = e.ext.fieldMiddleware
copyCtx.RootResolverMiddleware = e.ext.rootFieldMiddleware
copyCtx.Stats.Read = params.ReadTime
copyCtx.Stats.OperationStart = graphql.GetStartTime(ctx)
opCtx = &copyCtx
} else {
opCtx.DisableIntrospection = true
opCtx.RecoverFunc = e.recoverFunc
opCtx.ResolverMiddleware = e.ext.fieldMiddleware
opCtx.RootResolverMiddleware = e.ext.rootFieldMiddleware
opCtx.Stats.Read = params.ReadTime
opCtx.Stats.OperationStart = graphql.GetStartTime(ctx)
}

ctx = graphql.WithOperationContext(ctx, opCtx)

for _, p := range e.ext.operationParameterMutators {
Expand Down
2 changes: 2 additions & 0 deletions graphql/handler/transport/http_post.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ func (h POST) Do(w http.ResponseWriter, r *http.Request, exec graphql.GraphExecu
Start: start,
End: graphql.Now(),
}
params.Headers.Set("Connection", "Upgrade")
params.Headers.Set("Upgrade", "websocket")

bodyString, err := getRequestBody(r)
if err != nil {
Expand Down
10 changes: 8 additions & 2 deletions graphql/handler/transport/websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,13 @@ func (c *wsConnection) closeOnCancel(ctx context.Context) {

func (c *wsConnection) subscribe(start time.Time, msg *message) {
ctx := graphql.StartOperationTrace(c.ctx)
var params *graphql.RawParams
params := &graphql.RawParams{
Headers: http.Header{
"Upgrade": []string{"websocket"},
"Connection": []string{"Upgrade"},
},
}

if err := jsonDecode(bytes.NewReader(msg.payload), &params); err != nil {
c.sendError(msg.id, &gqlerror.Error{Message: "invalid json"})
c.complete(msg.id)
Expand All @@ -386,7 +392,7 @@ func (c *wsConnection) subscribe(start time.Time, msg *message) {
Start: start,
End: graphql.Now(),
}

rc, err := c.exec.CreateOperationContext(ctx, params)
if err != nil {
resp := c.exec.DispatchError(graphql.WithOperationContext(ctx, rc), err)
Expand Down
Loading