Skip to content

Commit

Permalink
Use go 1.18 compatible atomic operations
Browse files Browse the repository at this point in the history
  • Loading branch information
UnAfraid committed May 30, 2023
1 parent 5ad8c74 commit 62c23dc
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 19 deletions.
18 changes: 9 additions & 9 deletions codegen/generated!.gotpl
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@
}

func (e *executableSchema) Complexity(typeName, field string, childComplexity int, rawArgs map[string]interface{}) (int, bool) {
ec := executionContext{nil, e, atomic.Int32{}, atomic.Int32{}, nil}
ec := executionContext{nil, e, 0, 0, nil}
_ = ec
{{ if not .Config.OmitComplexity -}}
switch typeName + "." + field {
Expand Down Expand Up @@ -138,7 +138,7 @@

func (e *executableSchema) Exec(ctx context.Context) graphql.ResponseHandler {
rc := graphql.GetOperationContext(ctx)
ec := executionContext{rc, e, atomic.Int32{}, atomic.Int32{}, make(chan graphql.DeferredResult)}
ec := executionContext{rc, e, 0, 0, make(chan graphql.DeferredResult)}
inputUnmarshalMap := graphql.BuildUnmarshalerMap(
{{- range $input := .Inputs -}}
{{ if not $input.HasUnmarshal }}
Expand All @@ -164,9 +164,9 @@
data = ec._{{.QueryRoot.Name}}(ctx, rc.Operation.SelectionSet)
{{- end }}
} else {
if ec.pendingDeferred.Load() > 0 {
if atomic.LoadInt32(&ec.pendingDeferred) > 0 {
result := <-ec.deferredResults
ec.pendingDeferred.Add(-1)
atomic.AddInt32(&ec.pendingDeferred, -1)
data = result.Result
response.Path = result.Path
response.Label = result.Label
Expand All @@ -178,8 +178,8 @@
var buf bytes.Buffer
data.MarshalGQL(&buf)
response.Data = buf.Bytes()
if ec.deferred.Load() > 0 {
hasNext := ec.pendingDeferred.Load() > 0
if atomic.LoadInt32(&ec.deferred) > 0 {
hasNext := atomic.LoadInt32(&ec.pendingDeferred) > 0
response.HasNext = &hasNext
}

Expand Down Expand Up @@ -240,13 +240,13 @@
type executionContext struct {
*graphql.OperationContext
*executableSchema
deferred atomic.Int32
pendingDeferred atomic.Int32
deferred int32
pendingDeferred int32
deferredResults chan graphql.DeferredResult
}

func (ec *executionContext) processDeferredGroup(dg graphql.DeferredGroup) {
ec.pendingDeferred.Add(1)
atomic.AddInt32(&ec.pendingDeferred, 1)
go func () {
ctx := graphql.WithFreshResponseContext(dg.Context)
dg.FieldSet.Dispatch(ctx)
Expand Down
2 changes: 1 addition & 1 deletion codegen/object.gotpl
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func (ec *executionContext) _{{$object.Name}}(ctx context.Context, sel ast.Selec
out.Dispatch(ctx)
if out.Invalids > 0 { return graphql.Null }

ec.deferred.Add(int32(len(deferred)))
atomic.AddInt32(&ec.deferred, int32(len(deferred)))

for label, dfs := range deferred {
ec.processDeferredGroup(graphql.DeferredGroup{
Expand Down
18 changes: 9 additions & 9 deletions codegen/root_.gotpl
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func (e *executableSchema) Schema() *ast.Schema {
}

func (e *executableSchema) Complexity(typeName, field string, childComplexity int, rawArgs map[string]interface{}) (int, bool) {
ec := executionContext{nil, e, atomic.Int32{}, atomic.Int32{}, nil}
ec := executionContext{nil, e, 0, 0, nil}
_ = ec
{{- if not .Config.OmitComplexity }}
switch typeName + "." + field {
Expand Down Expand Up @@ -111,7 +111,7 @@ func (e *executableSchema) Complexity(typeName, field string, childComplexity in

func (e *executableSchema) Exec(ctx context.Context) graphql.ResponseHandler {
rc := graphql.GetOperationContext(ctx)
ec := executionContext{rc, e, atomic.Int32{}, atomic.Int32{}, make(chan graphql.DeferredResult)}
ec := executionContext{rc, e, 0, 0, make(chan graphql.DeferredResult)}
inputUnmarshalMap := graphql.BuildUnmarshalerMap(
{{- range $input := .Inputs -}}
{{ if not $input.HasUnmarshal }}
Expand All @@ -137,9 +137,9 @@ func (e *executableSchema) Exec(ctx context.Context) graphql.ResponseHandler {
data = ec._{{.QueryRoot.Name}}(ctx, rc.Operation.SelectionSet)
{{- end }}
} else {
if ec.pendingDeferred.Load() > 0 {
if atomic.LoadInt32(&ec.pendingDeferred) > 0 {
result := <-ec.deferredResults
ec.pendingDeferred.Add(-1)
atomic.AddInt32(&ec.pendingDeferred, -1)
data = result.Result
response.Path = result.Path
response.Label = result.Label
Expand All @@ -151,8 +151,8 @@ func (e *executableSchema) Exec(ctx context.Context) graphql.ResponseHandler {
var buf bytes.Buffer
data.MarshalGQL(&buf)
response.Data = buf.Bytes()
if ec.deferred.Load() > 0 {
hasNext := ec.pendingDeferred.Load() > 0
if atomic.LoadInt32(&ec.deferred) > 0 {
hasNext := atomic.LoadInt32(&ec.pendingDeferred) > 0
response.HasNext = &hasNext
}

Expand Down Expand Up @@ -213,13 +213,13 @@ func (e *executableSchema) Exec(ctx context.Context) graphql.ResponseHandler {
type executionContext struct {
*graphql.OperationContext
*executableSchema
deferred atomic.Int32
pendingDeferred atomic.Int32
deferred int32
pendingDeferred int32
deferredResults chan graphql.DeferredResult
}

func (ec *executionContext) processDeferredGroup(dg graphql.DeferredGroup) {
ec.pendingDeferred.Add(1)
atomic.AddInt32(&ec.pendingDeferred, 1)
go func () {
ctx := graphql.WithFreshResponseContext(dg.Context)
dg.FieldSet.Dispatch(ctx)
Expand Down

0 comments on commit 62c23dc

Please sign in to comment.