Skip to content

Commit

Permalink
Finally - fixed the cache version for single query if its enabled via…
Browse files Browse the repository at this point in the history
… header.
  • Loading branch information
lukaszraczylo committed Apr 8, 2023
1 parent 841c136 commit 55182f2
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 8 deletions.
16 changes: 10 additions & 6 deletions base.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -14,7 +15,6 @@ import (
"os"
"strings"

"github.com/allegro/bigcache"
"github.com/gookit/goutil/envutil"
)

Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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())
}
}
7 changes: 5 additions & 2 deletions query.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,14 @@ func (c *BaseClient) Query(queryContent string, queryVariables interface{}, quer

// Check for library specific headers
if len(queryHeaders) > 0 {
queryHeadersModified := c.parseQueryHeaders(queryHeaders)
queryHeadersModified := 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 cacheBaseClient.cache.enabled != c.cache.enabled && cacheBaseClient.cache.enabled {
cacheBaseClient.Logger.Debug(cacheBaseClient, "Switching cache on as per single-request header")
cacheBaseClient.enableCache()
}
}
}

Expand Down
58 changes: 58 additions & 0 deletions query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}()
}
})
}
}

0 comments on commit 55182f2

Please sign in to comment.