Skip to content
Merged
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.15.0
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.15.0 h1:AG4l/QZrj1IBqxxPzz40r31fmKBopXQw+UjsgHYu8j4=
github.com/wundergraph/graphql-go-tools/v2 v2.15.0/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.15.0
// 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
4 changes: 2 additions & 2 deletions router/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -334,8 +334,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.15.0 h1:AG4l/QZrj1IBqxxPzz40r31fmKBopXQw+UjsgHYu8j4=
github.com/wundergraph/graphql-go-tools/v2 v2.15.0/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
112 changes: 112 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,112 @@
package in_memory

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

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

const entryCost = 1
const maxSize = 100_000

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 implements enginecache.GetMany.
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 implements enginecache.SetMany.
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)
Comment thread
dkorittki marked this conversation as resolved.
}
// 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 closes the in memory cache
func (c *InMemoryCache) Close() {
c.closeOnce.Do(c.cache.Close)
}
Loading
Loading