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 | ||
|
|
cd16b92dca | ||
|
|
b314a25549 |
@@ -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
|
||||
|
||||
@@ -37,15 +39,113 @@ Therefore, I present you the simple client to which you can copy & paste your gr
|
||||
* `GRAPHQL_CACHE_TTL` - Cache TTL in seconds for SELECT type of queries. Default: `5`
|
||||
* `GRAPHQL_OUTPUT` - Output format. Default: `string`, available: `byte`, `string`, `mapstring`
|
||||
* `LOG_LEVEL` - Logging level. Default: `info` available: `debug`, `info`, `warn`, `error`
|
||||
* `GRAPHQL_RETRIES_ENABLE` - Should retries be enabled? Default: `true`
|
||||
* `GRAPHQL_RETRIES_NUMBER` - Number of retries: Default: `1`
|
||||
* `GRAPHQL_RETRIES_ENABLE` - Should retries be enabled? Default: `false`
|
||||
* `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
|
||||
|
||||
* `gql.SetEndpoint('your-endpoint-url')` - modifies endpoint, without the need to set the environment variable
|
||||
* `gql.SetOutput('byte')` - modifies output format, without the need to set the environment variable
|
||||
|
||||
### Retries
|
||||
|
||||
The library supports automatic retries for both HTTP-level errors and GraphQL errors that match configurable patterns.
|
||||
|
||||
**Retry behavior:**
|
||||
- **HTTP errors** (status codes outside 200-299): Always retried if retries are enabled
|
||||
- **GraphQL errors**: Only retried if the error message contains one of the configured patterns
|
||||
- **Non-matching errors**: Fail immediately without retrying (e.g., validation errors, field not found)
|
||||
|
||||
**Configuring retryable patterns:**
|
||||
|
||||
By default, errors containing these patterns will trigger retries:
|
||||
- `postgres` - Database connection/transaction errors
|
||||
- `connection` - Network connection issues
|
||||
- `timeout` - Request timeout errors
|
||||
- `transaction` - Database transaction conflicts
|
||||
- `could not` - Generic transient failures
|
||||
- `temporarily unavailable` - Service unavailability
|
||||
- `deadlock` - Database deadlocks
|
||||
|
||||
You can customize these patterns using the `GRAPHQL_RETRIES_PATTERNS` environment variable:
|
||||
|
||||
```bash
|
||||
export GRAPHQL_RETRIES_PATTERNS="postgres,connection,timeout,custom-error-pattern"
|
||||
```
|
||||
|
||||
**Example:** To handle flaky Hasura database connections, enable retries:
|
||||
|
||||
```bash
|
||||
export GRAPHQL_RETRIES_ENABLE=true
|
||||
export GRAPHQL_RETRIES_NUMBER=3
|
||||
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:
|
||||
|
||||
+92
-90
@@ -3,12 +3,11 @@ package gql
|
||||
import (
|
||||
"bytes"
|
||||
"compress/gzip"
|
||||
"crypto/tls"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
"unicode/utf8"
|
||||
|
||||
@@ -17,8 +16,6 @@ import (
|
||||
libpack_logger "github.com/lukaszraczylo/go-simple-graphql/logging"
|
||||
)
|
||||
|
||||
// Size limit constants are defined in query.go
|
||||
|
||||
// min returns the smaller of two integers
|
||||
func min(a, b int) int {
|
||||
if a < b {
|
||||
@@ -35,38 +32,49 @@ func max(a, b int) int {
|
||||
return b
|
||||
}
|
||||
|
||||
// getDefaultClient creates a default HTTP client with optimized settings
|
||||
// This is called dynamically to ensure environment variables are read at runtime
|
||||
func getDefaultClient() *http.Client {
|
||||
return &http.Client{
|
||||
Transport: &http.Transport{
|
||||
MaxIdleConns: 100,
|
||||
MaxIdleConnsPerHost: 100,
|
||||
IdleConnTimeout: 90 * time.Second,
|
||||
DisableCompression: true, // Disable automatic request compression to prevent "trailing garbage" errors
|
||||
ForceAttemptHTTP2: true,
|
||||
TLSClientConfig: getTLSConfig(),
|
||||
},
|
||||
Timeout: 30 * time.Second,
|
||||
// isRetryableError checks if GraphQL errors contain patterns that indicate a transient failure
|
||||
// that should be retried (e.g., database connection issues, timeouts, transaction conflicts)
|
||||
func isRetryableError(errors []struct {
|
||||
Message interface{} `json:"message"`
|
||||
}, patterns []string) bool {
|
||||
if len(errors) == 0 || len(patterns) == 0 {
|
||||
return false
|
||||
}
|
||||
|
||||
for _, err := range errors {
|
||||
// Try to extract error message as string
|
||||
var msgStr string
|
||||
switch msg := err.Message.(type) {
|
||||
case string:
|
||||
msgStr = msg
|
||||
case map[string]interface{}:
|
||||
// Handle nested error structures (e.g., {"message": "...", "extensions": {...}})
|
||||
if nestedMsg, ok := msg["message"].(string); ok {
|
||||
msgStr = nestedMsg
|
||||
}
|
||||
default:
|
||||
// If we can't convert to string, try fmt.Sprint as fallback
|
||||
msgStr = fmt.Sprint(msg)
|
||||
}
|
||||
|
||||
if msgStr == "" {
|
||||
continue
|
||||
}
|
||||
|
||||
// Check if error message contains any of the configured retry patterns (case-insensitive)
|
||||
msgLower := strings.ToLower(msgStr)
|
||||
for _, pattern := range patterns {
|
||||
if strings.Contains(msgLower, strings.ToLower(pattern)) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
// getTLSConfig returns TLS configuration based on environment settings
|
||||
// By default, TLS verification is enabled for security.
|
||||
// Set GRAPHQL_INSECURE_SKIP_VERIFY=true to disable verification (NOT RECOMMENDED for production)
|
||||
func getTLSConfig() *tls.Config {
|
||||
skipVerify := os.Getenv("GRAPHQL_INSECURE_SKIP_VERIFY") == "true"
|
||||
|
||||
if skipVerify {
|
||||
// Log warning when running in insecure mode
|
||||
fmt.Fprintf(os.Stderr, "WARNING: TLS certificate verification is disabled. This is insecure and should not be used in production.\n")
|
||||
}
|
||||
|
||||
return &tls.Config{
|
||||
InsecureSkipVerify: skipVerify,
|
||||
MinVersion: tls.VersionTLS12, // Enforce minimum TLS 1.2 for security
|
||||
}
|
||||
}
|
||||
// Note: defaultClient removed - all clients should be created via NewConnection()
|
||||
// which properly configures HTTP/2 transport with correct settings
|
||||
|
||||
func (qe *QueryExecutor) executeQuery() ([]byte, error) {
|
||||
// Reuse buffer from pool to avoid allocations
|
||||
@@ -76,10 +84,7 @@ func (qe *QueryExecutor) executeQuery() ([]byte, error) {
|
||||
|
||||
buf.Write(qe.Query)
|
||||
|
||||
qe.mu.RLock()
|
||||
endpoint := qe.endpoint
|
||||
qe.mu.RUnlock()
|
||||
httpRequest, err := http.NewRequest(http.MethodPost, endpoint, nil)
|
||||
httpRequest, err := http.NewRequest(http.MethodPost, qe.endpoint, nil)
|
||||
if err != nil {
|
||||
qe.Logger.Error(&libpack_logger.LogMessage{
|
||||
Message: "Can't create HTTP request",
|
||||
@@ -103,9 +108,7 @@ func (qe *QueryExecutor) executeQuery() ([]byte, error) {
|
||||
|
||||
retriesMax := 1
|
||||
if qe.Retries {
|
||||
qe.mu.RLock()
|
||||
retriesMax = qe.retries_number
|
||||
qe.mu.RUnlock()
|
||||
}
|
||||
|
||||
var queryResult queryResults
|
||||
@@ -123,8 +126,8 @@ func (qe *QueryExecutor) executeQuery() ([]byte, error) {
|
||||
"body_size": len(requestBody),
|
||||
"contains_jsonQuery": bytes.Contains(requestBody, []byte(`"jsonQuery"`)),
|
||||
"contains_base64": bytes.Contains(requestBody, []byte("eyJ")),
|
||||
"first_100_chars": sanitizeForLogging(string(requestBody[:min(100, len(requestBody))])),
|
||||
"last_50_chars": sanitizeForLogging(string(requestBody[max(0, len(requestBody)-50):])),
|
||||
"first_100_chars": string(requestBody[:min(100, len(requestBody))]),
|
||||
"last_50_chars": string(requestBody[max(0, len(requestBody)-50):]),
|
||||
"is_valid_utf8": utf8.Valid(requestBody),
|
||||
"has_null_bytes": bytes.Contains(requestBody, []byte{0}),
|
||||
"has_control_chars": hasControlChars(requestBody),
|
||||
@@ -154,14 +157,16 @@ func (qe *QueryExecutor) executeQuery() ([]byte, error) {
|
||||
},
|
||||
})
|
||||
|
||||
// Use default client if custom client is not set
|
||||
qe.mu.RLock()
|
||||
client := qe.client
|
||||
qe.mu.RUnlock()
|
||||
if client == nil {
|
||||
client = getDefaultClient()
|
||||
// Client should always be set via NewConnection(), but check for safety
|
||||
if qe.client == nil {
|
||||
qe.Logger.Error(&libpack_logger.LogMessage{
|
||||
Message: "HTTP client not initialized - use NewConnection() to create client",
|
||||
Pairs: nil,
|
||||
})
|
||||
return fmt.Errorf("HTTP client not initialized")
|
||||
}
|
||||
httpResponse, err := client.Do(httpRequest)
|
||||
|
||||
httpResponse, err := qe.client.Do(httpRequest)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -184,32 +189,16 @@ func (qe *QueryExecutor) executeQuery() ([]byte, error) {
|
||||
},
|
||||
})
|
||||
|
||||
// Check Content-Length header if available for early size validation
|
||||
if contentLength := httpResponse.ContentLength; contentLength > 0 && contentLength > MaxResponseSize {
|
||||
return fmt.Errorf("response size %d exceeds maximum allowed size of %d bytes", contentLength, MaxResponseSize)
|
||||
}
|
||||
|
||||
// First, read the entire response body into a buffer with size limit
|
||||
// First, read the entire response body into a buffer
|
||||
rawBuf := bufferPool.Get().(*bytes.Buffer)
|
||||
rawBuf.Reset()
|
||||
defer bufferPool.Put(rawBuf)
|
||||
|
||||
// Use LimitedReader to prevent reading more than MaxResponseSize
|
||||
limitedReader := &io.LimitedReader{
|
||||
R: httpResponse.Body,
|
||||
N: MaxResponseSize + 1, // Read one extra byte to detect oversized responses
|
||||
}
|
||||
|
||||
n, err := io.Copy(rawBuf, limitedReader)
|
||||
_, err = io.Copy(rawBuf, httpResponse.Body)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error reading HTTP response body: %w", err)
|
||||
}
|
||||
|
||||
// Check if response exceeded size limit
|
||||
if n > MaxResponseSize {
|
||||
return fmt.Errorf("response size exceeds maximum allowed size of %d bytes", MaxResponseSize)
|
||||
}
|
||||
|
||||
rawData := rawBuf.Bytes()
|
||||
qe.Logger.Debug(&libpack_logger.LogMessage{
|
||||
Message: "Read raw response data",
|
||||
@@ -372,6 +361,38 @@ func (qe *QueryExecutor) executeQuery() ([]byte, error) {
|
||||
return fmt.Errorf("error unmarshalling HTTP response: %w", err)
|
||||
}
|
||||
|
||||
// Check for GraphQL errors in the response
|
||||
if len(queryResult.Errors) > 0 {
|
||||
// Determine if this is a retryable error based on configured patterns
|
||||
if isRetryableError(queryResult.Errors, qe.retries_patterns) {
|
||||
// Log as warning since we'll retry
|
||||
qe.Logger.Warning(&libpack_logger.LogMessage{
|
||||
Message: "Retryable GraphQL error detected",
|
||||
Pairs: map[string]interface{}{"errors": queryResult.Errors},
|
||||
})
|
||||
// Return error to trigger retry mechanism
|
||||
return fmt.Errorf("retryable error executing query: %s", queryResult.Errors)
|
||||
}
|
||||
|
||||
// Non-retryable error - log and fail immediately without retrying
|
||||
qe.Logger.Error(&libpack_logger.LogMessage{
|
||||
Message: "Non-retryable GraphQL error",
|
||||
Pairs: map[string]interface{}{"errors": queryResult.Errors},
|
||||
})
|
||||
// Use retry.Unrecoverable to skip remaining retry attempts
|
||||
return retry.Unrecoverable(fmt.Errorf("error executing query: %s", queryResult.Errors))
|
||||
}
|
||||
|
||||
// Check for null data in the response
|
||||
if queryResult.Data == nil {
|
||||
qe.Logger.Error(&libpack_logger.LogMessage{
|
||||
Message: "GraphQL query returned no data",
|
||||
Pairs: map[string]interface{}{"error": "data field is null"},
|
||||
})
|
||||
// Null data is typically not retryable (indicates query structure issue)
|
||||
return retry.Unrecoverable(errors.New("error executing query: no data"))
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
retry.OnRetry(func(n uint, err error) {
|
||||
@@ -382,42 +403,23 @@ func (qe *QueryExecutor) executeQuery() ([]byte, error) {
|
||||
}),
|
||||
retry.Attempts(uint(retriesMax)),
|
||||
retry.DelayType(retry.BackOffDelay),
|
||||
retry.Delay(func() time.Duration {
|
||||
qe.mu.RLock()
|
||||
defer qe.mu.RUnlock()
|
||||
return qe.retries_delay
|
||||
}()),
|
||||
retry.Delay(time.Duration(qe.retries_delay)),
|
||||
retry.MaxDelay(10*time.Second),
|
||||
retry.LastErrorOnly(true),
|
||||
)
|
||||
if err != nil {
|
||||
qe.Logger.Debug(&libpack_logger.LogMessage{
|
||||
Message: "Error executing HTTP request",
|
||||
qe.Logger.Error(&libpack_logger.LogMessage{
|
||||
Message: "Query execution failed after retries",
|
||||
Pairs: map[string]interface{}{"error": err.Error()},
|
||||
})
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if len(queryResult.Errors) > 0 {
|
||||
qe.Logger.Debug(&libpack_logger.LogMessage{
|
||||
Message: "Error executing query",
|
||||
Pairs: map[string]interface{}{"error": queryResult.Errors},
|
||||
})
|
||||
return nil, fmt.Errorf("error executing query: %s", queryResult.Errors)
|
||||
}
|
||||
|
||||
if queryResult.Data == nil {
|
||||
qe.Logger.Debug(&libpack_logger.LogMessage{
|
||||
Message: "Error executing query",
|
||||
Pairs: map[string]interface{}{"error": "no data"},
|
||||
})
|
||||
return nil, errors.New("error executing query: no data")
|
||||
}
|
||||
|
||||
// At this point, we have successfully executed the query and validated the response
|
||||
// Marshal queryResult.Data
|
||||
jsonData, err := json.Marshal(queryResult.Data)
|
||||
if err != nil {
|
||||
qe.Logger.Debug(&libpack_logger.LogMessage{
|
||||
qe.Logger.Error(&libpack_logger.LogMessage{
|
||||
Message: "Error marshalling query result",
|
||||
Pairs: map[string]interface{}{"error": err.Error(), "data": queryResult.Data},
|
||||
})
|
||||
|
||||
@@ -185,7 +185,7 @@ func (suite *Tests) TestQueryExecutor_executeQuery() {
|
||||
assert.Equal(3, attempts) // Should have retried twice before succeeding
|
||||
})
|
||||
|
||||
suite.T().Run("should use default client when none provided", func(t *testing.T) {
|
||||
suite.T().Run("should return error when client not initialized", func(t *testing.T) {
|
||||
client := CreateTestClient()
|
||||
|
||||
qe := &QueryExecutor{
|
||||
@@ -196,12 +196,13 @@ func (suite *Tests) TestQueryExecutor_executeQuery() {
|
||||
Retries: false,
|
||||
}
|
||||
qe.endpoint = mockServer.URL
|
||||
qe.client = nil // No client provided
|
||||
qe.client = nil // No client provided (should always use NewConnection())
|
||||
qe.cache = client.cache
|
||||
|
||||
result, err := qe.executeQuery()
|
||||
assert.NoError(err)
|
||||
assert.NotNil(result)
|
||||
assert.Error(err)
|
||||
assert.Nil(result)
|
||||
assert.Contains(err.Error(), "HTTP client not initialized")
|
||||
})
|
||||
|
||||
suite.T().Run("should set default Content-Type header", func(t *testing.T) {
|
||||
|
||||
@@ -2,9 +2,7 @@ package gql
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@@ -12,88 +10,73 @@ import (
|
||||
"golang.org/x/net/http2"
|
||||
)
|
||||
|
||||
func (b *BaseClient) createHttpClient() (*http.Client, error) {
|
||||
// Optimized connection pool settings for better performance
|
||||
httpTransport := &http.Transport{
|
||||
MaxIdleConns: 100, // Reduced from 512
|
||||
MaxConnsPerHost: 50, // Reduced from 512
|
||||
MaxIdleConnsPerHost: 10, // Reduced from 512
|
||||
IdleConnTimeout: 30 * time.Second, // Increased for better reuse
|
||||
ResponseHeaderTimeout: 10 * time.Second, // Reduced for faster timeouts
|
||||
DisableKeepAlives: false,
|
||||
DisableCompression: true, // Disable automatic request compression to prevent "trailing garbage" errors
|
||||
WriteBufferSize: 4096, // Optimize buffer size
|
||||
ReadBufferSize: 4096, // Optimize buffer size
|
||||
}
|
||||
func (b *BaseClient) createHttpClient() (http_client *http.Client) {
|
||||
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
|
||||
}
|
||||
|
||||
var http_client *http.Client
|
||||
|
||||
// Protect read of endpoint field
|
||||
b.mu.RLock()
|
||||
endpoint := b.endpoint
|
||||
b.mu.RUnlock()
|
||||
|
||||
if strings.HasPrefix(endpoint, "http://") {
|
||||
http_client = &http.Client{
|
||||
Timeout: 30 * time.Second, // Increased for better reliability
|
||||
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/1.1 over http",
|
||||
Message: "Using HTTP/1.1 for http:// endpoint",
|
||||
Pairs: nil,
|
||||
})
|
||||
} else if strings.HasPrefix(endpoint, "https://") {
|
||||
tlsClientConfig := getHTTPClientTLSConfig()
|
||||
if strings.HasPrefix(endpoint, "https://") && tlsClientConfig.InsecureSkipVerify {
|
||||
b.Logger.Warning(&libpack_logger.LogMessage{
|
||||
Message: "TLS certificate verification is disabled. This is insecure and should not be used in production.",
|
||||
})
|
||||
} 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: 30 * time.Second, // Increased for better reuse
|
||||
PingTimeout: 10 * time.Second, // Reduced for faster detection
|
||||
WriteByteTimeout: 10 * time.Second, // Add write timeout
|
||||
DisableCompression: true, // Disable automatic request compression to prevent "trailing garbage" errors
|
||||
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: 30 * time.Second, // Increased for better reliability
|
||||
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 https",
|
||||
Message: "Using HTTP/2 for https:// endpoint",
|
||||
Pairs: nil,
|
||||
})
|
||||
} else {
|
||||
b.Logger.Critical(&libpack_logger.LogMessage{
|
||||
Message: "Invalid endpoint - neither http or https",
|
||||
Message: "Invalid endpoint - must start with http:// or https://",
|
||||
})
|
||||
// Return error instead of nil to prevent nil pointer dereference
|
||||
return nil, fmt.Errorf("invalid endpoint: %s - must start with http:// or https://", endpoint)
|
||||
}
|
||||
return http_client, nil
|
||||
}
|
||||
|
||||
// getHTTPClientTLSConfig returns TLS configuration for HTTP client
|
||||
// By default, TLS verification is enabled for security.
|
||||
// Set GRAPHQL_INSECURE_SKIP_VERIFY=true to disable verification (NOT RECOMMENDED for production)
|
||||
func getHTTPClientTLSConfig() *tls.Config {
|
||||
skipVerify := os.Getenv("GRAPHQL_INSECURE_SKIP_VERIFY") == "true"
|
||||
|
||||
if skipVerify {
|
||||
// Log warning to stderr when running in insecure mode
|
||||
fmt.Fprintf(os.Stderr, "WARNING: TLS certificate verification is disabled. This is insecure and should not be used in production.\n")
|
||||
}
|
||||
|
||||
return &tls.Config{
|
||||
InsecureSkipVerify: skipVerify,
|
||||
MinVersion: tls.VersionTLS12, // Enforce minimum TLS 1.2 for security
|
||||
return nil
|
||||
}
|
||||
|
||||
return http_client
|
||||
}
|
||||
|
||||
+30
-103
@@ -2,32 +2,32 @@ package gql
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"os"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"golang.org/x/net/http2"
|
||||
)
|
||||
|
||||
// Note: http.Transport import is kept for http.ErrUseLastResponse usage in redirect tests
|
||||
|
||||
func (suite *Tests) TestBaseClient_createHttpClient() {
|
||||
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, err := client.createHttpClient()
|
||||
assert.NoError(err)
|
||||
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 regular HTTP 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.Equal(100, transport.MaxIdleConns)
|
||||
assert.Equal(50, transport.MaxConnsPerHost)
|
||||
assert.Equal(10, transport.MaxIdleConnsPerHost)
|
||||
assert.Equal(30*time.Second, transport.IdleConnTimeout)
|
||||
assert.Equal(10*time.Second, transport.ResponseHeaderTimeout)
|
||||
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)
|
||||
@@ -35,24 +35,12 @@ func (suite *Tests) TestBaseClient_createHttpClient() {
|
||||
})
|
||||
|
||||
suite.T().Run("should create HTTP/2 client for https endpoints", func(t *testing.T) {
|
||||
// Set environment variable for test to skip TLS verification
|
||||
oldValue := os.Getenv("GRAPHQL_INSECURE_SKIP_VERIFY")
|
||||
os.Setenv("GRAPHQL_INSECURE_SKIP_VERIFY", "true")
|
||||
t.Cleanup(func() {
|
||||
if oldValue != "" {
|
||||
os.Setenv("GRAPHQL_INSECURE_SKIP_VERIFY", oldValue)
|
||||
} else {
|
||||
os.Unsetenv("GRAPHQL_INSECURE_SKIP_VERIFY")
|
||||
}
|
||||
})
|
||||
|
||||
client := NewConnection()
|
||||
client.SetEndpoint("https://example.com/graphql")
|
||||
|
||||
httpClient, err := client.createHttpClient()
|
||||
assert.NoError(err)
|
||||
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
|
||||
@@ -61,18 +49,16 @@ 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) {
|
||||
client := NewConnection()
|
||||
client.SetEndpoint("ftp://example.com/graphql")
|
||||
|
||||
httpClient, err := client.createHttpClient()
|
||||
assert.Error(err)
|
||||
assert.Contains(err.Error(), "invalid endpoint")
|
||||
httpClient := client.createHttpClient()
|
||||
assert.Nil(httpClient)
|
||||
})
|
||||
|
||||
@@ -80,24 +66,21 @@ func (suite *Tests) TestBaseClient_createHttpClient() {
|
||||
client := NewConnection()
|
||||
client.SetEndpoint("http://example.com/graphql")
|
||||
|
||||
httpClient, err := client.createHttpClient()
|
||||
assert.NoError(err)
|
||||
httpClient := client.createHttpClient()
|
||||
assert.NotNil(httpClient)
|
||||
assert.NotNil(httpClient.CheckRedirect)
|
||||
|
||||
// Test redirect policy
|
||||
req, _ := http.NewRequest("GET", "http://example.com", nil)
|
||||
err2 := httpClient.CheckRedirect(req, []*http.Request{})
|
||||
assert.Equal(http.ErrUseLastResponse, err2)
|
||||
err := httpClient.CheckRedirect(req, []*http.Request{})
|
||||
assert.Equal(http.ErrUseLastResponse, err)
|
||||
})
|
||||
|
||||
suite.T().Run("should handle empty endpoint", func(t *testing.T) {
|
||||
client := NewConnection()
|
||||
client.SetEndpoint("")
|
||||
|
||||
httpClient, err := client.createHttpClient()
|
||||
assert.Error(err)
|
||||
assert.Contains(err.Error(), "invalid endpoint")
|
||||
httpClient := client.createHttpClient()
|
||||
assert.Nil(httpClient)
|
||||
})
|
||||
|
||||
@@ -105,53 +88,25 @@ func (suite *Tests) TestBaseClient_createHttpClient() {
|
||||
client := NewConnection()
|
||||
client.SetEndpoint("example.com/graphql")
|
||||
|
||||
httpClient, err := client.createHttpClient()
|
||||
assert.Error(err)
|
||||
assert.Contains(err.Error(), "invalid endpoint")
|
||||
httpClient := client.createHttpClient()
|
||||
assert.Nil(httpClient)
|
||||
})
|
||||
|
||||
suite.T().Run("should enable TLS verification by default for https endpoints", func(t *testing.T) {
|
||||
// Ensure the environment variable is not set
|
||||
oldValue := os.Getenv("GRAPHQL_INSECURE_SKIP_VERIFY")
|
||||
os.Unsetenv("GRAPHQL_INSECURE_SKIP_VERIFY")
|
||||
t.Cleanup(func() {
|
||||
if oldValue != "" {
|
||||
os.Setenv("GRAPHQL_INSECURE_SKIP_VERIFY", oldValue)
|
||||
}
|
||||
})
|
||||
|
||||
client := NewConnection()
|
||||
client.SetEndpoint("https://example.com/graphql")
|
||||
|
||||
httpClient, err := client.createHttpClient()
|
||||
assert.NoError(err)
|
||||
assert.NotNil(httpClient)
|
||||
|
||||
// Check that TLS verification is enabled by default
|
||||
transport, ok := httpClient.Transport.(*http2.Transport)
|
||||
assert.True(ok)
|
||||
assert.NotNil(transport.TLSClientConfig)
|
||||
assert.False(transport.TLSClientConfig.InsecureSkipVerify) // Should be false by default
|
||||
})
|
||||
}
|
||||
|
||||
func (suite *Tests) TestBaseClient_createHttpClient_transportSettings() {
|
||||
suite.T().Run("should configure HTTP transport correctly", 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, err := client.createHttpClient()
|
||||
assert.NoError(err)
|
||||
assert.NotNil(httpClient)
|
||||
httpClient := client.createHttpClient()
|
||||
transport := httpClient.Transport.(*http.Transport)
|
||||
|
||||
// Verify all transport settings
|
||||
// Verify all HTTP/1.1 transport settings
|
||||
assert.Equal(100, transport.MaxIdleConns)
|
||||
assert.Equal(50, transport.MaxConnsPerHost)
|
||||
assert.Equal(10, transport.MaxIdleConnsPerHost)
|
||||
assert.Equal(30*time.Second, transport.IdleConnTimeout)
|
||||
assert.Equal(10*time.Second, transport.ResponseHeaderTimeout)
|
||||
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)
|
||||
@@ -159,32 +114,19 @@ func (suite *Tests) TestBaseClient_createHttpClient_transportSettings() {
|
||||
})
|
||||
|
||||
suite.T().Run("should configure HTTP/2 transport correctly", func(t *testing.T) {
|
||||
// Set environment variable for test to skip TLS verification
|
||||
oldValue := os.Getenv("GRAPHQL_INSECURE_SKIP_VERIFY")
|
||||
os.Setenv("GRAPHQL_INSECURE_SKIP_VERIFY", "true")
|
||||
t.Cleanup(func() {
|
||||
if oldValue != "" {
|
||||
os.Setenv("GRAPHQL_INSECURE_SKIP_VERIFY", oldValue)
|
||||
} else {
|
||||
os.Unsetenv("GRAPHQL_INSECURE_SKIP_VERIFY")
|
||||
}
|
||||
})
|
||||
|
||||
client := NewConnection()
|
||||
client.SetEndpoint("https://example.com/graphql")
|
||||
|
||||
httpClient, err := client.createHttpClient()
|
||||
assert.NoError(err)
|
||||
assert.NotNil(httpClient)
|
||||
httpClient := client.createHttpClient()
|
||||
transport := httpClient.Transport.(*http2.Transport)
|
||||
|
||||
// Verify all HTTP/2 transport settings
|
||||
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)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -195,30 +137,17 @@ func (suite *Tests) TestBaseClient_createHttpClient_logging() {
|
||||
|
||||
// This test verifies that the function runs without error
|
||||
// The actual logging is tested indirectly through the logger tests
|
||||
httpClient, err := client.createHttpClient()
|
||||
assert.NoError(err)
|
||||
httpClient := client.createHttpClient()
|
||||
assert.NotNil(httpClient)
|
||||
})
|
||||
|
||||
suite.T().Run("should log HTTP/2 usage", func(t *testing.T) {
|
||||
// Set environment variable for test to skip TLS verification
|
||||
oldValue := os.Getenv("GRAPHQL_INSECURE_SKIP_VERIFY")
|
||||
os.Setenv("GRAPHQL_INSECURE_SKIP_VERIFY", "true")
|
||||
t.Cleanup(func() {
|
||||
if oldValue != "" {
|
||||
os.Setenv("GRAPHQL_INSECURE_SKIP_VERIFY", oldValue)
|
||||
} else {
|
||||
os.Unsetenv("GRAPHQL_INSECURE_SKIP_VERIFY")
|
||||
}
|
||||
})
|
||||
|
||||
client := NewConnection()
|
||||
client.SetEndpoint("https://example.com/graphql")
|
||||
|
||||
// This test verifies that the function runs without error
|
||||
// The actual logging is tested indirectly through the logger tests
|
||||
httpClient, err := client.createHttpClient()
|
||||
assert.NoError(err)
|
||||
httpClient := client.createHttpClient()
|
||||
assert.NotNil(httpClient)
|
||||
})
|
||||
|
||||
@@ -228,9 +157,7 @@ func (suite *Tests) TestBaseClient_createHttpClient_logging() {
|
||||
|
||||
// This test verifies that the function runs without error
|
||||
// and returns nil for invalid endpoints (Critical() won't exit in tests)
|
||||
httpClient, err := client.createHttpClient()
|
||||
assert.Error(err)
|
||||
assert.Contains(err.Error(), "invalid endpoint")
|
||||
httpClient := client.createHttpClient()
|
||||
assert.Nil(httpClient)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -10,7 +10,6 @@ import (
|
||||
// TestQueryJSONEncoding verifies that the Query struct serializes correctly
|
||||
// without including the JsonQuery field, preventing "Trailing garbage" errors
|
||||
func TestQueryJSONEncoding(t *testing.T) {
|
||||
t.Parallel()
|
||||
assert := assertions.New(t)
|
||||
client := CreateTestClient()
|
||||
|
||||
@@ -48,7 +47,6 @@ func TestQueryJSONEncoding(t *testing.T) {
|
||||
|
||||
// TestQueryJSONEncodingPreventsTrailingGarbage ensures the fix prevents the specific error
|
||||
func TestQueryJSONEncodingPreventsTrailingGarbage(t *testing.T) {
|
||||
t.Parallel()
|
||||
assert := assertions.New(t)
|
||||
client := CreateTestClient()
|
||||
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
package gql
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/gookit/goutil/envutil"
|
||||
@@ -10,6 +10,22 @@ import (
|
||||
logging "github.com/lukaszraczylo/go-simple-graphql/logging"
|
||||
)
|
||||
|
||||
// parseRetryPatterns parses a comma-separated string of retry patterns into a slice
|
||||
func parseRetryPatterns(patterns string) []string {
|
||||
if patterns == "" {
|
||||
return []string{}
|
||||
}
|
||||
parts := strings.Split(patterns, ",")
|
||||
result := make([]string, 0, len(parts))
|
||||
for _, p := range parts {
|
||||
trimmed := strings.TrimSpace(p)
|
||||
if trimmed != "" {
|
||||
result = append(result, trimmed)
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func NewConnection() (b *BaseClient) {
|
||||
// Read LOG_LEVEL environment variable
|
||||
logLevelStr := envutil.Getenv("LOG_LEVEL", "info")
|
||||
@@ -31,78 +47,58 @@ 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),
|
||||
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),
|
||||
}
|
||||
client, err := b.createHttpClient()
|
||||
if err != nil {
|
||||
b.Logger.Critical(&logging.LogMessage{
|
||||
Message: "Failed to create HTTP client",
|
||||
Pairs: map[string]interface{}{
|
||||
"error": err.Error(),
|
||||
},
|
||||
})
|
||||
// For backward compatibility, we'll still return the client but it won't work
|
||||
return b
|
||||
}
|
||||
b.client = client
|
||||
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
|
||||
}
|
||||
|
||||
func (b *BaseClient) SetEndpoint(endpoint string) {
|
||||
b.mu.Lock()
|
||||
defer b.mu.Unlock()
|
||||
b.endpoint = endpoint
|
||||
}
|
||||
|
||||
func (b *BaseClient) SetOutput(responseType string) error {
|
||||
// Validate responseType - allowed values are "byte", "string", "mapstring"
|
||||
allowedTypes := map[string]bool{
|
||||
"byte": true,
|
||||
"string": true,
|
||||
"mapstring": true,
|
||||
}
|
||||
|
||||
if !allowedTypes[responseType] {
|
||||
return fmt.Errorf("invalid response type: %s. Allowed values are: byte, string, mapstring", responseType)
|
||||
}
|
||||
|
||||
b.mu.Lock()
|
||||
defer b.mu.Unlock()
|
||||
func (b *BaseClient) SetOutput(responseType string) {
|
||||
// allowed are byte, string, mapstring
|
||||
// check if responseType is allowed
|
||||
// TODO: implement
|
||||
b.responseType = responseType
|
||||
|
||||
b.Logger.Debug(&logging.LogMessage{
|
||||
Message: "Response type updated",
|
||||
Pairs: map[string]interface{}{
|
||||
"response_type": responseType,
|
||||
},
|
||||
})
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *BaseClient) SetHTTPClient(client *http.Client) {
|
||||
b.mu.Lock()
|
||||
defer b.mu.Unlock()
|
||||
b.client = client
|
||||
}
|
||||
|
||||
func (b *BaseClient) SetQueryMinification(enabled bool) {
|
||||
b.mu.Lock()
|
||||
defer b.mu.Unlock()
|
||||
b.minify_queries = enabled
|
||||
b.Logger.Debug(&logging.LogMessage{
|
||||
Message: "GraphQL query minification setting updated",
|
||||
|
||||
@@ -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")
|
||||
})
|
||||
}
|
||||
@@ -10,13 +10,6 @@ import (
|
||||
libpack_logger "github.com/lukaszraczylo/go-simple-graphql/logging"
|
||||
)
|
||||
|
||||
// Size limit constants for security
|
||||
const (
|
||||
MaxQuerySize = 100 * 1024 // 100KB max query size
|
||||
MaxVariableSize = 10 * 1024 // 10KB max variables size
|
||||
MaxResponseSize = 10 * 1024 * 1024 // 10MB max response size
|
||||
)
|
||||
|
||||
// minifyGraphQLQuery removes unnecessary whitespace from GraphQL queries while preserving string literals
|
||||
func minifyGraphQLQuery(query string) string {
|
||||
if query == "" {
|
||||
@@ -245,10 +238,7 @@ func (b *BaseClient) compileQuery(queryPartials ...any) *Query {
|
||||
|
||||
// Apply query minification if enabled (default: true)
|
||||
finalQuery := query
|
||||
b.mu.RLock()
|
||||
minifyEnabled := b.minify_queries
|
||||
b.mu.RUnlock()
|
||||
if minifyEnabled {
|
||||
if b.minify_queries {
|
||||
originalSize := len(query)
|
||||
minifiedQuery := minifyGraphQLQuery(query)
|
||||
minifiedSize := len(minifiedQuery)
|
||||
@@ -278,36 +268,9 @@ func (b *BaseClient) compileQuery(queryPartials ...any) *Query {
|
||||
}
|
||||
|
||||
func (b *BaseClient) Query(query string, variables map[string]interface{}, headers map[string]interface{}) (any, error) {
|
||||
// Validate query size limit
|
||||
if len(query) > MaxQuerySize {
|
||||
b.Logger.Error(&libpack_logger.LogMessage{
|
||||
Message: "Query exceeds maximum size limit",
|
||||
Pairs: map[string]interface{}{
|
||||
"query_size": len(query),
|
||||
"max_size": MaxQuerySize,
|
||||
},
|
||||
})
|
||||
return nil, fmt.Errorf("query size %d exceeds maximum allowed size of %d bytes", len(query), MaxQuerySize)
|
||||
}
|
||||
|
||||
// Process flags before compilation to avoid recompilation
|
||||
enableCache, enableRetries, cleanedVariables := processFlags(variables, headers)
|
||||
|
||||
// Validate variables size limit
|
||||
if cleanedVariables != nil {
|
||||
variablesJSON, err := json.Marshal(cleanedVariables)
|
||||
if err == nil && len(variablesJSON) > MaxVariableSize {
|
||||
b.Logger.Error(&libpack_logger.LogMessage{
|
||||
Message: "Variables exceed maximum size limit",
|
||||
Pairs: map[string]interface{}{
|
||||
"variables_size": len(variablesJSON),
|
||||
"max_size": MaxVariableSize,
|
||||
},
|
||||
})
|
||||
return nil, fmt.Errorf("variables size %d exceeds maximum allowed size of %d bytes", len(variablesJSON), MaxVariableSize)
|
||||
}
|
||||
}
|
||||
|
||||
// Compile query once with cleaned variables
|
||||
compiledQuery := b.compileQuery(query, cleanedVariables)
|
||||
if compiledQuery == nil || compiledQuery.JsonQuery == nil {
|
||||
@@ -319,14 +282,11 @@ func (b *BaseClient) Query(query string, variables map[string]interface{}, heade
|
||||
}
|
||||
b.Logger.Debug(&libpack_logger.LogMessage{
|
||||
Message: "Compiled query",
|
||||
Pairs: map[string]interface{}{"query": sanitizeForLogging(compiledQuery.Query)},
|
||||
Pairs: map[string]interface{}{"query": compiledQuery},
|
||||
})
|
||||
|
||||
var queryHash string
|
||||
b.mu.RLock()
|
||||
cacheGlobal := b.cache_global
|
||||
b.mu.RUnlock()
|
||||
if (enableCache || cacheGlobal) && strutil.HasPrefix(compiledQuery.Query, "query") {
|
||||
if (enableCache || b.cache_global) && strutil.HasPrefix(compiledQuery.Query, "query") {
|
||||
b.Logger.Debug(&libpack_logger.LogMessage{
|
||||
Message: "Cache enabled",
|
||||
Pairs: nil,
|
||||
@@ -335,13 +295,13 @@ func (b *BaseClient) Query(query string, variables map[string]interface{}, heade
|
||||
if cachedValue := b.cacheLookup(queryHash); cachedValue != nil {
|
||||
b.Logger.Debug(&libpack_logger.LogMessage{
|
||||
Message: "Cache hit",
|
||||
Pairs: map[string]interface{}{"query_hash": queryHash},
|
||||
Pairs: map[string]interface{}{"query": compiledQuery},
|
||||
})
|
||||
return b.decodeResponse(cachedValue)
|
||||
}
|
||||
b.Logger.Debug(&libpack_logger.LogMessage{
|
||||
Message: "Cache miss",
|
||||
Pairs: map[string]interface{}{"query_hash": queryHash},
|
||||
Pairs: map[string]interface{}{"query": compiledQuery},
|
||||
})
|
||||
}
|
||||
|
||||
@@ -355,11 +315,7 @@ func (b *BaseClient) Query(query string, variables map[string]interface{}, heade
|
||||
}
|
||||
return "no-cache"
|
||||
}(),
|
||||
Retries: func() bool {
|
||||
b.mu.RLock()
|
||||
defer b.mu.RUnlock()
|
||||
return enableRetries || b.retries_enable
|
||||
}(),
|
||||
Retries: enableRetries || b.retries_enable,
|
||||
}
|
||||
|
||||
rv, err := q.executeQuery()
|
||||
|
||||
@@ -8,7 +8,6 @@ import (
|
||||
)
|
||||
|
||||
func TestMinifyGraphQLQuery(t *testing.T) {
|
||||
t.Parallel()
|
||||
assert := assertions.New(t)
|
||||
|
||||
tests := []struct {
|
||||
@@ -132,7 +131,6 @@ func TestMinifyGraphQLQuery(t *testing.T) {
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
result := minifyGraphQLQuery(tt.input)
|
||||
assert.Equal(tt.expected, result, "Minified query should match expected output")
|
||||
|
||||
@@ -152,7 +150,6 @@ func TestMinifyGraphQLQuery(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestMinifyGraphQLQuerySizeReduction(t *testing.T) {
|
||||
t.Parallel()
|
||||
assert := assertions.New(t)
|
||||
|
||||
// Test with a heavily formatted query
|
||||
@@ -211,11 +208,9 @@ func TestMinifyGraphQLQuerySizeReduction(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestBaseClientQueryMinification(t *testing.T) {
|
||||
t.Parallel()
|
||||
assert := assertions.New(t)
|
||||
|
||||
t.Run("should_minify_queries_by_default", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
client := CreateTestClient()
|
||||
|
||||
// Verify minification is enabled by default
|
||||
@@ -238,7 +233,6 @@ func TestBaseClientQueryMinification(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("should_respect_minification_disabled", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
client := CreateTestClient()
|
||||
client.SetQueryMinification(false)
|
||||
|
||||
@@ -260,7 +254,6 @@ func TestBaseClientQueryMinification(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("should_handle_string_literals_correctly", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
client := CreateTestClient()
|
||||
|
||||
query := `mutation test($value: String!) {
|
||||
@@ -285,7 +278,6 @@ func TestBaseClientQueryMinification(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("should_demonstrate_size_reduction_with_production_case", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
client := CreateTestClient()
|
||||
|
||||
// Use the production case from trailing garbage test
|
||||
@@ -332,7 +324,6 @@ func TestBaseClientQueryMinification(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestIsAlphaNumeric(t *testing.T) {
|
||||
t.Parallel()
|
||||
assert := assertions.New(t)
|
||||
|
||||
tests := []struct {
|
||||
@@ -360,7 +351,6 @@ func TestIsAlphaNumeric(t *testing.T) {
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(string(tt.char), func(t *testing.T) {
|
||||
t.Parallel()
|
||||
result := isAlphaNumeric(tt.char)
|
||||
assert.Equal(tt.expected, result, "isAlphaNumeric should correctly identify character type")
|
||||
})
|
||||
@@ -368,11 +358,9 @@ func TestIsAlphaNumeric(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestQueryMinificationEnvironmentVariable(t *testing.T) {
|
||||
t.Parallel()
|
||||
assert := assertions.New(t)
|
||||
|
||||
t.Run("should_respect_GRAPHQL_MINIFY_QUERIES_env_var", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
// This test would require setting environment variables
|
||||
// For now, we'll test the default behavior
|
||||
client := NewConnection()
|
||||
@@ -383,23 +371,19 @@ func TestQueryMinificationEnvironmentVariable(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestQueryMinificationEdgeCases(t *testing.T) {
|
||||
t.Parallel()
|
||||
assert := assertions.New(t)
|
||||
|
||||
t.Run("should_handle_empty_query", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
result := minifyGraphQLQuery("")
|
||||
assert.Equal("", result, "Empty query should remain empty")
|
||||
})
|
||||
|
||||
t.Run("should_handle_query_with_only_whitespace", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
result := minifyGraphQLQuery(" \n\t \n ")
|
||||
assert.Equal("", result, "Query with only whitespace should become empty")
|
||||
})
|
||||
|
||||
t.Run("should_handle_nested_quotes", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
query := `mutation test {
|
||||
insert_test(value: "String with \"nested quotes\" and 'mixed' quotes") {
|
||||
id
|
||||
@@ -411,7 +395,6 @@ func TestQueryMinificationEdgeCases(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("should_preserve_spaces_between_keywords", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
query := `query test($var: String!) {
|
||||
field(where: {id: {_eq: $var}}) {
|
||||
id
|
||||
|
||||
@@ -11,6 +11,7 @@ import (
|
||||
|
||||
assertions "github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/suite"
|
||||
"golang.org/x/net/http2"
|
||||
)
|
||||
|
||||
type RequestCompressionTestSuite struct {
|
||||
@@ -71,21 +72,26 @@ func (suite *RequestCompressionTestSuite) TestRequestCompression() {
|
||||
client.endpoint = endpoint
|
||||
|
||||
// Create HTTP client using the same logic as the library
|
||||
httpClient, err := client.createHttpClient()
|
||||
assert.NoError(err, "HTTP client should be created without error")
|
||||
httpClient := client.createHttpClient()
|
||||
assert.NotNil(httpClient, "HTTP client should be created")
|
||||
|
||||
// Verify transport settings
|
||||
// Verify transport settings based on protocol
|
||||
if strings.HasPrefix(endpoint, "http://") {
|
||||
// HTTP/1.1 transport
|
||||
// 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")
|
||||
assert.True(ok, "Should be http.Transport for http:// endpoints")
|
||||
if ok {
|
||||
assert.True(transport.DisableCompression, "DisableCompression should be true for HTTP transport")
|
||||
assert.True(transport.DisableCompression, "DisableCompression should be true to prevent trailing garbage")
|
||||
}
|
||||
} else {
|
||||
// HTTP/2 transport - this is where the issue might be
|
||||
// We need to ensure HTTP/2 transport also has compression disabled
|
||||
// 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")
|
||||
}
|
||||
}
|
||||
|
||||
qe := &QueryExecutor{
|
||||
|
||||
-63
@@ -1,63 +0,0 @@
|
||||
package gql
|
||||
|
||||
import (
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Sensitive data patterns to redact from logs
|
||||
var (
|
||||
// Pattern for common sensitive field names in JSON/GraphQL
|
||||
sensitiveFieldPattern = regexp.MustCompile(`(?i)"?(password|passwd|pwd|token|apikey|api_key|api-key|secret|authorization|auth|bearer|credentials|private_key|private-key|access_token|refresh_token|client_secret|session|cookie)"?\s*:\s*"[^"]*"`)
|
||||
|
||||
// Pattern for authorization headers
|
||||
authHeaderPattern = regexp.MustCompile(`(?i)(authorization|x-api-key|x-auth-token|bearer)\s*:\s*[^\s,}]+`)
|
||||
|
||||
// Pattern for basic auth in URLs
|
||||
basicAuthURLPattern = regexp.MustCompile(`(https?://)([^:]+):([^@]+)@`)
|
||||
|
||||
// Pattern for JWT tokens
|
||||
jwtPattern = regexp.MustCompile(`eyJ[A-Za-z0-9_-]+\.eyJ[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+`)
|
||||
)
|
||||
|
||||
const redactedText = "[REDACTED]"
|
||||
|
||||
// sanitizeForLogging removes sensitive data from strings before logging
|
||||
// This includes passwords, tokens, API keys, authorization headers, etc.
|
||||
func sanitizeForLogging(input string) string {
|
||||
if input == "" {
|
||||
return input
|
||||
}
|
||||
|
||||
// Create a working copy
|
||||
sanitized := input
|
||||
|
||||
// Redact sensitive fields in JSON/GraphQL
|
||||
sanitized = sensitiveFieldPattern.ReplaceAllStringFunc(sanitized, func(match string) string {
|
||||
// Preserve the field name but redact the value
|
||||
parts := strings.SplitN(match, ":", 2)
|
||||
if len(parts) == 2 {
|
||||
fieldName := parts[0]
|
||||
return fieldName + `: "` + redactedText + `"`
|
||||
}
|
||||
return redactedText
|
||||
})
|
||||
|
||||
// Redact authorization headers
|
||||
sanitized = authHeaderPattern.ReplaceAllStringFunc(sanitized, func(match string) string {
|
||||
parts := strings.SplitN(match, ":", 2)
|
||||
if len(parts) == 2 {
|
||||
headerName := parts[0]
|
||||
return headerName + ": " + redactedText
|
||||
}
|
||||
return redactedText
|
||||
})
|
||||
|
||||
// Redact basic auth in URLs
|
||||
sanitized = basicAuthURLPattern.ReplaceAllString(sanitized, "${1}"+redactedText+":"+redactedText+"@")
|
||||
|
||||
// Redact JWT tokens
|
||||
sanitized = jwtPattern.ReplaceAllString(sanitized, redactedText)
|
||||
|
||||
return sanitized
|
||||
}
|
||||
+18
-12
@@ -10,18 +10,24 @@ import (
|
||||
)
|
||||
|
||||
type BaseClient struct {
|
||||
mu sync.RWMutex // Protects all fields for thread-safe access
|
||||
cache *cache.Cache
|
||||
Logger *logging.Logger
|
||||
client *http.Client
|
||||
endpoint string
|
||||
responseType string
|
||||
retries_delay time.Duration
|
||||
retries_number int
|
||||
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 {
|
||||
|
||||
+2
-18
@@ -2,7 +2,6 @@ package gql
|
||||
|
||||
import (
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
assertions "github.com/stretchr/testify/assert"
|
||||
@@ -14,19 +13,11 @@ type Tests struct {
|
||||
}
|
||||
|
||||
var (
|
||||
assert *assertions.Assertions
|
||||
mockServer *httptest.Server
|
||||
originalInsecureSkipValue string
|
||||
assert *assertions.Assertions
|
||||
mockServer *httptest.Server
|
||||
)
|
||||
|
||||
func (suite *Tests) SetupSuite() {
|
||||
// Save original environment variable value
|
||||
originalInsecureSkipValue = os.Getenv("GRAPHQL_INSECURE_SKIP_VERIFY")
|
||||
|
||||
// Set environment variable to skip TLS verification for test mock server
|
||||
// The mock server uses self-signed certificates
|
||||
os.Setenv("GRAPHQL_INSECURE_SKIP_VERIFY", "true")
|
||||
|
||||
// Start mock GraphQL server
|
||||
mockServer = StartMockServer()
|
||||
}
|
||||
@@ -39,13 +30,6 @@ func (suite *Tests) TearDownSuite() {
|
||||
|
||||
// Cleanup shared test cache
|
||||
CleanupTestCache()
|
||||
|
||||
// Restore original environment variable value
|
||||
if originalInsecureSkipValue != "" {
|
||||
os.Setenv("GRAPHQL_INSECURE_SKIP_VERIFY", originalInsecureSkipValue)
|
||||
} else {
|
||||
os.Unsetenv("GRAPHQL_INSECURE_SKIP_VERIFY")
|
||||
}
|
||||
}
|
||||
|
||||
func (suite *Tests) SetupTest() {
|
||||
|
||||
Reference in New Issue
Block a user