Compare commits

...
2 Commits
Author SHA1 Message Date
lukaszraczylo 71a14721c5 fixup! Add support of h2c over cleartext http 2025-11-03 23:40:04 +00:00
lukaszraczylo cd16b92dca Add support of h2c over cleartext http 2025-11-03 23:33:28 +00:00
6 changed files with 69 additions and 63 deletions
+4 -2
View File
@@ -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
+11 -23
View File
@@ -3,7 +3,6 @@ package gql
import (
"bytes"
"compress/gzip"
"crypto/tls"
"errors"
"fmt"
"io"
@@ -74,23 +73,8 @@ func isRetryableError(errors []struct {
return false
}
var (
// Shared HTTP transport with optimized settings
defaultTransport = &http.Transport{
MaxIdleConns: 100,
MaxIdleConnsPerHost: 100,
IdleConnTimeout: 90 * time.Second,
DisableCompression: true, // Disable automatic request compression to prevent "trailing garbage" errors
ForceAttemptHTTP2: true,
TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, // Skip TLS verification for test environments
}
// Shared HTTP client with timeouts
defaultClient = &http.Client{
Transport: defaultTransport,
Timeout: 30 * time.Second,
}
)
// 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
@@ -173,12 +157,16 @@ func (qe *QueryExecutor) executeQuery() ([]byte, error) {
},
})
// Use default client if custom client is not set
client := qe.client
if client == nil {
client = defaultClient
// 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
}
+5 -4
View File
@@ -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) {
+31 -25
View File
@@ -11,60 +11,66 @@ import (
)
func (b *BaseClient) createHttpClient() (http_client *http.Client) {
// 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
}
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: 30 * time.Second,
ResponseHeaderTimeout: 10 * time.Second,
DisableKeepAlives: false,
DisableCompression: true, // Disable automatic compression to prevent trailing garbage errors
WriteBufferSize: 4096,
ReadBufferSize: 4096,
}
http_client = &http.Client{
Timeout: 30 * time.Second, // Increased for better reliability
Timeout: 30 * 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(b.endpoint, "https://") {
tlsClientConfig := &tls.Config{}
if strings.HasPrefix(b.endpoint, "https://") {
tlsClientConfig.InsecureSkipVerify = true
// 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: 30 * time.Second,
PingTimeout: 10 * time.Second,
WriteByteTimeout: 10 * time.Second,
DisableCompression: true, // Disable automatic compression to prevent trailing garbage errors
}
http_client = &http.Client{
Timeout: 30 * time.Second, // Increased for better reliability
Timeout: 30 * 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 nil
}
return http_client
}
+5 -3
View File
@@ -8,6 +8,8 @@ import (
"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()
@@ -18,7 +20,7 @@ func (suite *Tests) TestBaseClient_createHttpClient() {
assert.Equal(30*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)
@@ -92,14 +94,14 @@ func (suite *Tests) TestBaseClient_createHttpClient() {
}
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 := 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)
+13 -6
View File
@@ -11,6 +11,7 @@ import (
assertions "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
"golang.org/x/net/http2"
)
type RequestCompressionTestSuite struct {
@@ -74,17 +75,23 @@ func (suite *RequestCompressionTestSuite) TestRequestCompression() {
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{