fix(pricing): fallback to direct fetch after proxy transport errors
This commit is contained in:
@@ -8,6 +8,7 @@ import (
|
||||
"html"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"regexp"
|
||||
"strings"
|
||||
@@ -343,7 +344,18 @@ func fetchRawPricingPageOnce(url string, client *http.Client, opts officialPrici
|
||||
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return "", isRetriablePricingFetchError(err), fmt.Errorf("fetch %s: %w", url, err)
|
||||
if fallbackClient, ok := pricingFetchDirectFallbackClient(client, url); ok {
|
||||
fallbackResp, fallbackErr := fallbackClient.Do(req.Clone(req.Context()))
|
||||
if fallbackErr == nil {
|
||||
resp = fallbackResp
|
||||
err = nil
|
||||
} else {
|
||||
err = fallbackErr
|
||||
}
|
||||
}
|
||||
if err != nil {
|
||||
return "", isRetriablePricingFetchError(err), fmt.Errorf("fetch %s: %w", url, err)
|
||||
}
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
@@ -385,6 +397,34 @@ func isRetriablePricingFetchError(err error) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func pricingFetchDirectFallbackClient(baseClient *http.Client, rawURL string) (*http.Client, bool) {
|
||||
if baseClient == nil || !pricingFetchProxyConfigured() {
|
||||
return nil, false
|
||||
}
|
||||
parsed, err := url.Parse(rawURL)
|
||||
if err != nil {
|
||||
return nil, false
|
||||
}
|
||||
if parsed.Scheme != "http" && parsed.Scheme != "https" {
|
||||
return nil, false
|
||||
}
|
||||
transport := http.DefaultTransport.(*http.Transport).Clone()
|
||||
if custom, ok := baseClient.Transport.(*http.Transport); ok && custom != nil {
|
||||
transport = custom.Clone()
|
||||
}
|
||||
transport.Proxy = nil
|
||||
return &http.Client{Timeout: baseClient.Timeout, Transport: transport}, true
|
||||
}
|
||||
|
||||
func pricingFetchProxyConfigured() bool {
|
||||
for _, key := range []string{"HTTP_PROXY", "HTTPS_PROXY", "ALL_PROXY", "http_proxy", "https_proxy", "all_proxy"} {
|
||||
if strings.TrimSpace(os.Getenv(key)) != "" {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func cleanHTMLText(raw string) string {
|
||||
tagPattern := regexp.MustCompile(`(?is)<[^>]+>`)
|
||||
spacePattern := regexp.MustCompile(`[ \t]+`)
|
||||
|
||||
Reference in New Issue
Block a user