mirror of
https://github.com/lukaszraczylo/go-simple-graphql.git
synced 2026-10-16 01:07:57 +00:00
Micro optimisations.
This commit is contained in:
@@ -86,17 +86,17 @@ func NewConnection() *GraphQL {
|
|||||||
endpoint := pickGraphqlEndpoint()
|
endpoint := pickGraphqlEndpoint()
|
||||||
var httpClient *http.Client
|
var httpClient *http.Client
|
||||||
if strings.HasPrefix(endpoint, "http://") {
|
if strings.HasPrefix(endpoint, "http://") {
|
||||||
// HTTP/1.1 client
|
httpClient = &http.Client{
|
||||||
httpClient = &http.Client{}
|
Transport: http.DefaultTransport,
|
||||||
} else {
|
|
||||||
// HTTP/2 or HTTPS client
|
|
||||||
http2Transport := &http2.Transport{
|
|
||||||
AllowHTTP: true,
|
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
tlsClientConfig := &tls.Config{}
|
||||||
if strings.HasPrefix(endpoint, "https://") {
|
if strings.HasPrefix(endpoint, "https://") {
|
||||||
http2Transport.TLSClientConfig = &tls.Config{
|
tlsClientConfig.InsecureSkipVerify = true
|
||||||
InsecureSkipVerify: true,
|
}
|
||||||
}
|
http2Transport := &http2.Transport{
|
||||||
|
AllowHTTP: true,
|
||||||
|
TLSClientConfig: tlsClientConfig,
|
||||||
}
|
}
|
||||||
httpClient = &http.Client{
|
httpClient = &http.Client{
|
||||||
Transport: http2Transport,
|
Transport: http2Transport,
|
||||||
|
|||||||
@@ -47,21 +47,21 @@ func (g *GraphQL) Query(queryContent string, queryVariables interface{}, queryHe
|
|||||||
}
|
}
|
||||||
|
|
||||||
var body []byte
|
var body []byte
|
||||||
var queryResult *queryResults
|
var queryResult queryResults
|
||||||
queryHash := fmt.Sprintf("%x", md5.Sum(query))
|
var queryHash string
|
||||||
|
|
||||||
|
cacheEnabled := queryHeaders["gqlcache"] == true
|
||||||
// If header 'gqlcache' is set to true, we will enforce the cache for the query
|
// If header 'gqlcache' is set to true, we will enforce the cache for the query
|
||||||
if !pandati.IsZero(queryHeaders) && !pandati.IsZero(queryHeaders["gqlcache"]) && queryHeaders["gqlcache"].(bool) {
|
if cacheEnabled || g.Cache {
|
||||||
g.Log.Debug("Forced cache for query", map[string]interface{}{"_query": queryContent, "_headers": queryHeaders})
|
g.Log.Debug("Forced cache for query", map[string]interface{}{"_query": queryContent, "_headers": queryHeaders})
|
||||||
g.Cache = true
|
g.Cache = true
|
||||||
}
|
queryHash = fmt.Sprintf("%x", md5.Sum(query))
|
||||||
|
|
||||||
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, entryInfo, err := g.CacheStore.GetWithInfo(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})
|
||||||
if pandati.IsZero(entryInfo.EntryStatus) {
|
if pandati.IsZero(entryInfo.EntryStatus) {
|
||||||
return string(entry), nil
|
return pandati.BytesToString(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()})
|
||||||
@@ -76,7 +76,7 @@ func (g *GraphQL) Query(queryContent string, queryVariables interface{}, queryHe
|
|||||||
}
|
}
|
||||||
|
|
||||||
for header, value := range queryHeaders {
|
for header, value := range queryHeaders {
|
||||||
httpRequest.Header.Add(fmt.Sprintf("%v", header), fmt.Sprintf("%v", value))
|
httpRequest.Header.Add(header, fmt.Sprintf("%v", value))
|
||||||
}
|
}
|
||||||
|
|
||||||
var httpResponse *http.Response
|
var httpResponse *http.Response
|
||||||
@@ -130,7 +130,7 @@ func (g *GraphQL) Query(queryContent string, queryVariables interface{}, queryHe
|
|||||||
if g.Cache {
|
if g.Cache {
|
||||||
g.Log.Debug("Caching the query", map[string]interface{}{"_query": queryHash})
|
g.Log.Debug("Caching the query", map[string]interface{}{"_query": queryHash})
|
||||||
if queryContent[0:5] == "query" {
|
if queryContent[0:5] == "query" {
|
||||||
err = g.CacheStore.Set(queryHash, []byte(responseContent))
|
err = g.CacheStore.Set(queryHash, pandati.StringToBytes(responseContent))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
g.Log.Error("Unable to cache the query", map[string]interface{}{"_query": queryHash, "_error": err.Error()})
|
g.Log.Error("Unable to cache the query", map[string]interface{}{"_query": queryHash, "_error": err.Error()})
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user