Add an option for disabling HTTP/2 when communicating with Cody Gateway (#59668)

* Add an option that can be used to disable HTTP2 for CG communication

* Revert old test code
This commit is contained in:
Rafał Gajdulewicz 2024-01-17 15:36:40 +01:00 committed by GitHub
parent 5725b810b8
commit b73e246f0b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 28 additions and 1 deletions

View File

@ -18,3 +18,9 @@
:3081 {
reverse_proxy 127.0.0.1:3082
}
# Caddy (:3083) -> Cody Gateway (:9992)
{$SOURCEGRAPH_HTTPS_DOMAIN}:3083 {
tls internal
reverse_proxy 127.0.0.1:9992
}

View File

@ -39,7 +39,7 @@ func getBasic(endpoint string, provider conftypes.CompletionsProviderName, acces
case conftypes.CompletionsProviderNameAzureOpenAI:
return azureopenai.NewClient(azureopenai.GetAPIClient, endpoint, accessToken)
case conftypes.CompletionsProviderNameSourcegraph:
return codygateway.NewClient(httpcli.UncachedExternalDoer, endpoint, accessToken)
return codygateway.NewClient(httpcli.CodyGatewayDoer, endpoint, accessToken)
case conftypes.CompletionsProviderNameFireworks:
return fireworks.NewClient(httpcli.UncachedExternalDoer, endpoint, accessToken), nil
case conftypes.CompletionsProviderNameAWSBedrock:

View File

@ -111,6 +111,7 @@ var (
externalRetryDelayMax, _ = time.ParseDuration(env.Get("SRC_HTTP_CLI_EXTERNAL_RETRY_DELAY_MAX", "3s", "Max retry delay duration for external HTTP requests"))
externalRetryMaxAttempts, _ = strconv.Atoi(env.Get("SRC_HTTP_CLI_EXTERNAL_RETRY_MAX_ATTEMPTS", "20", "Max retry attempts for external HTTP requests"))
externalRetryAfterMaxDuration, _ = time.ParseDuration(env.Get("SRC_HTTP_CLI_EXTERNAL_RETRY_AFTER_MAX_DURATION", "3s", "Max duration to wait in retry-after header before we won't auto-retry"))
codyGatewayDisableHTTP2 = env.MustGetBool("SRC_HTTP_CLI_DISABLE_CODY_GATEWAY_HTTP2", false, "Whether we should disable HTTP2 for Cody Gateway communication")
)
// NewExternalClientFactory returns a httpcli.Factory with common options
@ -178,6 +179,10 @@ var ExternalDoer, _ = ExternalClientFactory.Doer()
// This client does not cache responses. To cache responses see ExternalDoer instead.
var UncachedExternalDoer, _ = UncachedExternalClientFactory.Doer()
// CodyGatewayDoer is a client for communication with Cody Gateway.
// This client does not cache responses.
var CodyGatewayDoer, _ = UncachedExternalClientFactory.Doer(NewDisableHTTP2Opt(codyGatewayDisableHTTP2))
// TestExternalClientFactory is a httpcli.Factory with common options
// and is created for tests where you'd normally use an ExternalClientFactory.
// Must be used in tests only as it doesn't apply any IP restrictions.
@ -827,6 +832,22 @@ func NewMaxIdleConnsPerHostOpt(max int) Opt {
}
}
// NewDisableHTTP2Opt returns an Opt that makes the http.Client use HTTP/1.1 (instead of defaulting to HTTP/2).
func NewDisableHTTP2Opt(disable bool) Opt {
return func(cli *http.Client) error {
tr, err := getTransportForMutation(cli)
if err != nil {
return errors.Wrap(err, "httpcli.NewDisableHTTP2Opt")
}
if disable {
tr.ForceAttemptHTTP2 = false
tr.TLSNextProto = make(map[string]func(authority string, c *tls.Conn) http.RoundTripper)
tr.TLSClientConfig = &tls.Config{}
}
return nil
}
}
// NewTimeoutOpt returns a Opt that sets the Timeout field of an http.Client.
func NewTimeoutOpt(timeout time.Duration) Opt {
return func(cli *http.Client) error {