mirror of
https://github.com/lukaszraczylo/go-simple-graphql.git
synced 2026-10-14 00:58:32 +00:00
fixup! Add support of h2c over cleartext http
This commit is contained in:
@@ -11,42 +11,58 @@ 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: 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,
|
||||
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: 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,
|
||||
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 {
|
||||
|
||||
Reference in New Issue
Block a user