mirror of
https://github.com/lukaszraczylo/graphql-monitoring-proxy.git
synced 2026-11-19 10:55:04 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
de59ce03a2 | ||
|
|
8c5f229bbd |
@@ -434,6 +434,8 @@ query MyProducts @cached(refresh: true) {
|
||||
}
|
||||
```
|
||||
|
||||
To skip the cache for one request, send `X-Skip-Cache: true` or `Cache-Control: no-cache` (or `no-store`). The proxy then sends the request to the backend, does not store the response and does not merge it with an identical request in flight. Use it for reads that gate money, quotas or permissions.
|
||||
|
||||
#### Memory-Aware Caching
|
||||
|
||||
Starting with version `0.26.0`, the memory cache implementation has been enhanced with memory-aware features to prevent out-of-memory situations:
|
||||
|
||||
@@ -25,7 +25,7 @@ require (
|
||||
github.com/redis/go-redis/v9 v9.23.0
|
||||
github.com/sony/gobreaker/v2 v2.4.0
|
||||
github.com/stretchr/testify v1.12.1
|
||||
github.com/valyala/fasthttp v1.78.0
|
||||
github.com/valyala/fasthttp v1.79.0
|
||||
go.opentelemetry.io/otel v1.47.0
|
||||
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.47.0
|
||||
go.opentelemetry.io/otel/sdk v1.47.0
|
||||
@@ -47,7 +47,7 @@ require (
|
||||
github.com/grpc-ecosystem/grpc-gateway/v2 v2.33.0 // indirect
|
||||
github.com/jackc/pgpassfile v1.0.0 // indirect
|
||||
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
|
||||
github.com/jackc/puddle/v2 v2.2.3 // indirect
|
||||
github.com/jackc/puddle/v2 v2.2.4 // indirect
|
||||
github.com/klauspost/compress v1.20.4 // indirect
|
||||
github.com/mattn/go-colorable v0.1.16 // indirect
|
||||
github.com/mattn/go-isatty v0.0.24 // indirect
|
||||
|
||||
@@ -64,8 +64,8 @@ github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 h1:iCEnooe7Ulw
|
||||
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM=
|
||||
github.com/jackc/pgx/v5 v5.13.0 h1:Hl08k64maU0nrRyVUEbwCnK6/gT0EhMrtAqBHz9/gA4=
|
||||
github.com/jackc/pgx/v5 v5.13.0/go.mod h1:u2Fi6no03HCSBYE+ZVdLxbwdu3xyi2vU+K/YNJioSds=
|
||||
github.com/jackc/puddle/v2 v2.2.3 h1:f8HoREISb3l5eO9coPgK7U3hKLhWGokOFVcThl5jrao=
|
||||
github.com/jackc/puddle/v2 v2.2.3/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4=
|
||||
github.com/jackc/puddle/v2 v2.2.4 h1:N6Mz6EGB9DL0bz3OEfxJ7eoOpjRHnnjpOh+G8kbLoG8=
|
||||
github.com/jackc/puddle/v2 v2.2.4/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4=
|
||||
github.com/klauspost/compress v1.20.4 h1:dD0v/bvxtjCBqQu6sx1TNWkalEY5lNTIe+HY9w8TeNI=
|
||||
github.com/klauspost/compress v1.20.4/go.mod h1:LUdAzn7YLVvxLpc7y3V1m40wESHTgc1422pwwBSKYuI=
|
||||
github.com/klauspost/cpuid/v2 v2.4.0 h1:S6Hrbc7+ywsr0r+RLapfGBHfyefhCTwEh3A0tV913Dw=
|
||||
@@ -104,8 +104,8 @@ github.com/tinylib/msgp v1.6.5 h1:iAH6XTP7BpzErjBZlujJQXxA+GmRi2YWs6M4KdLtNbE=
|
||||
github.com/tinylib/msgp v1.6.5/go.mod h1:RSp0LW9oSxFut3KzESt5Voq4GVWyS+PSulT77roAqEA=
|
||||
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
|
||||
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
|
||||
github.com/valyala/fasthttp v1.78.0 h1:tKfLdx7cD8HZOTP1wc2OpbQMbgO1HP12J0y/XMdyW6Q=
|
||||
github.com/valyala/fasthttp v1.78.0/go.mod h1:8NXUY/ZXyjqEWN5cbfIdAAv+ES+5yfcl2EGNo872KzQ=
|
||||
github.com/valyala/fasthttp v1.79.0 h1:32ur91W4kWG+1s6x3oDlP8/KNmjlZZlUzSSC4p9LSWE=
|
||||
github.com/valyala/fasthttp v1.79.0/go.mod h1:NQN+PlgP93+JRPnCiApN5XwnI24oMQf8kuzrxtsTy9A=
|
||||
github.com/valyala/fastrand v1.1.0 h1:f+5HkLW4rsgzdNoleUOB69hyT9IlD2ZQh9GyDMfb5G8=
|
||||
github.com/valyala/fastrand v1.1.0/go.mod h1:HWqCzkrkg6QXT8V2EXWvXCoow7vLwOFN002oeRzjapQ=
|
||||
github.com/valyala/histogram v1.2.0 h1:wyYGAZZt3CpwUiIb9AU/Zbllg1llXyrtApRS815OLoQ=
|
||||
|
||||
@@ -726,7 +726,9 @@ func performProxyRequest(c fiber.Ctx, proxyURL string) error {
|
||||
|
||||
// Check if request coalescing is enabled
|
||||
rc := GetRequestCoalescer()
|
||||
if rc != nil && cfg.RequestCoalescing.Enable {
|
||||
// A cache-bypass read must not join an identical request already in flight:
|
||||
// that one may have started before the write the caller needs to see.
|
||||
if rc != nil && cfg.RequestCoalescing.Enable && !skipCacheRequested(c) {
|
||||
// Use request coalescing to deduplicate identical concurrent requests
|
||||
response, err := rc.Do(cacheKey, func() (*CoalescedResponse, error) {
|
||||
// Execute the actual proxy request
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
@@ -474,6 +475,18 @@ func extractUserInfoFromHeaders(get func(string) string) (string, string, error)
|
||||
return extractedUserID, extractedRoleName, nil
|
||||
}
|
||||
|
||||
// skipCacheRequested reports whether the client asked to bypass the response
|
||||
// cache, with the same headers the upstream engine honours: X-Skip-Cache: true,
|
||||
// or Cache-Control no-cache / no-store. Callers send them on reads that gate
|
||||
// money, quotas or permissions, which must not be served stale.
|
||||
func skipCacheRequested(c fiber.Ctx) bool {
|
||||
if strings.EqualFold(c.Get("X-Skip-Cache"), "true") {
|
||||
return true
|
||||
}
|
||||
cc := strings.ToLower(c.Get(fiber.HeaderCacheControl))
|
||||
return strings.Contains(cc, "no-cache") || strings.Contains(cc, "no-store")
|
||||
}
|
||||
|
||||
// handleCaching manages the caching logic for GraphQL requests
|
||||
func handleCaching(c fiber.Ctx, parsedResult *parseGraphQLQueryResult, userID, userRole string) (bool, error) {
|
||||
// Calculate query hash for cache key - now includes user context for security
|
||||
@@ -499,7 +512,7 @@ func handleCaching(c fiber.Ctx, parsedResult *parseGraphQLQueryResult, userID, u
|
||||
|
||||
// Check if caching is enabled
|
||||
cacheEnabled := parsedResult.cacheRequest || cfg.Cache.CacheEnable || cfg.Cache.CacheRedisEnable
|
||||
if !cacheEnabled {
|
||||
if !cacheEnabled || skipCacheRequested(c) {
|
||||
// No caching, just proxy the request
|
||||
if err := proxyTheRequest(c, parsedResult.activeEndpoint); err != nil {
|
||||
cfg.Monitoring.Increment(libpack_monitoring.MetricsFailed, nil)
|
||||
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -874,3 +875,91 @@ func TestStartHTTPProxy_ErrorHandlerConfigured(t *testing.T) {
|
||||
t.Fatal("StartHTTPProxy did not return after shutdownHTTPProxy")
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleCaching_BypassHeadersSkipCache(t *testing.T) {
|
||||
parseConfig()
|
||||
_ = StartMonitoringServer()
|
||||
|
||||
libpack_cache.EnableCache(&libpack_cache.CacheConfig{
|
||||
Logger: cfg.Logger,
|
||||
TTL: 60,
|
||||
})
|
||||
|
||||
cfgMutex.Lock()
|
||||
origEnable := cfg.Cache.CacheEnable
|
||||
cfg.Cache.CacheEnable = true
|
||||
cfg.Cache.CacheTTL = 60
|
||||
cfgMutex.Unlock()
|
||||
defer func() {
|
||||
cfgMutex.Lock()
|
||||
cfg.Cache.CacheEnable = origEnable
|
||||
cfgMutex.Unlock()
|
||||
}()
|
||||
|
||||
var hits atomic.Int32
|
||||
backend := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
n := hits.Add(1)
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(200)
|
||||
_, _ = fmt.Fprintf(w, `{"data":{"n":%d}}`, n)
|
||||
}))
|
||||
defer backend.Close()
|
||||
|
||||
cfgMutex.Lock()
|
||||
origHost := cfg.Server.HostGraphQL
|
||||
origHostRO := cfg.Server.HostGraphQLReadOnly
|
||||
cfg.Server.HostGraphQL = backend.URL
|
||||
cfg.Server.HostGraphQLReadOnly = backend.URL
|
||||
cfgMutex.Unlock()
|
||||
defer func() {
|
||||
cfgMutex.Lock()
|
||||
cfg.Server.HostGraphQL = origHost
|
||||
cfg.Server.HostGraphQLReadOnly = origHostRO
|
||||
cfgMutex.Unlock()
|
||||
}()
|
||||
|
||||
app := fiber.New()
|
||||
app.Post("/*", processGraphQLRequest)
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
header string
|
||||
value string
|
||||
}{
|
||||
{"X-Skip-Cache", "X-Skip-Cache", "true"},
|
||||
{"Cache-Control no-cache", "Cache-Control", "no-cache"},
|
||||
{"Cache-Control no-store", "Cache-Control", "no-store, max-age=0"},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
libpack_cache.CacheClear()
|
||||
body := `{"query":"query { n }"}`
|
||||
send := func(header, value string) *http.Response {
|
||||
req := httptest.NewRequest("POST", "/v1/graphql", strings.NewReader(body))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
if header != "" {
|
||||
req.Header.Set(header, value)
|
||||
}
|
||||
resp, err := app.Test(req, fiber.TestConfig{Timeout: 10 * time.Second})
|
||||
if err != nil {
|
||||
t.Fatalf("request: %v", err)
|
||||
}
|
||||
_ = resp.Body.Close()
|
||||
return resp
|
||||
}
|
||||
|
||||
send("", "") // warm the cache
|
||||
before := hits.Load()
|
||||
resp := send(tt.header, tt.value)
|
||||
if resp.Header.Get("X-Cache-Hit") == "true" {
|
||||
t.Fatal("bypass request was answered from the cache")
|
||||
}
|
||||
if got := hits.Load(); got != before+1 {
|
||||
t.Fatalf("bypass request must reach the backend: hits %d, want %d", got, before+1)
|
||||
}
|
||||
if resp := send("", ""); resp.Header.Get("X-Cache-Hit") != "true" {
|
||||
t.Fatal("plain request after a bypass must still use the cache")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user