fix(pricing): fallback to direct fetch after proxy transport errors
Some checks failed
CI / go-test (push) Has been cancelled
CI / scripts-regression (push) Has been cancelled
CI / frontend-build (push) Has been cancelled
CI / docker-build (push) Has been cancelled

This commit is contained in:
phamnazage-jpg
2026-05-24 19:04:17 +08:00
parent 5cb551de68
commit 2a64160a2b
2 changed files with 83 additions and 1 deletions

View File

@@ -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]+`)