mirror of
https://github.com/lukaszraczylo/go-simple-graphql.git
synced 2026-10-13 00:48:37 +00:00
Compare commits
Vendored
+60
-33
@@ -3,6 +3,7 @@ package libpack_cache
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"compress/gzip"
|
"compress/gzip"
|
||||||
|
"hash/fnv"
|
||||||
"io"
|
"io"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
@@ -13,12 +14,25 @@ type CacheEntry struct {
|
|||||||
Value []byte
|
Value []byte
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const shardCount = 256 // Must be power of 2
|
||||||
|
|
||||||
|
type shard struct {
|
||||||
|
entries map[string]CacheEntry
|
||||||
|
sync.RWMutex
|
||||||
|
}
|
||||||
|
|
||||||
type Cache struct {
|
type Cache struct {
|
||||||
compressPool sync.Pool
|
compressPool sync.Pool
|
||||||
decompressPool sync.Pool
|
decompressPool sync.Pool
|
||||||
entries sync.Map
|
shards [shardCount]*shard
|
||||||
globalTTL time.Duration
|
globalTTL time.Duration
|
||||||
sync.RWMutex
|
}
|
||||||
|
|
||||||
|
// getShard returns the appropriate shard for a given key
|
||||||
|
func (c *Cache) getShard(key string) *shard {
|
||||||
|
hash := fnv.New32a()
|
||||||
|
hash.Write([]byte(key))
|
||||||
|
return c.shards[hash.Sum32()%shardCount]
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(globalTTL time.Duration) *Cache {
|
func New(globalTTL time.Duration) *Cache {
|
||||||
@@ -32,13 +46,19 @@ func New(globalTTL time.Duration) *Cache {
|
|||||||
},
|
},
|
||||||
decompressPool: sync.Pool{
|
decompressPool: sync.Pool{
|
||||||
New: func() interface{} {
|
New: func() interface{} {
|
||||||
// Ensure that new is returning a new reader initialized with an empty byte buffer
|
|
||||||
r, _ := gzip.NewReader(bytes.NewReader([]byte{}))
|
r, _ := gzip.NewReader(bytes.NewReader([]byte{}))
|
||||||
return r
|
return r
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Initialize shards
|
||||||
|
for i := 0; i < shardCount; i++ {
|
||||||
|
cache.shards[i] = &shard{
|
||||||
|
entries: make(map[string]CacheEntry),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
go cache.cleanupRoutine(globalTTL)
|
go cache.cleanupRoutine(globalTTL)
|
||||||
return cache
|
return cache
|
||||||
}
|
}
|
||||||
@@ -52,33 +72,43 @@ func (c *Cache) cleanupRoutine(globalTTL time.Duration) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
func (c *Cache) Set(key string, value []byte, ttl time.Duration) {
|
func (c *Cache) Set(key string, value []byte, ttl time.Duration) {
|
||||||
c.Lock() // use the lock
|
shard := c.getShard(key)
|
||||||
defer c.Unlock()
|
shard.Lock()
|
||||||
|
defer shard.Unlock()
|
||||||
expiresAt := time.Now().Add(ttl)
|
|
||||||
|
|
||||||
compressedValue, err := c.compress(value)
|
compressedValue, err := c.compress(value)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
entry := CacheEntry{
|
shard.entries[key] = CacheEntry{
|
||||||
Value: compressedValue,
|
Value: compressedValue,
|
||||||
ExpiresAt: expiresAt,
|
ExpiresAt: time.Now().Add(ttl),
|
||||||
}
|
}
|
||||||
c.entries.Store(key, entry)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Cache) Get(key string) ([]byte, bool) {
|
func (c *Cache) Get(key string) ([]byte, bool) {
|
||||||
c.RLock() // use the read lock
|
shard := c.getShard(key)
|
||||||
defer c.RUnlock()
|
shard.RLock()
|
||||||
|
entry, ok := shard.entries[key]
|
||||||
entry, ok := c.entries.Load(key)
|
if !ok {
|
||||||
if !ok || entry.(CacheEntry).ExpiresAt.Before(time.Now()) {
|
shard.RUnlock()
|
||||||
return nil, false
|
return nil, false
|
||||||
}
|
}
|
||||||
compressedValue := entry.(CacheEntry).Value
|
|
||||||
value, err := c.decompress(compressedValue)
|
if entry.ExpiresAt.Before(time.Now()) {
|
||||||
|
shard.RUnlock()
|
||||||
|
// Clean up expired entry in background
|
||||||
|
go func() {
|
||||||
|
shard.Lock()
|
||||||
|
delete(shard.entries, key)
|
||||||
|
shard.Unlock()
|
||||||
|
}()
|
||||||
|
return nil, false
|
||||||
|
}
|
||||||
|
shard.RUnlock()
|
||||||
|
|
||||||
|
value, err := c.decompress(entry.Value)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, false
|
return nil, false
|
||||||
}
|
}
|
||||||
@@ -87,26 +117,23 @@ func (c *Cache) Get(key string) ([]byte, bool) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *Cache) Delete(key string) {
|
func (c *Cache) Delete(key string) {
|
||||||
c.Lock()
|
shard := c.getShard(key)
|
||||||
defer c.Unlock()
|
shard.Lock()
|
||||||
|
delete(shard.entries, key)
|
||||||
_, ok := c.entries.Load(key)
|
shard.Unlock()
|
||||||
if !ok {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
c.entries.Delete(key)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Cache) CleanExpiredEntries() {
|
func (c *Cache) CleanExpiredEntries() {
|
||||||
now := time.Now()
|
now := time.Now()
|
||||||
c.entries.Range(func(key, value interface{}) bool {
|
for _, shard := range c.shards {
|
||||||
entry := value.(CacheEntry)
|
shard.Lock()
|
||||||
if entry.ExpiresAt.Before(now) {
|
for key, entry := range shard.entries {
|
||||||
c.entries.Delete(key)
|
if entry.ExpiresAt.Before(now) {
|
||||||
|
delete(shard.entries, key)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return true
|
shard.Unlock()
|
||||||
})
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Cache) compress(data []byte) ([]byte, error) {
|
func (c *Cache) compress(data []byte) ([]byte, error) {
|
||||||
|
|||||||
+31
-11
@@ -7,7 +7,6 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"sync"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/avast/retry-go/v4"
|
"github.com/avast/retry-go/v4"
|
||||||
@@ -15,11 +14,22 @@ import (
|
|||||||
libpack_logger "github.com/lukaszraczylo/go-simple-graphql/logging"
|
libpack_logger "github.com/lukaszraczylo/go-simple-graphql/logging"
|
||||||
)
|
)
|
||||||
|
|
||||||
var bufferPool = sync.Pool{
|
var (
|
||||||
New: func() interface{} {
|
// Shared HTTP transport with optimized settings
|
||||||
return new(bytes.Buffer)
|
defaultTransport = &http.Transport{
|
||||||
},
|
MaxIdleConns: 100,
|
||||||
}
|
MaxIdleConnsPerHost: 100,
|
||||||
|
IdleConnTimeout: 90 * time.Second,
|
||||||
|
DisableCompression: false, // Enable compression for responses
|
||||||
|
ForceAttemptHTTP2: true,
|
||||||
|
}
|
||||||
|
|
||||||
|
// Shared HTTP client with timeouts
|
||||||
|
defaultClient = &http.Client{
|
||||||
|
Transport: defaultTransport,
|
||||||
|
Timeout: 30 * time.Second,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
func (qe *QueryExecutor) executeQuery() ([]byte, error) {
|
func (qe *QueryExecutor) executeQuery() ([]byte, error) {
|
||||||
// Reuse buffer from pool to avoid allocations
|
// Reuse buffer from pool to avoid allocations
|
||||||
@@ -62,7 +72,12 @@ func (qe *QueryExecutor) executeQuery() ([]byte, error) {
|
|||||||
httpRequest.Body = io.NopCloser(bytes.NewReader(buf.Bytes()))
|
httpRequest.Body = io.NopCloser(bytes.NewReader(buf.Bytes()))
|
||||||
httpRequest.ContentLength = int64(buf.Len())
|
httpRequest.ContentLength = int64(buf.Len())
|
||||||
|
|
||||||
httpResponse, err := qe.client.Do(httpRequest)
|
// Use default client if custom client is not set
|
||||||
|
client := qe.client
|
||||||
|
if client == nil {
|
||||||
|
client = defaultClient
|
||||||
|
}
|
||||||
|
httpResponse, err := client.Do(httpRequest)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -88,14 +103,18 @@ func (qe *QueryExecutor) executeQuery() ([]byte, error) {
|
|||||||
reader = httpResponse.Body
|
reader = httpResponse.Body
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read all response body
|
// Use buffer pool for reading response
|
||||||
body, err := io.ReadAll(reader)
|
respBuf := bufferPool.Get().(*bytes.Buffer)
|
||||||
|
respBuf.Reset()
|
||||||
|
defer bufferPool.Put(respBuf)
|
||||||
|
|
||||||
|
_, err = io.Copy(respBuf, reader)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("error reading HTTP response: %w", err)
|
return fmt.Errorf("error reading HTTP response: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Unmarshal response
|
// Unmarshal response directly from buffer
|
||||||
err = json.Unmarshal(body, &queryResult)
|
err = json.Unmarshal(respBuf.Bytes(), &queryResult)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("error unmarshalling HTTP response: %w", err)
|
return fmt.Errorf("error unmarshalling HTTP response: %w", err)
|
||||||
}
|
}
|
||||||
@@ -111,6 +130,7 @@ func (qe *QueryExecutor) executeQuery() ([]byte, error) {
|
|||||||
retry.Attempts(uint(retriesMax)),
|
retry.Attempts(uint(retriesMax)),
|
||||||
retry.DelayType(retry.BackOffDelay),
|
retry.DelayType(retry.BackOffDelay),
|
||||||
retry.Delay(time.Duration(qe.retries_delay)),
|
retry.Delay(time.Duration(qe.retries_delay)),
|
||||||
|
retry.MaxDelay(10 * time.Second),
|
||||||
retry.LastErrorOnly(true),
|
retry.LastErrorOnly(true),
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -1,14 +1,16 @@
|
|||||||
module github.com/lukaszraczylo/go-simple-graphql
|
module github.com/lukaszraczylo/go-simple-graphql
|
||||||
|
|
||||||
go 1.21
|
go 1.23.0
|
||||||
|
|
||||||
|
toolchain go1.23.6
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/avast/retry-go/v4 v4.6.0
|
github.com/avast/retry-go/v4 v4.6.1
|
||||||
github.com/goccy/go-json v0.10.3
|
github.com/goccy/go-json v0.10.5
|
||||||
github.com/goccy/go-reflect v1.2.0
|
github.com/goccy/go-reflect v1.2.0
|
||||||
github.com/gookit/goutil v0.6.17
|
github.com/gookit/goutil v0.6.18
|
||||||
github.com/stretchr/testify v1.9.0
|
github.com/stretchr/testify v1.10.0
|
||||||
golang.org/x/net v0.30.0
|
golang.org/x/net v0.37.0
|
||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
@@ -16,9 +18,9 @@ require (
|
|||||||
github.com/gookit/color v1.5.4 // indirect
|
github.com/gookit/color v1.5.4 // indirect
|
||||||
github.com/pmezard/go-difflib v1.0.0 // indirect
|
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||||
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect
|
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect
|
||||||
golang.org/x/sync v0.8.0 // indirect
|
golang.org/x/sync v0.12.0 // indirect
|
||||||
golang.org/x/sys v0.26.0 // indirect
|
golang.org/x/sys v0.31.0 // indirect
|
||||||
golang.org/x/term v0.25.0 // indirect
|
golang.org/x/term v0.30.0 // indirect
|
||||||
golang.org/x/text v0.19.0 // indirect
|
golang.org/x/text v0.23.0 // indirect
|
||||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -1,33 +1,33 @@
|
|||||||
github.com/avast/retry-go/v4 v4.6.0 h1:K9xNA+KeB8HHc2aWFuLb25Offp+0iVRXEvFx8IinRJA=
|
github.com/avast/retry-go/v4 v4.6.1 h1:VkOLRubHdisGrHnTu89g08aQEWEgRU7LVEop3GbIcMk=
|
||||||
github.com/avast/retry-go/v4 v4.6.0/go.mod h1:gvWlPhBVsvBbLkVGDg/KwvBv0bEkCOLRRSHKIr2PyOE=
|
github.com/avast/retry-go/v4 v4.6.1/go.mod h1:V6oF8njAwxJ5gRo1Q7Cxab24xs5NCWZBeaHHBklR8mA=
|
||||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
github.com/goccy/go-json v0.10.3 h1:KZ5WoDbxAIgm2HNbYckL0se1fHD6rz5j4ywS6ebzDqA=
|
github.com/goccy/go-json v0.10.5 h1:Fq85nIqj+gXn/S5ahsiTlK3TmC85qgirsdTP/+DeaC4=
|
||||||
github.com/goccy/go-json v0.10.3/go.mod h1:oq7eo15ShAhp70Anwd5lgX2pLfOS3QCiwU/PULtXL6M=
|
github.com/goccy/go-json v0.10.5/go.mod h1:oq7eo15ShAhp70Anwd5lgX2pLfOS3QCiwU/PULtXL6M=
|
||||||
github.com/goccy/go-reflect v1.2.0 h1:O0T8rZCuNmGXewnATuKYnkL0xm6o8UNOJZd/gOkb9ms=
|
github.com/goccy/go-reflect v1.2.0 h1:O0T8rZCuNmGXewnATuKYnkL0xm6o8UNOJZd/gOkb9ms=
|
||||||
github.com/goccy/go-reflect v1.2.0/go.mod h1:n0oYZn8VcV2CkWTxi8B9QjkCoq6GTtCEdfmR66YhFtE=
|
github.com/goccy/go-reflect v1.2.0/go.mod h1:n0oYZn8VcV2CkWTxi8B9QjkCoq6GTtCEdfmR66YhFtE=
|
||||||
github.com/gookit/color v1.5.4 h1:FZmqs7XOyGgCAxmWyPslpiok1k05wmY3SJTytgvYFs0=
|
github.com/gookit/color v1.5.4 h1:FZmqs7XOyGgCAxmWyPslpiok1k05wmY3SJTytgvYFs0=
|
||||||
github.com/gookit/color v1.5.4/go.mod h1:pZJOeOS8DM43rXbp4AZo1n9zCU2qjpcRko0b6/QJi9w=
|
github.com/gookit/color v1.5.4/go.mod h1:pZJOeOS8DM43rXbp4AZo1n9zCU2qjpcRko0b6/QJi9w=
|
||||||
github.com/gookit/goutil v0.6.17 h1:SxmbDz2sn2V+O+xJjJhJT/sq1/kQh6rCJ7vLBiRPZjI=
|
github.com/gookit/goutil v0.6.18 h1:MUVj0G16flubWT8zYVicIuisUiHdgirPAkmnfD2kKgw=
|
||||||
github.com/gookit/goutil v0.6.17/go.mod h1:rSw1LchE1I3TDWITZvefoAC9tS09SFu3lHXLCV7EaEY=
|
github.com/gookit/goutil v0.6.18/go.mod h1:AY/5sAwKe7Xck+mEbuxj0n/bc3qwrGNe3Oeulln7zBA=
|
||||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||||
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
|
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
|
||||||
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
||||||
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e h1:JVG44RsyaB9T2KIHavMF/ppJZNG9ZpyihvCd0w101no=
|
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e h1:JVG44RsyaB9T2KIHavMF/ppJZNG9ZpyihvCd0w101no=
|
||||||
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e/go.mod h1:RbqR21r5mrJuqunuUZ/Dhy/avygyECGrLceyNeo4LiM=
|
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e/go.mod h1:RbqR21r5mrJuqunuUZ/Dhy/avygyECGrLceyNeo4LiM=
|
||||||
golang.org/x/exp v0.0.0-20220909182711-5c715a9e8561 h1:MDc5xs78ZrZr3HMQugiXOAkSZtfTpbJLDr/lwfgO53E=
|
golang.org/x/exp v0.0.0-20220909182711-5c715a9e8561 h1:MDc5xs78ZrZr3HMQugiXOAkSZtfTpbJLDr/lwfgO53E=
|
||||||
golang.org/x/exp v0.0.0-20220909182711-5c715a9e8561/go.mod h1:cyybsKvd6eL0RnXn6p/Grxp8F5bW7iYuBgsNCOHpMYE=
|
golang.org/x/exp v0.0.0-20220909182711-5c715a9e8561/go.mod h1:cyybsKvd6eL0RnXn6p/Grxp8F5bW7iYuBgsNCOHpMYE=
|
||||||
golang.org/x/net v0.30.0 h1:AcW1SDZMkb8IpzCdQUaIq2sP4sZ4zw+55h6ynffypl4=
|
golang.org/x/net v0.37.0 h1:1zLorHbz+LYj7MQlSf1+2tPIIgibq2eL5xkrGk6f+2c=
|
||||||
golang.org/x/net v0.30.0/go.mod h1:2wGyMJ5iFasEhkwi13ChkO/t1ECNC4X4eBKkVFyYFlU=
|
golang.org/x/net v0.37.0/go.mod h1:ivrbrMbzFq5J41QOQh0siUuly180yBYtLp+CKbEaFx8=
|
||||||
golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ=
|
golang.org/x/sync v0.12.0 h1:MHc5BpPuC30uJk597Ri8TV3CNZcTLu6B6z4lJy+g6Jw=
|
||||||
golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
|
golang.org/x/sync v0.12.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA=
|
||||||
golang.org/x/sys v0.26.0 h1:KHjCJyddX0LoSTb3J+vWpupP9p0oznkqVk/IfjymZbo=
|
golang.org/x/sys v0.31.0 h1:ioabZlmFYtWhL+TRYpcnNlLwhyxaM9kWTDEmfnprqik=
|
||||||
golang.org/x/sys v0.26.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
golang.org/x/sys v0.31.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
|
||||||
golang.org/x/term v0.25.0 h1:WtHI/ltw4NvSUig5KARz9h521QvRC8RmF/cuYqifU24=
|
golang.org/x/term v0.30.0 h1:PQ39fJZ+mfadBm0y5WlL4vlM7Sx1Hgf13sMIY2+QS9Y=
|
||||||
golang.org/x/term v0.25.0/go.mod h1:RPyXicDX+6vLxogjjRxjgD2TKtmAO6NZBsBRfrOLu7M=
|
golang.org/x/term v0.30.0/go.mod h1:NYYFdzHoI5wRh/h5tDMdMqCqPJZEuNqVR5xJLd/n67g=
|
||||||
golang.org/x/text v0.19.0 h1:kTxAhCbGbxhK0IwgSKiMO5awPoDQ0RpfiVYBfK860YM=
|
golang.org/x/text v0.23.0 h1:D71I7dUrlY+VX0gQShAThNGHFxZ13dGLBHQLVl1mJlY=
|
||||||
golang.org/x/text v0.19.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
|
golang.org/x/text v0.23.0/go.mod h1:/BLNzu4aZCJ1+kcD0DNRotWKage4q2rGVAg4o22unh4=
|
||||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||||
|
|||||||
+1
-1
@@ -48,6 +48,6 @@ func (b *BaseClient) decodeResponse(response []byte) (any, error) {
|
|||||||
Message: "Can't decode response",
|
Message: "Can't decode response",
|
||||||
Pairs: map[string]interface{}{"error": "unknown response type"},
|
Pairs: map[string]interface{}{"error": "unknown response type"},
|
||||||
})
|
})
|
||||||
return nil, fmt.Errorf("Can't decode response - unknown response type specified")
|
return nil, fmt.Errorf("can't decode response - unknown response type specified")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -58,11 +58,11 @@ func Benchmark_NewLogger(b *testing.B) {
|
|||||||
got := New()
|
got := New()
|
||||||
|
|
||||||
if tt.triggers.ModFormat.Format != "" {
|
if tt.triggers.ModFormat.Format != "" {
|
||||||
got = got.SetFormat(tt.triggers.ModFormat.Format)
|
got.SetFormat(tt.triggers.ModFormat.Format)
|
||||||
}
|
}
|
||||||
|
|
||||||
if tt.triggers.ModLevel.Level != 0 {
|
if tt.triggers.ModLevel.Level != 0 {
|
||||||
got = got.SetMinLogLevel(tt.triggers.ModLevel.Level)
|
got.SetMinLogLevel(tt.triggers.ModLevel.Level)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ package libpack_logger
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -11,30 +10,6 @@ import (
|
|||||||
"github.com/goccy/go-reflect"
|
"github.com/goccy/go-reflect"
|
||||||
)
|
)
|
||||||
|
|
||||||
func captureStderr(f func()) string {
|
|
||||||
originalStderr := os.Stderr
|
|
||||||
r, w, _ := os.Pipe()
|
|
||||||
os.Stderr = w
|
|
||||||
f()
|
|
||||||
w.Close()
|
|
||||||
var buf bytes.Buffer
|
|
||||||
buf.ReadFrom(r)
|
|
||||||
os.Stderr = originalStderr
|
|
||||||
return buf.String()
|
|
||||||
}
|
|
||||||
|
|
||||||
func captureStdOut(f func()) string {
|
|
||||||
originalStdout := os.Stdout
|
|
||||||
r, w, _ := os.Pipe()
|
|
||||||
os.Stdout = w
|
|
||||||
f()
|
|
||||||
w.Close()
|
|
||||||
var buf bytes.Buffer
|
|
||||||
buf.ReadFrom(r)
|
|
||||||
os.Stdout = originalStdout
|
|
||||||
return buf.String()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (suite *LoggerTestSuite) Test_LogMessageString() {
|
func (suite *LoggerTestSuite) Test_LogMessageString() {
|
||||||
msg := &LogMessage{
|
msg := &LogMessage{
|
||||||
Message: "test message",
|
Message: "test message",
|
||||||
|
|||||||
+2
-2
@@ -3,7 +3,7 @@ package gql
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"io/ioutil"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -15,7 +15,7 @@ func StartMockServer() *httptest.Server {
|
|||||||
// Create your handler
|
// Create your handler
|
||||||
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
// Your existing handler code
|
// Your existing handler code
|
||||||
bodyBytes, err := ioutil.ReadAll(r.Body)
|
bodyBytes, err := io.ReadAll(r.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -0,0 +1,24 @@
|
|||||||
|
package gql
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"sync"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
// Pre-sized buffer pool for better performance
|
||||||
|
bufferPool = sync.Pool{
|
||||||
|
New: func() interface{} {
|
||||||
|
// Pre-allocate buffer with 4KB capacity
|
||||||
|
b := bytes.NewBuffer(make([]byte, 0, 4096))
|
||||||
|
return b
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
// Pre-allocate error maps to reduce allocations
|
||||||
|
errPairsPool = sync.Pool{
|
||||||
|
New: func() interface{} {
|
||||||
|
return make(map[string]interface{}, 1)
|
||||||
|
},
|
||||||
|
}
|
||||||
|
)
|
||||||
@@ -11,56 +11,73 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func (b *BaseClient) convertToJSON(v any) []byte {
|
func (b *BaseClient) convertToJSON(v any) []byte {
|
||||||
// Reuse buffer to reduce allocations
|
|
||||||
buf := bufferPool.Get().(*bytes.Buffer)
|
buf := bufferPool.Get().(*bytes.Buffer)
|
||||||
defer bufferPool.Put(buf)
|
defer bufferPool.Put(buf)
|
||||||
buf.Reset()
|
buf.Reset()
|
||||||
|
|
||||||
// Use json.Marshal to avoid adding extra newline
|
// Use json.NewEncoder for better performance with buffers
|
||||||
jsonData, err := json.Marshal(v)
|
enc := json.NewEncoder(buf)
|
||||||
if err != nil {
|
enc.SetEscapeHTML(false) // Reduce unnecessary escaping
|
||||||
|
|
||||||
|
if err := enc.Encode(v); err != nil {
|
||||||
|
errPairs := errPairsPool.Get().(map[string]interface{})
|
||||||
|
errPairs["error"] = err.Error()
|
||||||
b.Logger.Error(&libpack_logger.LogMessage{
|
b.Logger.Error(&libpack_logger.LogMessage{
|
||||||
Message: "Can't convert to JSON",
|
Message: "Can't convert to JSON",
|
||||||
Pairs: map[string]interface{}{"error": err.Error()},
|
Pairs: errPairs,
|
||||||
})
|
})
|
||||||
|
errPairsPool.Put(errPairs)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Copy the bytes to the buffer
|
// Get the buffer bytes directly, trimming the trailing newline
|
||||||
buf.Write(jsonData)
|
bytes := buf.Bytes()
|
||||||
|
if len(bytes) > 0 && bytes[len(bytes)-1] == '\n' {
|
||||||
|
bytes = bytes[:len(bytes)-1]
|
||||||
|
}
|
||||||
|
|
||||||
// Copy the bytes to a new slice before returning
|
// Make a copy of the bytes since the buffer will be reused
|
||||||
result := make([]byte, buf.Len())
|
result := make([]byte, len(bytes))
|
||||||
copy(result, buf.Bytes())
|
copy(result, bytes)
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *BaseClient) compileQuery(queryPartials ...any) *Query {
|
func (b *BaseClient) compileQuery(queryPartials ...any) *Query {
|
||||||
var query string
|
var query string
|
||||||
var variables map[string]interface{}
|
var variables map[string]interface{}
|
||||||
for _, partial := range queryPartials {
|
|
||||||
switch val := partial.(type) {
|
// Pre-allocate the query with an estimated size
|
||||||
case string:
|
if len(queryPartials) > 0 {
|
||||||
query = val
|
if str, ok := queryPartials[0].(string); ok {
|
||||||
case map[string]interface{}:
|
query = str
|
||||||
variables = val
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Only allocate variables map if we have more than one partial
|
||||||
|
if len(queryPartials) > 1 {
|
||||||
|
if vars, ok := queryPartials[1].(map[string]interface{}); ok {
|
||||||
|
variables = vars
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if query == "" {
|
if query == "" {
|
||||||
|
errPairs := errPairsPool.Get().(map[string]interface{})
|
||||||
|
errPairs["error"] = "query is empty"
|
||||||
b.Logger.Error(&libpack_logger.LogMessage{
|
b.Logger.Error(&libpack_logger.LogMessage{
|
||||||
Message: "Can't compile query",
|
Message: "Can't compile query",
|
||||||
Pairs: map[string]interface{}{"error": "query is empty"},
|
Pairs: errPairs,
|
||||||
})
|
})
|
||||||
|
errPairsPool.Put(errPairs)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
jsonQuery := b.convertToJSON(&Query{Query: query, Variables: variables})
|
// Construct query object once
|
||||||
return &Query{
|
q := &Query{
|
||||||
Query: query,
|
Query: query,
|
||||||
Variables: variables,
|
Variables: variables,
|
||||||
JsonQuery: jsonQuery,
|
|
||||||
}
|
}
|
||||||
|
q.JsonQuery = b.convertToJSON(q)
|
||||||
|
return q
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *BaseClient) Query(query string, variables map[string]interface{}, headers map[string]interface{}) (any, error) {
|
func (b *BaseClient) Query(query string, variables map[string]interface{}, headers map[string]interface{}) (any, error) {
|
||||||
|
|||||||
Reference in New Issue
Block a user