Skip to content
Open
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
2 changes: 1 addition & 1 deletion router-tests/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ require (
github.com/wundergraph/cosmo/router v0.0.0-20260710155145-803a4bc06d92
github.com/wundergraph/cosmo/router-plugin v0.0.0-20250808194725-de123ba1c65e
github.com/wundergraph/cosmo/speedtrap v0.0.0-00010101000000-000000000000
github.com/wundergraph/graphql-go-tools/v2 v2.14.2
github.com/wundergraph/graphql-go-tools/v2 v2.14.3-0.20260804183019-ff8f37c5cd22
go.opentelemetry.io/otel v1.44.0
go.opentelemetry.io/otel/sdk v1.44.0
go.opentelemetry.io/otel/sdk/metric v1.44.0
Expand Down
4 changes: 2 additions & 2 deletions router-tests/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -386,8 +386,8 @@ github.com/wundergraph/astjson v1.1.0 h1:xORDosrZ87zQFJwNGe/HIHXqzpdHOFmqWgykCLV
github.com/wundergraph/astjson v1.1.0/go.mod h1:h12D/dxxnedtLzsKyBLK7/Oe4TAoGpRVC9nDpDrZSWw=
github.com/wundergraph/go-arena v1.3.0 h1:n0ng5a1vbd8YGq1u3rMr0vPU5f6AZ1BXIiUhL1UIok8=
github.com/wundergraph/go-arena v1.3.0/go.mod h1:ROOysEHWJjLQ8FSfNxZCziagb7Qw2nXY3/vgKRh7eWw=
github.com/wundergraph/graphql-go-tools/v2 v2.14.2 h1:zPqpmGdVLtTR3nc/H7kPWgMN+a/DR5ccFLGyS1WtxeQ=
github.com/wundergraph/graphql-go-tools/v2 v2.14.2/go.mod h1:zREIKLmpjfNcGSubndaW/913r0Y8XbbYOXQeZFkwHdo=
github.com/wundergraph/graphql-go-tools/v2 v2.14.3-0.20260804183019-ff8f37c5cd22 h1:kU1zoy548x0x3I/LdyWEBJTDryguQjOv4R4fl4gqd30=
github.com/wundergraph/graphql-go-tools/v2 v2.14.3-0.20260804183019-ff8f37c5cd22/go.mod h1:zREIKLmpjfNcGSubndaW/913r0Y8XbbYOXQeZFkwHdo=
github.com/xrash/smetrics v0.0.0-20250705151800-55b8f293f342 h1:FnBeRrxr7OU4VvAzt5X7s6266i6cSVkkFPS0TuXWbIg=
github.com/xrash/smetrics v0.0.0-20250705151800-55b8f293f342/go.mod h1:Ohn+xnUBiLI6FVj/9LpzZWtj1/D6lUovWYBkxHVV3aM=
github.com/yosida95/uritemplate/v3 v3.0.2 h1:Ed3Oyj9yrmi9087+NczuL5BwkIc4wvTb5zIM+UJPGz4=
Expand Down
2 changes: 1 addition & 1 deletion router/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ require (
github.com/tidwall/gjson v1.18.0
github.com/tidwall/sjson v1.2.5
github.com/twmb/franz-go v1.16.1
github.com/wundergraph/graphql-go-tools/v2 v2.14.2
github.com/wundergraph/graphql-go-tools/v2 v2.14.3-0.20260806200345-7e74c071ddad
// Do not upgrade, it renames attributes we rely on
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.68.0
go.opentelemetry.io/contrib/propagators/b3 v1.44.0
Expand Down
6 changes: 4 additions & 2 deletions router/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -334,8 +334,10 @@ github.com/wundergraph/astjson v1.1.0 h1:xORDosrZ87zQFJwNGe/HIHXqzpdHOFmqWgykCLV
github.com/wundergraph/astjson v1.1.0/go.mod h1:h12D/dxxnedtLzsKyBLK7/Oe4TAoGpRVC9nDpDrZSWw=
github.com/wundergraph/go-arena v1.3.0 h1:n0ng5a1vbd8YGq1u3rMr0vPU5f6AZ1BXIiUhL1UIok8=
github.com/wundergraph/go-arena v1.3.0/go.mod h1:ROOysEHWJjLQ8FSfNxZCziagb7Qw2nXY3/vgKRh7eWw=
github.com/wundergraph/graphql-go-tools/v2 v2.14.2 h1:zPqpmGdVLtTR3nc/H7kPWgMN+a/DR5ccFLGyS1WtxeQ=
github.com/wundergraph/graphql-go-tools/v2 v2.14.2/go.mod h1:zREIKLmpjfNcGSubndaW/913r0Y8XbbYOXQeZFkwHdo=
github.com/wundergraph/graphql-go-tools/v2 v2.14.3-0.20260804183019-ff8f37c5cd22 h1:kU1zoy548x0x3I/LdyWEBJTDryguQjOv4R4fl4gqd30=
github.com/wundergraph/graphql-go-tools/v2 v2.14.3-0.20260804183019-ff8f37c5cd22/go.mod h1:zREIKLmpjfNcGSubndaW/913r0Y8XbbYOXQeZFkwHdo=
github.com/wundergraph/graphql-go-tools/v2 v2.14.3-0.20260806200345-7e74c071ddad h1:aviYivHMoDkr2w5nJklPUj3wBMnxwge1UIoIHY36kTY=
github.com/wundergraph/graphql-go-tools/v2 v2.14.3-0.20260806200345-7e74c071ddad/go.mod h1:zREIKLmpjfNcGSubndaW/913r0Y8XbbYOXQeZFkwHdo=
github.com/yosida95/uritemplate/v3 v3.0.2 h1:Ed3Oyj9yrmi9087+NczuL5BwkIc4wvTb5zIM+UJPGz4=
github.com/yosida95/uritemplate/v3 v3.0.2/go.mod h1:ILOh0sOhIJR3+L/8afwt/kE++YT040gmv5BQTMR2HP4=
github.com/yuin/gopher-lua v1.1.1 h1:kYKnWBjvbNP4XLT3+bPEwAXJx262OhaHDWDVOPjL46M=
Expand Down
116 changes: 116 additions & 0 deletions router/pkg/entitycaching/cache/in_memory/in_memory.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package in_memory

import (
"bytes"
"context"
"fmt"
"math"
"sync"

"github.com/dgraph-io/ristretto/v2"
enginecache "github.com/wundergraph/graphql-go-tools/v2/pkg/entitycaching"
)

const entryCost = 1
const maxSize = math.MaxInt32

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd probably go for something much smaller, like a million or so. This blows OOM way before we reach maxInt32


type InMemoryCache struct {
cache *ristretto.Cache[string, []byte]
// closeOnce keeps Close idempotent, so two shutdown paths reaching it is
// not a panic on a channel ristretto has already closed.
closeOnce sync.Once
}

// NewInMemoryCache returns a cache holding at most maxEntries entries. The
// caller owns it and must Close it.
func NewInMemoryCache(maxEntries int64) (*InMemoryCache, error) {
if maxEntries <= 0 {
return nil, fmt.Errorf("in memory entity cache needs a positive size, got %d", maxEntries)
}
if maxEntries > maxSize {
return nil, fmt.Errorf("in memory entity cache size is too large: %d", maxEntries)
}

cache, err := ristretto.NewCache(&ristretto.Config[string, []byte]{
MaxCost: maxEntries,
NumCounters: maxEntries * 10,
IgnoreInternalCost: true,
BufferItems: 64,
})
if err != nil {
return nil, fmt.Errorf("failed to create in memory entity cache: %w", err)
}

return &InMemoryCache{cache: cache}, nil
}

// GetMany returns the entries it found, keyed by the key they were asked for.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since it's an implementation of the engines interface method I'd simply write

Suggested change
// GetMany returns the entries it found, keyed by the key they were asked for.
// GetMany implements enginecache.GetMany.

Any behaviour, guard logic and rules should be documented on the interface method godic as a contract for any implementor (imo)

func (c *InMemoryCache) GetMany(ctx context.Context, keys []string) (map[string]enginecache.Item, error) {
if err := ctx.Err(); err != nil {
return nil, err
}

if len(keys) == 0 {
return nil, nil
}

results := make(map[string]enginecache.Item, len(keys))
for _, key := range keys {
// In case there are duplicate keys, ignore duplicate fetches
if _, found := results[key]; found {
continue
}

// We need to do getTTL OR store the expiration date on the entry
// however doing the latter still means that its a different value
// from the expiration date the ristretto uses internally
value, getOk := c.cache.Get(key)
ttl, ttlOk := c.cache.GetTTL(key)

// Either the key does not exist, or it just expired now
if !getOk || !ttlOk || ttl <= 0 {
continue
}

results[key] = enginecache.Item{Key: key, Value: bytes.Clone(value), TTL: ttl}
}

return results, nil
}

// SetMany stores every item, all of which must carry a positive TTL. A single
// item without one fails the whole batch with an ErrMissingTTL and nothing is
// stored. It otherwise only fails on a cancelled context.
Comment on lines +81 to +83

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since it's an implementation of the engines interface method I'd simply write

Suggested change
// SetMany stores every item, all of which must carry a positive TTL. A single
// item without one fails the whole batch with an ErrMissingTTL and nothing is
// stored. It otherwise only fails on a cancelled context.
// SetMany implements enginecache.SetMany.

Any behaviour, guard logic and rules should be documented on the interface method godic as a contract for any implementor (imo)

func (c *InMemoryCache) SetMany(ctx context.Context, items []enginecache.Item) error {
if err := ctx.Err(); err != nil {
return err
}

if len(items) == 0 {
return nil
}

// Map used for deduplications
last := make(map[string]enginecache.Item, len(items))

for _, item := range items {
if item.TTL <= 0 {
return fmt.Errorf("%w: key %q", enginecache.ErrMissingTTL, item.Key)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On the lib version currently referred to by the routers go.mod there is no ErrMissingTTL. I think you need to bumb the engine version

}
// In case user sends same key, use the last entry to save
last[item.Key] = item
}

for _, item := range last {
c.cache.SetWithTTL(item.Key, bytes.Clone(item.Value), entryCost, item.TTL)
}

c.cache.Wait()
return nil
}

// Close stops the goroutines ristretto runs behind the cache. It is safe to
// call more than once, but not while a GetMany or a SetMany is in flight.
Comment on lines +112 to +113

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but not while a GetMany or a SetMany is in flight

We can't control this from here right? I checked and Ristretto will actually panic if you close the cache during a concurrent write. Can you secure it with an RWMutex?

func (c *InMemoryCache) Close() {
c.closeOnce.Do(c.cache.Close)
}
Loading