Compare commits

...
10 Commits
3 changed files with 149 additions and 16 deletions
+10 -6
View File
@@ -4,6 +4,7 @@ import (
"crypto/tls"
"time"
"github.com/allegro/bigcache"
"github.com/lukaszraczylo/go-simple-graphql/utils/concurrency"
"github.com/lukaszraczylo/go-simple-graphql/utils/logger"
@@ -14,7 +15,6 @@ import (
"os"
"strings"
"github.com/allegro/bigcache"
"github.com/gookit/goutil/envutil"
)
@@ -43,11 +43,7 @@ func NewConnection() *BaseClient {
b.retries.delay = envutil.GetInt("GRAPHQL_RETRIES_DELAY", 300)
if b.cache.enabled {
var err error
b.cache.client, err = bigcache.NewBigCache(bigcache.DefaultConfig(time.Duration(b.cache.ttl) * time.Second))
if err != nil {
b.Logger.Error(b, "Error while creating cache client;", "error", err.Error())
}
b.enableCache()
}
b_tmp_log_level := envutil.Getenv("LOG_LEVEL", "info")
@@ -102,3 +98,11 @@ func (b *BaseClient) SetEndpoint(endpoint string) {
func (b *BaseClient) SetOutput(output string) {
b.responseType = output
}
func (b *BaseClient) enableCache() {
var err error
b.cache.client, err = bigcache.NewBigCache(bigcache.DefaultConfig(time.Duration(b.cache.ttl) * time.Second))
if err != nil {
b.Logger.Error(b, "Error while creating cache client;", "error", err.Error())
}
}
+21 -9
View File
@@ -73,18 +73,25 @@ func (c *BaseClient) Query(queryContent string, queryVariables interface{}, quer
var queryHash string
var cachedResponse []byte
cbc := reflect.ValueOf(*c).Interface().(BaseClient)
cacheBaseClient := &cbc
cacheBaseClient := c
defer func() {
cacheBaseClient = nil
}()
query := cacheBaseClient.NewQuery(queryContent, queryVariables)
// Check for library specific headers
if len(queryHeaders) > 0 {
queryHeadersModified := c.parseQueryHeaders(queryHeaders)
queryHeadersModified, cache_enabled := cacheBaseClient.parseQueryHeaders(queryHeaders)
// compare if there are any changes
if !reflect.DeepEqual(queryHeadersModified, queryHeaders) {
cacheBaseClient.Logger.Debug(cacheBaseClient, "Headers modified, creating a new client")
if queryHeadersModified["gqlcache"] != nil && c.cache.enabled != cache_enabled {
cacheBaseClient.Logger.Debug(cacheBaseClient, "Switching cache on as per single-request header")
cbc := reflect.ValueOf(*c).Interface().(BaseClient)
cacheBaseClient := &cbc
cacheBaseClient.enableCache()
}
}
}
@@ -92,7 +99,7 @@ func (c *BaseClient) Query(queryContent string, queryVariables interface{}, quer
queryHash = strutil.Md5(fmt.Sprintf("%s-%+v", query.compiledQuery, queryHeaders))
cacheBaseClient.Logger.Debug(cacheBaseClient, "Hash calculated;", "hash:", queryHash)
cachedResponse = cacheBaseClient.cacheLookup(queryHash)
cachedResponse = c.cacheLookup(queryHash)
if cachedResponse != nil {
cacheBaseClient.Logger.Debug(cacheBaseClient, "Found cached response")
return cacheBaseClient.decodeResponse(cachedResponse), nil
@@ -113,8 +120,12 @@ func (c *BaseClient) Query(queryContent string, queryVariables interface{}, quer
return nil, err
}
if cacheBaseClient.cache.enabled {
cacheBaseClient.cache.client.Set(queryHash, jsonData)
if cacheBaseClient.cache.enabled && jsonData != nil && queryHash != "" {
c.cache.client.Set(queryHash, jsonData)
} else if cacheBaseClient.cache.enabled && jsonData == nil {
cacheBaseClient.Logger.Warn(cacheBaseClient, "Response is empty")
} else if cacheBaseClient.cache.enabled && queryHash == "" {
cacheBaseClient.Logger.Warn(cacheBaseClient, "Query hash is empty")
}
return cacheBaseClient.decodeResponse(jsonData), err
@@ -140,11 +151,12 @@ func (c *BaseClient) decodeResponse(jsonData []byte) any {
}
}
func (c *BaseClient) parseQueryHeaders(queryHeaders map[string]interface{}) (returnHeaders map[string]interface{}) {
func (c *BaseClient) parseQueryHeaders(queryHeaders map[string]interface{}) (returnHeaders map[string]interface{}, cache_enabled bool) {
returnHeaders = make(map[string]interface{})
for k, v := range queryHeaders {
if k == "gqlcache" {
c.cache.enabled, _ = strconv.ParseBool(fmt.Sprintf("%v", v))
cache_enabled = true
continue
}
if k == "gqlretries" {
@@ -153,5 +165,5 @@ func (c *BaseClient) parseQueryHeaders(queryHeaders map[string]interface{}) (ret
}
returnHeaders[k] = v
}
return returnHeaders
return returnHeaders, cache_enabled
}
+118 -1
View File
@@ -1,6 +1,7 @@
package gql
import (
"math/rand"
"reflect"
"testing"
@@ -333,7 +334,7 @@ func TestBaseClient_parseQueryHeaders(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := NewConnection()
if gotReturnHeaders := c.parseQueryHeaders(tt.args.queryHeaders); !reflect.DeepEqual(gotReturnHeaders, tt.wantReturnHeaders) {
if gotReturnHeaders, _ := c.parseQueryHeaders(tt.args.queryHeaders); !reflect.DeepEqual(gotReturnHeaders, tt.wantReturnHeaders) {
t.Errorf("BaseClient.parseQueryHeaders() = %v, want %v", gotReturnHeaders, tt.wantReturnHeaders)
}
})
@@ -413,3 +414,119 @@ func TestBaseClient_QueryCache(t *testing.T) {
})
}
}
func TestBaseClient_QueryCacheRandomizedRace(t *testing.T) {
type fields struct {
graphql_endpoint string
cache_enabled bool
}
type args struct {
queryVariables interface{}
queryHeaders map[string]interface{}
queryContent string
}
tests := []struct {
want any
args args
name string
fields fields
wantErr bool
}{
{
name: "Test QueryCache - enabled",
fields: fields{
graphql_endpoint: "https://spacex-production.up.railway.app/",
},
args: args{
queryContent: `query Dragons {
dragons {
name
}
}`,
queryHeaders: map[string]interface{}{
"x-apollo-operation-name": "Missions-Potato-Test-Golang",
"content-type": "application/json",
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := NewConnection()
if tt.fields.graphql_endpoint != "" {
c.endpoint = tt.fields.graphql_endpoint
}
for i := 0; i < 10; i++ {
c.cache.enabled = rand.Intn(2) == 0
go func() {
_, err := c.Query(tt.args.queryContent, tt.args.queryVariables, tt.args.queryHeaders)
if !tt.wantErr {
assert.NoError(t, err)
} else {
assert.Error(t, err)
}
}()
}
})
}
}
func TestBaseClient_QueryCacheRandomizedRaceViaHeader(t *testing.T) {
type fields struct {
graphql_endpoint string
cache_enabled bool
}
type args struct {
queryVariables interface{}
queryHeaders map[string]interface{}
queryContent string
}
tests := []struct {
want any
args args
name string
fields fields
wantErr bool
}{
{
name: "Test QueryCache - enabled",
fields: fields{
graphql_endpoint: "https://spacex-production.up.railway.app/",
},
args: args{
queryContent: `query Dragons {
dragons {
name
}
}`,
queryHeaders: map[string]interface{}{
"x-apollo-operation-name": "Missions-Potato-Test-Golang",
"content-type": "application/json",
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := NewConnection()
if tt.fields.graphql_endpoint != "" {
c.endpoint = tt.fields.graphql_endpoint
}
for i := 0; i < 10; i++ {
tt.args.queryHeaders["gqlcache"] = rand.Intn(2) == 0
go func() {
_, err := c.Query(tt.args.queryContent, tt.args.queryVariables, tt.args.queryHeaders)
if !tt.wantErr {
assert.NoError(t, err)
} else {
assert.Error(t, err)
}
}()
}
})
}
}