删除请求超时

This commit is contained in:
Sakurasan
2026-09-02 13:08:32 +08:00
parent 6d951a0d61
commit 923ac79039
5 changed files with 20 additions and 26 deletions
+1 -1
View File
@@ -27,7 +27,7 @@ OT_MASTER_KEY=change-me-master-key
OT_PROXY_UPSTREAM_KEY= OT_PROXY_UPSTREAM_KEY=
OT_PROXY_UPSTREAM_BASE_URL=https://api.openai.com OT_PROXY_UPSTREAM_BASE_URL=https://api.openai.com
OT_PROXY_DEFAULT_MODEL=gpt-4o-mini OT_PROXY_DEFAULT_MODEL=gpt-4o-mini
OT_PROXY_TIMEOUT=120s OT_PROXY_TIMEOUT=300s
# 渠道健康检查 # 渠道健康检查
OT_PROXY_HEALTH_INTERVAL=60s OT_PROXY_HEALTH_INTERVAL=60s
+1 -1
View File
@@ -31,7 +31,7 @@ func serve() error {
} }
defer a.Shutdown(context.Background()) defer a.Shutdown(context.Background())
gw := proxy.NewGateway(a.DB, a.Enc, a.Usage, a.Limit, cfg.RateLimit.UserRPS, cfg.Proxy.LogRaw) gw := proxy.NewGateway(a.DB, a.Enc, a.Usage, a.Limit, cfg.RateLimit.UserRPS, cfg.Proxy.LogRaw, cfg.Proxy.Timeout)
router := api.NewRouter(a, gw) router := api.NewRouter(a, gw)
srv := &http.Server{ srv := &http.Server{
+1 -1
View File
@@ -127,7 +127,7 @@ func Load() (*Config, error) {
v.SetDefault("proxy.upstream_base_url", "https://api.openai.com") v.SetDefault("proxy.upstream_base_url", "https://api.openai.com")
v.SetDefault("proxy.upstream_key", "") v.SetDefault("proxy.upstream_key", "")
v.SetDefault("proxy.default_model", "gpt-4o-mini") v.SetDefault("proxy.default_model", "gpt-4o-mini")
v.SetDefault("proxy.timeout", "120s") v.SetDefault("proxy.timeout", "300s")
v.SetDefault("proxy.health_interval", "60s") v.SetDefault("proxy.health_interval", "60s")
v.SetDefault("proxy.health_fail_threshold", 2) v.SetDefault("proxy.health_fail_threshold", 2)
v.SetDefault("proxy.log_raw", false) v.SetDefault("proxy.log_raw", false)
+2 -2
View File
@@ -109,7 +109,7 @@ func contains(list []string, s string) bool {
return false return false
} }
func NewGateway(db *gorm.DB, enc *crypto.Encryptor, rec *usage.Recorder, lim *ratelimit.Limiter, userRPS int, logRaw bool) *Gateway { func NewGateway(db *gorm.DB, enc *crypto.Encryptor, rec *usage.Recorder, lim *ratelimit.Limiter, userRPS int, logRaw bool, gw_timeout time.Duration) *Gateway {
return &Gateway{ return &Gateway{
db: db, db: db,
ch: channel.NewService(db, enc), ch: channel.NewService(db, enc),
@@ -118,7 +118,7 @@ func NewGateway(db *gorm.DB, enc *crypto.Encryptor, rec *usage.Recorder, lim *ra
lim: lim, lim: lim,
userRPS: userRPS, userRPS: userRPS,
logRaw: logRaw, logRaw: logRaw,
hc: &http.Client{Timeout: 120 * time.Second}, hc: &http.Client{Timeout: gw_timeout * time.Second},
} }
} }
+1 -7
View File
@@ -3,7 +3,6 @@ package proxy
import ( import (
"bufio" "bufio"
"bytes" "bytes"
"context"
"crypto/rand" "crypto/rand"
"encoding/hex" "encoding/hex"
"encoding/json" "encoding/json"
@@ -218,9 +217,7 @@ func (g *Gateway) proxyOne(c *gin.Context, ch *store.Channel, plan *upstreamPlan
} }
} }
ctx, cancel := context.WithTimeout(c.Request.Context(), time.Duration(ch.TimeoutMS)*time.Millisecond) req, err := http.NewRequest(http.MethodPost, upstreamURL(ch, plan.proto, plan.path), bytes.NewReader(upBody))
defer cancel()
req, err := http.NewRequestWithContext(ctx, http.MethodPost, upstreamURL(ch, plan.proto, plan.path), bytes.NewReader(upBody))
if err != nil { if err != nil {
return false, false, http.StatusInternalServerError, []byte("failed to build upstream request") return false, false, http.StatusInternalServerError, []byte("failed to build upstream request")
} }
@@ -242,9 +239,6 @@ func (g *Gateway) proxyOne(c *gin.Context, ch *store.Channel, plan *upstreamPlan
start := time.Now() start := time.Now()
resp, err := g.hc.Do(req) resp, err := g.hc.Do(req)
if err != nil { if err != nil {
if ctx.Err() == context.DeadlineExceeded {
return false, true, http.StatusGatewayTimeout, []byte("upstream request timed out")
}
return false, true, http.StatusBadGateway, []byte("upstream request failed: " + err.Error()) return false, true, http.StatusBadGateway, []byte("upstream request failed: " + err.Error())
} }
defer resp.Body.Close() defer resp.Body.Close()