图像生成上游同步响应可能超过 120 秒,原共享 HTTP 客户端会把仍在处理的提交标记为结果不确定。将客户端超时调整为 10 分钟,并增加回归测试;任务执行仍由现有租约、任务超时和取消机制约束。\n\n验证:\n- env -u AI_GATEWAY_TEST_DATABASE_URL go test ./internal/runner -run 'TestProviderHTTPClientTimeoutAllowsLongRunningMediaRequests|TestPlatformProxyMode' -count=1\n- env -u AI_GATEWAY_TEST_DATABASE_URL go test ./... -count=1
76 lines
2.0 KiB
Go
76 lines
2.0 KiB
Go
package runner
|
|
|
|
import (
|
|
"net/http"
|
|
"net/url"
|
|
"strings"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/easyai/easyai-ai-gateway/apps/api/internal/clients"
|
|
"github.com/easyai/easyai-ai-gateway/apps/api/internal/netproxy"
|
|
"github.com/easyai/easyai-ai-gateway/apps/api/internal/store"
|
|
)
|
|
|
|
type httpClientCache struct {
|
|
none *http.Client
|
|
global *http.Client
|
|
mu sync.Mutex
|
|
custom map[string]*http.Client
|
|
}
|
|
|
|
const providerHTTPClientTimeout = 10 * time.Minute
|
|
|
|
func newHTTPClientCache() *httpClientCache {
|
|
return &httpClientCache{
|
|
none: newHTTPClient(nil),
|
|
global: newHTTPClient(http.ProxyFromEnvironment),
|
|
custom: map[string]*http.Client{},
|
|
}
|
|
}
|
|
|
|
func (s *Service) httpClientForCandidate(candidate store.RuntimeModelCandidate, simulated bool) (*http.Client, error) {
|
|
if simulated {
|
|
return s.httpClients.none, nil
|
|
}
|
|
config, err := netproxy.Normalize(netproxy.FromPlatformConfig(candidate.PlatformConfig))
|
|
if err != nil {
|
|
return nil, &clients.ClientError{Code: "invalid_proxy", Message: err.Error(), Retryable: false}
|
|
}
|
|
switch config.Mode {
|
|
case netproxy.ModeGlobal:
|
|
if strings.TrimSpace(s.cfg.GlobalHTTPProxy) != "" {
|
|
return s.httpClients.customClient(s.cfg.GlobalHTTPProxy)
|
|
}
|
|
return s.httpClients.global, nil
|
|
case netproxy.ModeCustom:
|
|
return s.httpClients.customClient(config.HTTPProxy)
|
|
default:
|
|
return s.httpClients.none, nil
|
|
}
|
|
}
|
|
|
|
func (c *httpClientCache) customClient(rawProxy string) (*http.Client, error) {
|
|
normalized, proxyURL, err := netproxy.ParseHTTPProxy(rawProxy)
|
|
if err != nil {
|
|
return nil, &clients.ClientError{Code: "invalid_proxy", Message: err.Error(), Retryable: false}
|
|
}
|
|
c.mu.Lock()
|
|
defer c.mu.Unlock()
|
|
if client := c.custom[normalized]; client != nil {
|
|
return client, nil
|
|
}
|
|
client := newHTTPClient(http.ProxyURL(proxyURL))
|
|
c.custom[normalized] = client
|
|
return client, nil
|
|
}
|
|
|
|
func newHTTPClient(proxy func(*http.Request) (*url.URL, error)) *http.Client {
|
|
transport := http.DefaultTransport.(*http.Transport).Clone()
|
|
transport.Proxy = proxy
|
|
return &http.Client{
|
|
Timeout: providerHTTPClientTimeout,
|
|
Transport: transport,
|
|
}
|
|
}
|