mirror of
https://github.com/lukaszraczylo/go-simple-graphql.git
synced 2026-10-14 00:58:32 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
55182f2f3d
|
@@ -4,6 +4,7 @@ import (
|
|||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/allegro/bigcache"
|
||||||
"github.com/lukaszraczylo/go-simple-graphql/utils/concurrency"
|
"github.com/lukaszraczylo/go-simple-graphql/utils/concurrency"
|
||||||
"github.com/lukaszraczylo/go-simple-graphql/utils/logger"
|
"github.com/lukaszraczylo/go-simple-graphql/utils/logger"
|
||||||
|
|
||||||
@@ -14,7 +15,6 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/allegro/bigcache"
|
|
||||||
"github.com/gookit/goutil/envutil"
|
"github.com/gookit/goutil/envutil"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -43,11 +43,7 @@ func NewConnection() *BaseClient {
|
|||||||
b.retries.delay = envutil.GetInt("GRAPHQL_RETRIES_DELAY", 300)
|
b.retries.delay = envutil.GetInt("GRAPHQL_RETRIES_DELAY", 300)
|
||||||
|
|
||||||
if b.cache.enabled {
|
if b.cache.enabled {
|
||||||
var err error
|
b.enableCache()
|
||||||
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_tmp_log_level := envutil.Getenv("LOG_LEVEL", "info")
|
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) {
|
func (b *BaseClient) SetOutput(output string) {
|
||||||
b.responseType = output
|
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())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -80,11 +80,14 @@ func (c *BaseClient) Query(queryContent string, queryVariables interface{}, quer
|
|||||||
|
|
||||||
// Check for library specific headers
|
// Check for library specific headers
|
||||||
if len(queryHeaders) > 0 {
|
if len(queryHeaders) > 0 {
|
||||||
queryHeadersModified := c.parseQueryHeaders(queryHeaders)
|
queryHeadersModified := cacheBaseClient.parseQueryHeaders(queryHeaders)
|
||||||
// compare if there are any changes
|
// compare if there are any changes
|
||||||
|
|
||||||
if !reflect.DeepEqual(queryHeadersModified, queryHeaders) {
|
if !reflect.DeepEqual(queryHeadersModified, queryHeaders) {
|
||||||
cacheBaseClient.Logger.Debug(cacheBaseClient, "Headers modified, creating a new client")
|
if cacheBaseClient.cache.enabled != c.cache.enabled && cacheBaseClient.cache.enabled {
|
||||||
|
cacheBaseClient.Logger.Debug(cacheBaseClient, "Switching cache on as per single-request header")
|
||||||
|
cacheBaseClient.enableCache()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -472,3 +472,61 @@ func TestBaseClient_QueryCacheRandomizedRace(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user