Compare commits

...
2 Commits
Author SHA1 Message Date
lukaszraczylo d1406eeac8 Improve http2 / SSL support 2021-12-10 22:33:47 +00:00
lukaszraczylo ea7ec683ef Check for possible cache eviction. 2021-12-03 15:12:02 +00:00
3 changed files with 28 additions and 7 deletions
+5 -5
View File
@@ -15,7 +15,6 @@ package gql
import ( import (
"crypto/tls" "crypto/tls"
"fmt" "fmt"
"net"
"net/http" "net/http"
"os" "os"
"strconv" "strconv"
@@ -77,15 +76,15 @@ func setCacheEnabled() bool {
} }
func NewConnection() *GraphQL { func NewConnection() *GraphQL {
return &GraphQL{ g := GraphQL{
Endpoint: pickGraphqlEndpoint(), Endpoint: pickGraphqlEndpoint(),
HttpClient: &http.Client{ HttpClient: &http.Client{
Transport: &http2.Transport{ Transport: &http2.Transport{
DisableCompression: false, DisableCompression: false,
AllowHTTP: true, AllowHTTP: true,
PingTimeout: 5 * time.Second, PingTimeout: 10 * time.Second,
DialTLS: func(network, addr string, cfg *tls.Config) (net.Conn, error) { TLSClientConfig: &tls.Config{
return net.Dial(network, addr) InsecureSkipVerify: true,
}, },
}, },
}, },
@@ -93,6 +92,7 @@ func NewConnection() *GraphQL {
Cache: setCacheEnabled(), Cache: setCacheEnabled(),
CacheStore: setupCache(), CacheStore: setupCache(),
} }
return &g
} }
func setupCache() *bigcache.BigCache { func setupCache() *bigcache.BigCache {
+4 -2
View File
@@ -52,9 +52,11 @@ func (g *GraphQL) Query(queryContent string, queryVariables interface{}, queryHe
if g.Cache { if g.Cache {
g.Log.Debug("Checking the cache for the query", map[string]interface{}{"_query": queryHash}) g.Log.Debug("Checking the cache for the query", map[string]interface{}{"_query": queryHash})
if entry, err := g.CacheStore.Get(queryHash); err == nil { if entry, entryInfo, err := g.CacheStore.GetWithInfo(queryHash); err == nil {
g.Log.Debug("Found the query in the cache", map[string]interface{}{"_query": queryHash}) g.Log.Debug("Found the query in the cache", map[string]interface{}{"_query": queryHash})
return string(entry), nil if pandati.IsZero(entryInfo.EntryStatus) {
return string(entry), nil
}
} else { } else {
g.Log.Debug("Unable to find the query in the cache", map[string]interface{}{"_query": queryHash, "_error": err.Error()}) g.Log.Debug("Unable to find the query in the cache", map[string]interface{}{"_query": queryHash, "_error": err.Error()})
} }
+19
View File
@@ -132,6 +132,25 @@ func (suite *TestSuite) Test_GraphQL_Query() {
wantResult: ``, wantResult: ``,
wantErr: true, wantErr: true,
}, },
{
name: "Valid query to https endpoint",
endpoint: "https://web-dev.telegram-bot.app/v1/graphql",
isLocal: false,
args: args{
queryContent: `query packages_prices {
available_packages: tbl_available_packages(where: {enabled: {_eq: true}}) {
package_name
package_discount
package_price
package_size
package_type
}
}`,
queryVariables: nil,
},
wantResult: `{"available_packages":[{"package_discount":0,"package_name":"media_1000","package_price":750,"package_size":1000,"package_type":"media"},{"package_discount":5,"package_name":"media_5000","package_price":3563,"package_size":5000,"package_type":"media"},{"package_discount":10,"package_name":"media_10000","package_price":6750,"package_size":10000,"package_type":"media"},{"package_discount":15,"package_name":"media_25000","package_price":15938,"package_size":25000,"package_type":"media"},{"package_discount":0,"package_name":"voice_500","package_price":1000,"package_size":500,"package_type":"voice"},{"package_discount":10,"package_name":"voice_1000","package_price":1800,"package_size":1000,"package_type":"voice"},{"package_discount":15,"package_name":"voice_2500","package_price":4250,"package_size":2500,"package_type":"voice"}]}`,
wantErr: false,
},
} }
for _, tt := range tests { for _, tt := range tests {
suite.T().Run(tt.name, func(t *testing.T) { suite.T().Run(tt.name, func(t *testing.T) {