mirror of
https://github.com/lukaszraczylo/go-simple-graphql.git
synced 2026-10-13 00:48:37 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
489247c42a | ||
|
|
6c55bccc20 | ||
|
|
97cf896a17 | ||
|
|
71a14721c5 |
@@ -24,9 +24,11 @@ Therefore, I present you the simple client to which you can copy & paste your gr
|
||||
## Features
|
||||
|
||||
* Executing GraphQL queries as they are, without types declaration
|
||||
* HTTP2 support!
|
||||
* HTTP/2 support for HTTPS endpoints (automatic)
|
||||
* HTTP/1.1 for HTTP endpoints (compatible with all servers)
|
||||
* Automatic retry with configurable error patterns
|
||||
* Support for additional headers
|
||||
* Query cache
|
||||
* Query cache with compression
|
||||
|
||||
## Usage example
|
||||
|
||||
@@ -41,6 +43,10 @@ Therefore, I present you the simple client to which you can copy & paste your gr
|
||||
* `GRAPHQL_RETRIES_NUMBER` - Number of retries: Default: `3`
|
||||
* `GRAPHQL_RETRIES_DELAY` - Delay in retries in milliseconds. Default: `250`
|
||||
* `GRAPHQL_RETRIES_PATTERNS` - Comma-separated error patterns that trigger retries. Default: `postgres,connection,timeout,transaction,could not,temporarily unavailable,deadlock`
|
||||
* `GRAPHQL_POOL_WARMUP_ENABLED` - Enable intelligent connection pool warmup. Default: `false`
|
||||
* `GRAPHQL_POOL_SIZE` - Number of connections to pre-create and maintain. Default: `5`
|
||||
* `GRAPHQL_POOL_WARMUP_QUERY` - Query used for warming up connections. Default: `query{__typename}`
|
||||
* `GRAPHQL_POOL_HEALTH_INTERVAL` - Interval in seconds for pool health checks. Default: `30`
|
||||
|
||||
### Modifiers on the fly
|
||||
|
||||
@@ -83,6 +89,63 @@ export GRAPHQL_RETRIES_DELAY=250
|
||||
|
||||
This will retry GraphQL queries up to 3 times when postgres transaction errors occur, with exponential backoff starting at 250ms.
|
||||
|
||||
### Intelligent Connection Pooling
|
||||
|
||||
The library supports intelligent connection pooling with pre-warmed connections and automatic health monitoring. This significantly improves performance during traffic spikes by keeping hot standby connections ready.
|
||||
|
||||
**How it works:**
|
||||
- **Pre-warming**: Connections are created and warmed up during initialization
|
||||
- **Health monitoring**: Background process periodically checks connection health
|
||||
- **Auto-refresh**: Automatically replaces stale or broken connections
|
||||
- **Hot standbys**: Ready connections eliminate cold-start latency
|
||||
|
||||
**Configuration:**
|
||||
|
||||
Enable connection pool warmup for production environments with variable traffic:
|
||||
|
||||
```bash
|
||||
export GRAPHQL_POOL_WARMUP_ENABLED=true
|
||||
export GRAPHQL_POOL_SIZE=5 # Number of hot standby connections
|
||||
export GRAPHQL_POOL_HEALTH_INTERVAL=30 # Health check every 30 seconds
|
||||
export GRAPHQL_POOL_WARMUP_QUERY="query{__typename}" # Lightweight health check query
|
||||
```
|
||||
|
||||
**Benefits:**
|
||||
- ⚡ **Instant response** - Connections already established during traffic spikes
|
||||
- 🔄 **Auto-healing** - Detects and replaces broken connections automatically
|
||||
- 📊 **Predictable performance** - Eliminates connection establishment latency
|
||||
- 🛡️ **Resilience** - Maintains pool health even during network instability
|
||||
|
||||
**Recommended settings by use case:**
|
||||
|
||||
```bash
|
||||
# High-traffic API (e.g., production web app)
|
||||
GRAPHQL_POOL_WARMUP_ENABLED=true
|
||||
GRAPHQL_POOL_SIZE=10
|
||||
GRAPHQL_POOL_HEALTH_INTERVAL=15
|
||||
|
||||
# Medium-traffic service (e.g., background jobs)
|
||||
GRAPHQL_POOL_WARMUP_ENABLED=true
|
||||
GRAPHQL_POOL_SIZE=5
|
||||
GRAPHQL_POOL_HEALTH_INTERVAL=30
|
||||
|
||||
# Low-traffic/development
|
||||
GRAPHQL_POOL_WARMUP_ENABLED=false # Default: on-demand connections
|
||||
```
|
||||
|
||||
**Graceful shutdown:**
|
||||
|
||||
When shutting down your application, stop the pool monitor gracefully:
|
||||
|
||||
```go
|
||||
func main() {
|
||||
gql := graphql.NewConnection()
|
||||
defer gql.StopPoolMonitor() // Ensure clean shutdown
|
||||
|
||||
// Your application logic
|
||||
}
|
||||
```
|
||||
|
||||
### Cache
|
||||
|
||||
You have two options to enable the cache:
|
||||
|
||||
@@ -11,42 +11,64 @@ import (
|
||||
)
|
||||
|
||||
func (b *BaseClient) createHttpClient() (http_client *http.Client) {
|
||||
// Create TLS config for HTTPS endpoints
|
||||
var tlsClientConfig *tls.Config
|
||||
if strings.HasPrefix(b.endpoint, "https://") {
|
||||
tlsClientConfig = &tls.Config{
|
||||
InsecureSkipVerify: true, // TODO: Make this configurable via environment variable
|
||||
}
|
||||
}
|
||||
|
||||
// Use HTTP/2 transport for both http:// (h2c) and https:// endpoints
|
||||
// AllowHTTP=true enables HTTP/2 Cleartext (h2c) for unencrypted connections
|
||||
http2Transport := &http2.Transport{
|
||||
AllowHTTP: true, // Enable h2c for http:// endpoints
|
||||
TLSClientConfig: tlsClientConfig, // nil for http://, configured for https://
|
||||
ReadIdleTimeout: 30 * time.Second, // Increased for better connection reuse
|
||||
PingTimeout: 10 * time.Second, // Reduced for faster dead connection detection
|
||||
WriteByteTimeout: 10 * time.Second, // Write timeout for better error handling
|
||||
DisableCompression: true, // Disable automatic compression to prevent trailing garbage errors
|
||||
}
|
||||
|
||||
http_client = &http.Client{
|
||||
Timeout: 30 * time.Second, // Overall request timeout
|
||||
Transport: http2Transport,
|
||||
CheckRedirect: func(req *http.Request, via []*http.Request) error {
|
||||
return http.ErrUseLastResponse // Don't follow redirects automatically
|
||||
},
|
||||
}
|
||||
|
||||
// Log which protocol is being used
|
||||
if strings.HasPrefix(b.endpoint, "http://") {
|
||||
// Use HTTP/1.1 for http:// endpoints
|
||||
// Note: h2c (HTTP/2 Cleartext) is rarely supported by servers
|
||||
httpTransport := &http.Transport{
|
||||
MaxIdleConns: 100,
|
||||
MaxConnsPerHost: 50,
|
||||
MaxIdleConnsPerHost: 10,
|
||||
IdleConnTimeout: 60 * time.Second,
|
||||
ResponseHeaderTimeout: 20 * time.Second,
|
||||
DisableKeepAlives: false,
|
||||
DisableCompression: true, // Disable automatic compression to prevent trailing garbage errors
|
||||
WriteBufferSize: 4096,
|
||||
ReadBufferSize: 4096,
|
||||
// Additional settings for connection health
|
||||
ExpectContinueTimeout: 2 * time.Second, // Timeout for 100-Continue
|
||||
TLSHandshakeTimeout: 20 * time.Second, // Timeout for TLS handshake
|
||||
}
|
||||
|
||||
http_client = &http.Client{
|
||||
Timeout: 60 * time.Second,
|
||||
Transport: httpTransport,
|
||||
CheckRedirect: func(req *http.Request, via []*http.Request) error {
|
||||
return http.ErrUseLastResponse
|
||||
},
|
||||
}
|
||||
|
||||
b.Logger.Debug(&libpack_logger.LogMessage{
|
||||
Message: "Using HTTP/2 Cleartext (h2c) over http",
|
||||
Message: "Using HTTP/1.1 for http:// endpoint",
|
||||
Pairs: nil,
|
||||
})
|
||||
} else if strings.HasPrefix(b.endpoint, "https://") {
|
||||
// Use HTTP/2 for https:// endpoints
|
||||
tlsClientConfig := &tls.Config{
|
||||
InsecureSkipVerify: true, // TODO: Make this configurable via environment variable
|
||||
}
|
||||
|
||||
http2Transport := &http2.Transport{
|
||||
AllowHTTP: true,
|
||||
TLSClientConfig: tlsClientConfig,
|
||||
ReadIdleTimeout: 60 * time.Second, // Close idle connections after 60s
|
||||
PingTimeout: 20 * time.Second, // Detect dead connections with PING
|
||||
WriteByteTimeout: 20 * time.Second, // Timeout for write operations
|
||||
DisableCompression: true, // Disable automatic compression to prevent trailing garbage errors
|
||||
// StrictMaxConcurrentStreams forces the HTTP/2 connection to wait for SETTINGS frame
|
||||
// This helps with connection health detection
|
||||
StrictMaxConcurrentStreams: true,
|
||||
}
|
||||
|
||||
http_client = &http.Client{
|
||||
Timeout: 60 * time.Second,
|
||||
Transport: http2Transport,
|
||||
CheckRedirect: func(req *http.Request, via []*http.Request) error {
|
||||
return http.ErrUseLastResponse
|
||||
},
|
||||
}
|
||||
|
||||
b.Logger.Debug(&libpack_logger.LogMessage{
|
||||
Message: "Using HTTP/2 over TLS (https)",
|
||||
Message: "Using HTTP/2 for https:// endpoint",
|
||||
Pairs: nil,
|
||||
})
|
||||
} else {
|
||||
|
||||
+31
-25
@@ -11,24 +11,27 @@ import (
|
||||
// Note: http.Transport import is kept for http.ErrUseLastResponse usage in redirect tests
|
||||
|
||||
func (suite *Tests) TestBaseClient_createHttpClient() {
|
||||
suite.T().Run("should create HTTP/2 client for http endpoints (h2c)", func(t *testing.T) {
|
||||
suite.T().Run("should create HTTP/1.1 client for http endpoints", func(t *testing.T) {
|
||||
client := NewConnection()
|
||||
client.SetEndpoint("http://example.com/graphql")
|
||||
|
||||
httpClient := client.createHttpClient()
|
||||
assert.NotNil(httpClient)
|
||||
assert.Equal(30*time.Second, httpClient.Timeout)
|
||||
assert.Equal(60*time.Second, httpClient.Timeout)
|
||||
assert.NotNil(httpClient.Transport)
|
||||
|
||||
// Check that it's using HTTP/2 transport with h2c (HTTP/2 Cleartext)
|
||||
transport, ok := httpClient.Transport.(*http2.Transport)
|
||||
// Check that it's using HTTP/1.1 transport (h2c is rarely supported by servers)
|
||||
transport, ok := httpClient.Transport.(*http.Transport)
|
||||
assert.True(ok)
|
||||
assert.True(transport.AllowHTTP) // Required for h2c
|
||||
assert.Nil(transport.TLSClientConfig) // No TLS for http://
|
||||
assert.Equal(30*time.Second, transport.ReadIdleTimeout)
|
||||
assert.Equal(10*time.Second, transport.PingTimeout)
|
||||
assert.Equal(10*time.Second, transport.WriteByteTimeout)
|
||||
assert.Equal(100, transport.MaxIdleConns)
|
||||
assert.Equal(50, transport.MaxConnsPerHost)
|
||||
assert.Equal(10, transport.MaxIdleConnsPerHost)
|
||||
assert.Equal(60*time.Second, transport.IdleConnTimeout)
|
||||
assert.Equal(20*time.Second, transport.ResponseHeaderTimeout)
|
||||
assert.False(transport.DisableKeepAlives)
|
||||
assert.True(transport.DisableCompression) // Request compression disabled to prevent "trailing garbage" errors
|
||||
assert.Equal(4096, transport.WriteBufferSize)
|
||||
assert.Equal(4096, transport.ReadBufferSize)
|
||||
})
|
||||
|
||||
suite.T().Run("should create HTTP/2 client for https endpoints", func(t *testing.T) {
|
||||
@@ -37,7 +40,7 @@ func (suite *Tests) TestBaseClient_createHttpClient() {
|
||||
|
||||
httpClient := client.createHttpClient()
|
||||
assert.NotNil(httpClient)
|
||||
assert.Equal(30*time.Second, httpClient.Timeout)
|
||||
assert.Equal(60*time.Second, httpClient.Timeout)
|
||||
assert.NotNil(httpClient.Transport)
|
||||
|
||||
// Check that it's using HTTP/2 transport
|
||||
@@ -46,9 +49,9 @@ func (suite *Tests) TestBaseClient_createHttpClient() {
|
||||
assert.True(transport.AllowHTTP)
|
||||
assert.NotNil(transport.TLSClientConfig)
|
||||
assert.True(transport.TLSClientConfig.InsecureSkipVerify)
|
||||
assert.Equal(30*time.Second, transport.ReadIdleTimeout)
|
||||
assert.Equal(10*time.Second, transport.PingTimeout)
|
||||
assert.Equal(10*time.Second, transport.WriteByteTimeout)
|
||||
assert.Equal(60*time.Second, transport.ReadIdleTimeout)
|
||||
assert.Equal(20*time.Second, transport.PingTimeout)
|
||||
assert.Equal(20*time.Second, transport.WriteByteTimeout)
|
||||
})
|
||||
|
||||
suite.T().Run("should handle invalid endpoints", func(t *testing.T) {
|
||||
@@ -91,20 +94,23 @@ func (suite *Tests) TestBaseClient_createHttpClient() {
|
||||
}
|
||||
|
||||
func (suite *Tests) TestBaseClient_createHttpClient_transportSettings() {
|
||||
suite.T().Run("should configure HTTP/2 transport correctly for http://", func(t *testing.T) {
|
||||
suite.T().Run("should configure HTTP/1.1 transport correctly for http://", func(t *testing.T) {
|
||||
client := NewConnection()
|
||||
client.SetEndpoint("http://example.com/graphql")
|
||||
|
||||
httpClient := client.createHttpClient()
|
||||
transport := httpClient.Transport.(*http2.Transport)
|
||||
transport := httpClient.Transport.(*http.Transport)
|
||||
|
||||
// Verify all HTTP/2 transport settings for h2c
|
||||
assert.True(transport.AllowHTTP)
|
||||
assert.Nil(transport.TLSClientConfig) // No TLS for http://
|
||||
assert.Equal(30*time.Second, transport.ReadIdleTimeout)
|
||||
assert.Equal(10*time.Second, transport.PingTimeout)
|
||||
assert.Equal(10*time.Second, transport.WriteByteTimeout)
|
||||
// Verify all HTTP/1.1 transport settings
|
||||
assert.Equal(100, transport.MaxIdleConns)
|
||||
assert.Equal(50, transport.MaxConnsPerHost)
|
||||
assert.Equal(10, transport.MaxIdleConnsPerHost)
|
||||
assert.Equal(60*time.Second, transport.IdleConnTimeout)
|
||||
assert.Equal(20*time.Second, transport.ResponseHeaderTimeout)
|
||||
assert.False(transport.DisableKeepAlives)
|
||||
assert.True(transport.DisableCompression) // Request compression disabled to prevent "trailing garbage" errors
|
||||
assert.Equal(4096, transport.WriteBufferSize)
|
||||
assert.Equal(4096, transport.ReadBufferSize)
|
||||
})
|
||||
|
||||
suite.T().Run("should configure HTTP/2 transport correctly", func(t *testing.T) {
|
||||
@@ -118,14 +124,14 @@ func (suite *Tests) TestBaseClient_createHttpClient_transportSettings() {
|
||||
assert.True(transport.AllowHTTP)
|
||||
assert.NotNil(transport.TLSClientConfig)
|
||||
assert.True(transport.TLSClientConfig.InsecureSkipVerify)
|
||||
assert.Equal(30*time.Second, transport.ReadIdleTimeout)
|
||||
assert.Equal(10*time.Second, transport.PingTimeout)
|
||||
assert.Equal(10*time.Second, transport.WriteByteTimeout)
|
||||
assert.Equal(60*time.Second, transport.ReadIdleTimeout)
|
||||
assert.Equal(20*time.Second, transport.PingTimeout)
|
||||
assert.Equal(20*time.Second, transport.WriteByteTimeout)
|
||||
})
|
||||
}
|
||||
|
||||
func (suite *Tests) TestBaseClient_createHttpClient_logging() {
|
||||
suite.T().Run("should log HTTP/2 h2c usage", func(t *testing.T) {
|
||||
suite.T().Run("should log HTTP/1.1 usage", func(t *testing.T) {
|
||||
client := NewConnection()
|
||||
client.SetEndpoint("http://example.com/graphql")
|
||||
|
||||
|
||||
@@ -47,24 +47,39 @@ func NewConnection() (b *BaseClient) {
|
||||
})
|
||||
|
||||
b = &BaseClient{
|
||||
endpoint: envutil.Getenv("GRAPHQL_ENDPOINT", "https://api.github.com/graphql"),
|
||||
responseType: envutil.Getenv("GRAPHQL_OUTPUT", "string"),
|
||||
Logger: logger,
|
||||
cache: cache.New(time.Duration(envutil.GetInt("GRAPHQL_CACHE_TTL", 5)) * time.Second),
|
||||
cache_global: envutil.GetBool("GRAPHQL_CACHE_ENABLED", false),
|
||||
retries_enable: envutil.GetBool("GRAPHQL_RETRIES_ENABLE", false),
|
||||
retries_delay: time.Duration(envutil.GetInt("GRAPHQL_RETRIES_DELAY", 250) * int(time.Millisecond)),
|
||||
retries_number: envutil.GetInt("GRAPHQL_RETRIES_NUMBER", 3),
|
||||
retries_patterns: parseRetryPatterns(envutil.Getenv("GRAPHQL_RETRIES_PATTERNS", "postgres,connection,timeout,transaction,could not,temporarily unavailable,deadlock")),
|
||||
minify_queries: envutil.GetBool("GRAPHQL_MINIFY_QUERIES", true), // Default: enabled for production efficiency
|
||||
endpoint: envutil.Getenv("GRAPHQL_ENDPOINT", "https://api.github.com/graphql"),
|
||||
responseType: envutil.Getenv("GRAPHQL_OUTPUT", "string"),
|
||||
Logger: logger,
|
||||
cache: cache.New(time.Duration(envutil.GetInt("GRAPHQL_CACHE_TTL", 5)) * time.Second),
|
||||
cache_global: envutil.GetBool("GRAPHQL_CACHE_ENABLED", false),
|
||||
retries_enable: envutil.GetBool("GRAPHQL_RETRIES_ENABLE", false),
|
||||
retries_delay: time.Duration(envutil.GetInt("GRAPHQL_RETRIES_DELAY", 250) * int(time.Millisecond)),
|
||||
retries_number: envutil.GetInt("GRAPHQL_RETRIES_NUMBER", 3),
|
||||
retries_patterns: parseRetryPatterns(envutil.Getenv("GRAPHQL_RETRIES_PATTERNS", "postgres,connection,timeout,transaction,could not,temporarily unavailable,deadlock")),
|
||||
minify_queries: envutil.GetBool("GRAPHQL_MINIFY_QUERIES", true), // Default: enabled for production efficiency
|
||||
pool_warmup_enabled: envutil.GetBool("GRAPHQL_POOL_WARMUP_ENABLED", false),
|
||||
pool_size: envutil.GetInt("GRAPHQL_POOL_SIZE", 5),
|
||||
pool_warmup_query: envutil.Getenv("GRAPHQL_POOL_WARMUP_QUERY", "query{__typename}"),
|
||||
pool_health_interval: time.Duration(envutil.GetInt("GRAPHQL_POOL_HEALTH_INTERVAL", 30)) * time.Second,
|
||||
pool_stop: make(chan bool, 1),
|
||||
}
|
||||
b.client = b.createHttpClient()
|
||||
b.Logger.Debug(&logging.LogMessage{
|
||||
Message: "Created new GraphQL client connection",
|
||||
Pairs: map[string]interface{}{
|
||||
"values": b,
|
||||
"endpoint": b.endpoint,
|
||||
"pool_warmup": b.pool_warmup_enabled,
|
||||
"pool_size": b.pool_size,
|
||||
"pool_health_check": b.pool_health_interval,
|
||||
},
|
||||
})
|
||||
|
||||
// Initialize connection pool if warmup is enabled
|
||||
if b.pool_warmup_enabled {
|
||||
b.warmupConnectionPool()
|
||||
b.startPoolHealthMonitor()
|
||||
}
|
||||
|
||||
return b
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,250 @@
|
||||
package gql
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
libpack_logger "github.com/lukaszraczylo/go-simple-graphql/logging"
|
||||
)
|
||||
|
||||
// warmupConnectionPool pre-creates connections by executing warmup queries
|
||||
// This establishes connections in the pool before actual traffic arrives
|
||||
func (b *BaseClient) warmupConnectionPool() {
|
||||
b.Logger.Info(&libpack_logger.LogMessage{
|
||||
Message: "Warming up connection pool",
|
||||
Pairs: map[string]interface{}{
|
||||
"pool_size": b.pool_size,
|
||||
"warmup_query": b.pool_warmup_query,
|
||||
"endpoint": b.endpoint,
|
||||
},
|
||||
})
|
||||
|
||||
startTime := time.Now()
|
||||
var wg sync.WaitGroup
|
||||
successCount := 0
|
||||
failCount := 0
|
||||
var mu sync.Mutex
|
||||
|
||||
// Pre-create connections concurrently
|
||||
for i := 0; i < b.pool_size; i++ {
|
||||
wg.Add(1)
|
||||
go func(index int) {
|
||||
defer wg.Done()
|
||||
|
||||
// Execute warmup query to establish connection
|
||||
_, err := b.Query(b.pool_warmup_query, nil, nil)
|
||||
|
||||
mu.Lock()
|
||||
if err != nil {
|
||||
failCount++
|
||||
b.Logger.Warning(&libpack_logger.LogMessage{
|
||||
Message: "Failed to warm up connection",
|
||||
Pairs: map[string]interface{}{
|
||||
"connection_index": index,
|
||||
"error": err.Error(),
|
||||
},
|
||||
})
|
||||
} else {
|
||||
successCount++
|
||||
b.Logger.Debug(&libpack_logger.LogMessage{
|
||||
Message: "Connection warmed up successfully",
|
||||
Pairs: map[string]interface{}{
|
||||
"connection_index": index,
|
||||
},
|
||||
})
|
||||
}
|
||||
mu.Unlock()
|
||||
}(i)
|
||||
}
|
||||
|
||||
wg.Wait()
|
||||
duration := time.Since(startTime)
|
||||
|
||||
b.Logger.Info(&libpack_logger.LogMessage{
|
||||
Message: "Connection pool warmup completed",
|
||||
Pairs: map[string]interface{}{
|
||||
"successful": successCount,
|
||||
"failed": failCount,
|
||||
"total": b.pool_size,
|
||||
"duration_ms": duration.Milliseconds(),
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
// startPoolHealthMonitor starts a background goroutine that periodically
|
||||
// checks and maintains pool health by sending health check queries
|
||||
func (b *BaseClient) startPoolHealthMonitor() {
|
||||
b.Logger.Info(&libpack_logger.LogMessage{
|
||||
Message: "Starting connection pool health monitor",
|
||||
Pairs: map[string]interface{}{
|
||||
"check_interval": b.pool_health_interval,
|
||||
"pool_size": b.pool_size,
|
||||
},
|
||||
})
|
||||
|
||||
go func() {
|
||||
ticker := time.NewTicker(b.pool_health_interval)
|
||||
defer ticker.Stop()
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-ticker.C:
|
||||
b.performPoolHealthCheck()
|
||||
case <-b.pool_stop:
|
||||
b.Logger.Info(&libpack_logger.LogMessage{
|
||||
Message: "Stopping connection pool health monitor",
|
||||
Pairs: nil,
|
||||
})
|
||||
return
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
// performPoolHealthCheck executes health check queries to maintain pool connections
|
||||
// This keeps connections alive and detects/replaces dead connections
|
||||
func (b *BaseClient) performPoolHealthCheck() {
|
||||
b.Logger.Debug(&libpack_logger.LogMessage{
|
||||
Message: "Performing pool health check",
|
||||
Pairs: map[string]interface{}{
|
||||
"pool_size": b.pool_size,
|
||||
},
|
||||
})
|
||||
|
||||
startTime := time.Now()
|
||||
var wg sync.WaitGroup
|
||||
healthyCount := 0
|
||||
unhealthyCount := 0
|
||||
var mu sync.Mutex
|
||||
|
||||
// Check a subset of connections to ensure pool health
|
||||
// We check pool_size/2 to balance between thoroughness and overhead
|
||||
checkCount := max(1, b.pool_size/2)
|
||||
|
||||
for i := 0; i < checkCount; i++ {
|
||||
wg.Add(1)
|
||||
go func(index int) {
|
||||
defer wg.Done()
|
||||
|
||||
// Create a context with timeout for health check
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
|
||||
// Use a channel to handle timeout
|
||||
done := make(chan error, 1)
|
||||
go func() {
|
||||
_, err := b.Query(b.pool_warmup_query, nil, nil)
|
||||
done <- err
|
||||
}()
|
||||
|
||||
select {
|
||||
case err := <-done:
|
||||
mu.Lock()
|
||||
if err != nil {
|
||||
unhealthyCount++
|
||||
b.Logger.Warning(&libpack_logger.LogMessage{
|
||||
Message: "Pool health check failed",
|
||||
Pairs: map[string]interface{}{
|
||||
"check_index": index,
|
||||
"error": err.Error(),
|
||||
},
|
||||
})
|
||||
} else {
|
||||
healthyCount++
|
||||
}
|
||||
mu.Unlock()
|
||||
case <-ctx.Done():
|
||||
mu.Lock()
|
||||
unhealthyCount++
|
||||
b.Logger.Warning(&libpack_logger.LogMessage{
|
||||
Message: "Pool health check timeout",
|
||||
Pairs: map[string]interface{}{
|
||||
"check_index": index,
|
||||
},
|
||||
})
|
||||
mu.Unlock()
|
||||
}
|
||||
}(i)
|
||||
}
|
||||
|
||||
wg.Wait()
|
||||
duration := time.Since(startTime)
|
||||
|
||||
b.Logger.Debug(&libpack_logger.LogMessage{
|
||||
Message: "Pool health check completed",
|
||||
Pairs: map[string]interface{}{
|
||||
"healthy": healthyCount,
|
||||
"unhealthy": unhealthyCount,
|
||||
"checked": checkCount,
|
||||
"duration_ms": duration.Milliseconds(),
|
||||
},
|
||||
})
|
||||
|
||||
// If more than 50% of checks failed, trigger a pool refresh
|
||||
if unhealthyCount > checkCount/2 {
|
||||
b.Logger.Warning(&libpack_logger.LogMessage{
|
||||
Message: "Pool health degraded, triggering refresh",
|
||||
Pairs: map[string]interface{}{
|
||||
"unhealthy_ratio": float64(unhealthyCount) / float64(checkCount),
|
||||
},
|
||||
})
|
||||
b.refreshConnectionPool()
|
||||
}
|
||||
}
|
||||
|
||||
// refreshConnectionPool attempts to refresh the connection pool
|
||||
// by executing warmup queries to replace stale connections
|
||||
func (b *BaseClient) refreshConnectionPool() {
|
||||
b.Logger.Info(&libpack_logger.LogMessage{
|
||||
Message: "Refreshing connection pool",
|
||||
Pairs: map[string]interface{}{
|
||||
"pool_size": b.pool_size,
|
||||
},
|
||||
})
|
||||
|
||||
var wg sync.WaitGroup
|
||||
refreshCount := max(1, b.pool_size/3) // Refresh 1/3 of pool
|
||||
|
||||
for i := 0; i < refreshCount; i++ {
|
||||
wg.Add(1)
|
||||
go func(index int) {
|
||||
defer wg.Done()
|
||||
|
||||
_, err := b.Query(b.pool_warmup_query, nil, nil)
|
||||
if err != nil {
|
||||
b.Logger.Debug(&libpack_logger.LogMessage{
|
||||
Message: "Pool refresh connection failed",
|
||||
Pairs: map[string]interface{}{
|
||||
"connection_index": index,
|
||||
"error": err.Error(),
|
||||
},
|
||||
})
|
||||
} else {
|
||||
b.Logger.Debug(&libpack_logger.LogMessage{
|
||||
Message: "Pool connection refreshed",
|
||||
Pairs: map[string]interface{}{
|
||||
"connection_index": index,
|
||||
},
|
||||
})
|
||||
}
|
||||
}(i)
|
||||
}
|
||||
|
||||
wg.Wait()
|
||||
}
|
||||
|
||||
// StopPoolMonitor gracefully stops the pool health monitor
|
||||
// Call this when shutting down the application
|
||||
// Uses sync.Once to ensure it only executes once, preventing race conditions
|
||||
func (b *BaseClient) StopPoolMonitor() {
|
||||
if b.pool_warmup_enabled {
|
||||
b.pool_stop_once.Do(func() {
|
||||
b.Logger.Info(&libpack_logger.LogMessage{
|
||||
Message: "Stopping pool monitor",
|
||||
Pairs: nil,
|
||||
})
|
||||
b.pool_stop <- true
|
||||
})
|
||||
}
|
||||
}
|
||||
+135
@@ -0,0 +1,135 @@
|
||||
package gql
|
||||
|
||||
import (
|
||||
"os"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func (suite *Tests) TestPoolWarmup() {
|
||||
suite.T().Run("should create client with pool warmup disabled by default", func(t *testing.T) {
|
||||
// Save original env vars
|
||||
originalPoolEnabled := os.Getenv("GRAPHQL_POOL_WARMUP_ENABLED")
|
||||
originalEndpoint := os.Getenv("GRAPHQL_ENDPOINT")
|
||||
defer func() {
|
||||
os.Setenv("GRAPHQL_POOL_WARMUP_ENABLED", originalPoolEnabled)
|
||||
os.Setenv("GRAPHQL_ENDPOINT", originalEndpoint)
|
||||
}()
|
||||
|
||||
os.Setenv("GRAPHQL_POOL_WARMUP_ENABLED", "false")
|
||||
os.Setenv("GRAPHQL_ENDPOINT", mockServer.URL)
|
||||
|
||||
client := NewConnection()
|
||||
defer client.StopPoolMonitor()
|
||||
|
||||
assert.False(client.pool_warmup_enabled)
|
||||
assert.Equal(5, client.pool_size) // Default size
|
||||
})
|
||||
|
||||
suite.T().Run("should warmup pool when enabled", func(t *testing.T) {
|
||||
originalPoolEnabled := os.Getenv("GRAPHQL_POOL_WARMUP_ENABLED")
|
||||
originalPoolSize := os.Getenv("GRAPHQL_POOL_SIZE")
|
||||
originalEndpoint := os.Getenv("GRAPHQL_ENDPOINT")
|
||||
defer func() {
|
||||
os.Setenv("GRAPHQL_POOL_WARMUP_ENABLED", originalPoolEnabled)
|
||||
os.Setenv("GRAPHQL_POOL_SIZE", originalPoolSize)
|
||||
os.Setenv("GRAPHQL_ENDPOINT", originalEndpoint)
|
||||
}()
|
||||
|
||||
os.Setenv("GRAPHQL_POOL_WARMUP_ENABLED", "true")
|
||||
os.Setenv("GRAPHQL_POOL_SIZE", "3")
|
||||
os.Setenv("GRAPHQL_ENDPOINT", mockServer.URL)
|
||||
|
||||
startTime := time.Now()
|
||||
client := NewConnection()
|
||||
defer client.StopPoolMonitor()
|
||||
|
||||
duration := time.Since(startTime)
|
||||
|
||||
assert.True(client.pool_warmup_enabled)
|
||||
assert.Equal(3, client.pool_size)
|
||||
// Warmup should complete relatively quickly for 3 connections
|
||||
assert.Less(duration.Seconds(), 5.0, "Pool warmup took too long")
|
||||
})
|
||||
|
||||
suite.T().Run("should use custom warmup query", func(t *testing.T) {
|
||||
originalPoolEnabled := os.Getenv("GRAPHQL_POOL_WARMUP_ENABLED")
|
||||
originalQuery := os.Getenv("GRAPHQL_POOL_WARMUP_QUERY")
|
||||
originalEndpoint := os.Getenv("GRAPHQL_ENDPOINT")
|
||||
defer func() {
|
||||
os.Setenv("GRAPHQL_POOL_WARMUP_ENABLED", originalPoolEnabled)
|
||||
os.Setenv("GRAPHQL_POOL_WARMUP_QUERY", originalQuery)
|
||||
os.Setenv("GRAPHQL_ENDPOINT", originalEndpoint)
|
||||
}()
|
||||
|
||||
os.Setenv("GRAPHQL_POOL_WARMUP_ENABLED", "false") // Don't actually warmup, just test configuration
|
||||
os.Setenv("GRAPHQL_POOL_WARMUP_QUERY", "query{__schema{queryType{name}}}")
|
||||
os.Setenv("GRAPHQL_ENDPOINT", mockServer.URL)
|
||||
|
||||
client := NewConnection()
|
||||
defer client.StopPoolMonitor()
|
||||
|
||||
assert.Equal("query{__schema{queryType{name}}}", client.pool_warmup_query)
|
||||
})
|
||||
}
|
||||
|
||||
func (suite *Tests) TestPoolHealthMonitor() {
|
||||
suite.T().Run("should stop pool monitor gracefully", func(t *testing.T) {
|
||||
client := CreateTestClient()
|
||||
client.pool_warmup_enabled = false
|
||||
|
||||
// Should not panic
|
||||
client.StopPoolMonitor()
|
||||
|
||||
assert.True(true, "Handled stop gracefully when monitor not running")
|
||||
})
|
||||
|
||||
suite.T().Run("should handle multiple stop calls", func(t *testing.T) {
|
||||
originalPoolEnabled := os.Getenv("GRAPHQL_POOL_WARMUP_ENABLED")
|
||||
originalPoolSize := os.Getenv("GRAPHQL_POOL_SIZE")
|
||||
originalEndpoint := os.Getenv("GRAPHQL_ENDPOINT")
|
||||
defer func() {
|
||||
os.Setenv("GRAPHQL_POOL_WARMUP_ENABLED", originalPoolEnabled)
|
||||
os.Setenv("GRAPHQL_POOL_SIZE", originalPoolSize)
|
||||
os.Setenv("GRAPHQL_ENDPOINT", originalEndpoint)
|
||||
}()
|
||||
|
||||
os.Setenv("GRAPHQL_POOL_WARMUP_ENABLED", "true")
|
||||
os.Setenv("GRAPHQL_POOL_SIZE", "2")
|
||||
os.Setenv("GRAPHQL_ENDPOINT", mockServer.URL)
|
||||
|
||||
client := NewConnection()
|
||||
|
||||
// Call stop multiple times - should not panic
|
||||
client.StopPoolMonitor()
|
||||
client.StopPoolMonitor()
|
||||
client.StopPoolMonitor()
|
||||
|
||||
assert.True(true, "Handled multiple stop calls gracefully")
|
||||
})
|
||||
}
|
||||
|
||||
func (suite *Tests) TestPoolHealthCheck() {
|
||||
suite.T().Run("should perform health check without panic", func(t *testing.T) {
|
||||
client := CreateTestClient()
|
||||
client.pool_warmup_enabled = true
|
||||
client.pool_size = 2
|
||||
client.pool_warmup_query = "query{__typename}"
|
||||
|
||||
// Perform a health check - should not panic
|
||||
client.performPoolHealthCheck()
|
||||
|
||||
assert.True(true, "Health check completed")
|
||||
})
|
||||
|
||||
suite.T().Run("should refresh pool without panic", func(t *testing.T) {
|
||||
client := CreateTestClient()
|
||||
client.pool_size = 2
|
||||
client.pool_warmup_query = "query{__typename}"
|
||||
|
||||
// Perform pool refresh - should not panic
|
||||
client.refreshConnectionPool()
|
||||
|
||||
assert.True(true, "Pool refresh completed")
|
||||
})
|
||||
}
|
||||
@@ -75,15 +75,20 @@ func (suite *RequestCompressionTestSuite) TestRequestCompression() {
|
||||
httpClient := client.createHttpClient()
|
||||
assert.NotNil(httpClient, "HTTP client should be created")
|
||||
|
||||
// Verify transport settings - now using HTTP/2 for all endpoints
|
||||
transport, ok := httpClient.Transport.(*http2.Transport)
|
||||
assert.True(ok, "Should be http2.Transport for all endpoints (h2c for http://, TLS for https://)")
|
||||
if ok {
|
||||
assert.True(transport.DisableCompression, "DisableCompression should be true to prevent trailing garbage")
|
||||
if strings.HasPrefix(endpoint, "http://") {
|
||||
assert.True(transport.AllowHTTP, "AllowHTTP should be true for h2c (HTTP/2 Cleartext)")
|
||||
assert.Nil(transport.TLSClientConfig, "TLSClientConfig should be nil for http:// endpoints")
|
||||
} else {
|
||||
// Verify transport settings based on protocol
|
||||
if strings.HasPrefix(endpoint, "http://") {
|
||||
// HTTP/1.1 transport for http:// (h2c rarely supported)
|
||||
transport, ok := httpClient.Transport.(*http.Transport)
|
||||
assert.True(ok, "Should be http.Transport for http:// endpoints")
|
||||
if ok {
|
||||
assert.True(transport.DisableCompression, "DisableCompression should be true to prevent trailing garbage")
|
||||
}
|
||||
} else {
|
||||
// HTTP/2 transport for https://
|
||||
transport, ok := httpClient.Transport.(*http2.Transport)
|
||||
assert.True(ok, "Should be http2.Transport for https:// endpoints")
|
||||
if ok {
|
||||
assert.True(transport.DisableCompression, "DisableCompression should be true to prevent trailing garbage")
|
||||
assert.True(transport.AllowHTTP, "AllowHTTP should be true")
|
||||
assert.NotNil(transport.TLSClientConfig, "TLSClientConfig should be set for https:// endpoints")
|
||||
}
|
||||
|
||||
+19
-12
@@ -2,6 +2,7 @@ package gql
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
cache "github.com/lukaszraczylo/go-simple-graphql/cache"
|
||||
@@ -9,18 +10,24 @@ import (
|
||||
)
|
||||
|
||||
type BaseClient struct {
|
||||
cache *cache.Cache
|
||||
Logger *logging.Logger
|
||||
client *http.Client
|
||||
endpoint string
|
||||
responseType string
|
||||
retries_delay time.Duration
|
||||
retries_number int
|
||||
retries_patterns []string // Error patterns that should trigger retries
|
||||
MaxGoRoutines int
|
||||
cache_global bool
|
||||
retries_enable bool
|
||||
minify_queries bool // Enable GraphQL query minification (default: true)
|
||||
cache *cache.Cache
|
||||
Logger *logging.Logger
|
||||
client *http.Client
|
||||
endpoint string
|
||||
responseType string
|
||||
retries_delay time.Duration
|
||||
retries_number int
|
||||
retries_patterns []string // Error patterns that should trigger retries
|
||||
pool_size int // Number of connections to pre-warm
|
||||
pool_warmup_enabled bool // Enable connection pool warmup
|
||||
pool_warmup_query string // Query to use for warming up connections
|
||||
pool_health_interval time.Duration // Interval for pool health checks
|
||||
pool_stop chan bool // Channel to stop pool monitor
|
||||
pool_stop_once sync.Once // Ensures StopPoolMonitor is called only once
|
||||
MaxGoRoutines int
|
||||
cache_global bool
|
||||
retries_enable bool
|
||||
minify_queries bool // Enable GraphQL query minification (default: true)
|
||||
}
|
||||
|
||||
type Query struct {
|
||||
|
||||
Reference in New Issue
Block a user