Skip to content

Commit

Permalink
feat: Add configurable size limit for parsers in pool (#881)
Browse files Browse the repository at this point in the history
  • Loading branch information
jfroundjian authored Sep 11, 2024
1 parent 227e8d8 commit 3c6d147
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 9 deletions.
2 changes: 1 addition & 1 deletion v2/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ require (
github.com/tidwall/gjson v1.17.0
github.com/tidwall/sjson v1.2.5
github.com/vektah/gqlparser/v2 v2.5.11
github.com/wundergraph/astjson v0.0.0-20240827102346-5fc60fe30f9c
github.com/wundergraph/astjson v0.0.0-20240910140849-bb15f94bd362
go.uber.org/atomic v1.11.0
go.uber.org/zap v1.26.0
golang.org/x/sync v0.7.0
Expand Down
6 changes: 2 additions & 4 deletions v2/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,8 @@ github.com/tidwall/sjson v1.2.5 h1:kLy8mja+1c9jlljvWTlSazM7cKDRfJuR/bOJhcY5NcY=
github.com/tidwall/sjson v1.2.5/go.mod h1:Fvgq9kS/6ociJEDnK0Fk1cpYF4FIW6ZF7LAe+6jwd28=
github.com/vektah/gqlparser/v2 v2.5.11 h1:JJxLtXIoN7+3x6MBdtIP59TP1RANnY7pXOaDnADQSf8=
github.com/vektah/gqlparser/v2 v2.5.11/go.mod h1:1rCcfwB2ekJofmluGWXMSEnPMZgbxzwj6FaZ/4OT8Cc=
github.com/wundergraph/astjson v0.0.0-20240822164222-c1cca919e2c8 h1:UiT9ykzZavKJXdtpqa1Uix/8BM/Rq2EULCqfcSZdbxo=
github.com/wundergraph/astjson v0.0.0-20240822164222-c1cca919e2c8/go.mod h1:eOTL6acwctsN4F3b7YE+eE2t8zcJ/doLm9sZzsxxxrE=
github.com/wundergraph/astjson v0.0.0-20240827102346-5fc60fe30f9c h1:Hsda/35Y00GOdIQE3ox3rT3kaU4cb2n9Xyuyylop1dk=
github.com/wundergraph/astjson v0.0.0-20240827102346-5fc60fe30f9c/go.mod h1:eOTL6acwctsN4F3b7YE+eE2t8zcJ/doLm9sZzsxxxrE=
github.com/wundergraph/astjson v0.0.0-20240910140849-bb15f94bd362 h1:MxNSJqQFJyhKwU4xPj6diIRLm+oY1wNbAZW0jJpikBE=
github.com/wundergraph/astjson v0.0.0-20240910140849-bb15f94bd362/go.mod h1:eOTL6acwctsN4F3b7YE+eE2t8zcJ/doLm9sZzsxxxrE=
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
go.uber.org/atomic v1.5.0/go.mod h1:sABNBOSYdrvTF6hTgEIbc7YasKWGhgEQZyfxyTvoXHQ=
go.uber.org/atomic v1.11.0 h1:ZvwS0R+56ePWxUNi+Atn9dWONBPp/AUETXlHW0DxSjE=
Expand Down
2 changes: 1 addition & 1 deletion v2/pkg/engine/resolve/loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -856,7 +856,7 @@ func BenchmarkLoader_LoadGraphQLResponseData(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
loader.Free()
resolvable.Reset()
resolvable.Reset(0)
err := resolvable.Init(ctx, nil, ast.OperationTypeQuery)
if err != nil {
b.Fatal(err)
Expand Down
4 changes: 2 additions & 2 deletions v2/pkg/engine/resolve/resolvable.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ func (r *Resolvable) parseJSON(data []byte) (*astjson.Value, error) {
return parser.ParseBytes(data)
}

func (r *Resolvable) Reset() {
func (r *Resolvable) Reset(maxRecyclableParserSize int) {
for i := range r.parsers {
parsers.Put(r.parsers[i])
parsers.PutIfSizeLessThan(r.parsers[i], maxRecyclableParserSize)
r.parsers[i] = nil
}
r.parsers = r.parsers[:0]
Expand Down
7 changes: 6 additions & 1 deletion v2/pkg/engine/resolve/resolve.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,11 @@ type ResolverOptions struct {
OmitSubgraphErrorLocations bool
// OmitSubgraphErrorExtensions omits the extensions field of Subgraph Errors
OmitSubgraphErrorExtensions bool

// MaxRecyclableParserSize limits the size of the Parser that can be recycled back into the Pool.
// If set to 0, no limit is applied
// This helps keep the Heap size more maintainable if you regularly perform large queries.
MaxRecyclableParserSize int
}

// New returns a new Resolver, ctx.Done() is used to cancel all active subscriptions & streams
Expand Down Expand Up @@ -167,7 +172,7 @@ func (r *Resolver) getTools() (time.Duration, *tools) {

func (r *Resolver) putTools(t *tools) {
t.loader.Free()
t.resolvable.Reset()
t.resolvable.Reset(r.options.MaxRecyclableParserSize)
r.tools.Put(t)
r.maxConcurrency <- struct{}{}
}
Expand Down

0 comments on commit 3c6d147

Please sign in to comment.