responses 直通超时可配,流式去总超时,代理补阶段超时
CI / test (push) Successful in 31s

This commit is contained in:
2026-07-16 21:20:02 +08:00
parent d56678e1de
commit cb66567256
13 changed files with 379 additions and 45 deletions
+29
View File
@@ -68,6 +68,9 @@ type AiGatewayService struct {
// grokWebSearch / grokXSearch 是 xai. 模型服务端搜索工具默认注入开关
grokWebSearch atomic.Bool
grokXSearch atomic.Bool
// upstreamWaitSec 是 responses 直通的上游无响应预算(秒):非流式为单次尝试
// 总超时,流式为等待响应头预算;multi-agent/搜索类模型远超 SDK 默认 60s
upstreamWaitSec atomic.Int64
}
// NewAiGatewayService 组装依赖;调用 StartCleanup 后开始调用日志周期清理。
@@ -78,6 +81,7 @@ func NewAiGatewayService(db *gorm.DB, configs *OciConfigService, client oci.Clie
s.streamGuardKB.Store(int64(loadIntSetting(db, settingAiStreamGuardKB, defaultStreamGuardKB)))
s.grokWebSearch.Store(loadBoolSetting(db, settingAiGrokWebSearch, true))
s.grokXSearch.Store(loadBoolSetting(db, settingAiGrokXSearch, true))
s.upstreamWaitSec.Store(int64(loadIntSetting(db, settingAiUpstreamWaitSec, defaultUpstreamWaitSec)))
return s
}
@@ -93,11 +97,17 @@ const (
// 开关,缺省开。
settingAiGrokWebSearch = "ai_grok_web_search"
settingAiGrokXSearch = "ai_grok_x_search"
// settingAiUpstreamWaitSec 是 responses 直通的上游无响应预算(秒)。
settingAiUpstreamWaitSec = "ai_upstream_wait_seconds"
)
// defaultStreamGuardKB 是保险丝阈值缺省值,低于实测断流边界留余量。
const defaultStreamGuardKB = 60
// defaultUpstreamWaitSec 是上游无响应预算缺省值(秒):multi-agent 非流式
// 实测 100~180s 才回响应头,给足余量;上下限见 SetUpstreamWait。
const defaultUpstreamWaitSec = 300
// loadBoolSetting 读 settings 表布尔键,无行或值非法时返回缺省。
func loadBoolSetting(db *gorm.DB, key string, def bool) bool {
var row model.Setting
@@ -165,6 +175,25 @@ func (s *AiGatewayService) SetStreamGuard(ctx context.Context, on bool, kb int)
return nil
}
// UpstreamWait 返回 responses 直通的上游无响应预算。
func (s *AiGatewayService) UpstreamWait() time.Duration {
return time.Duration(s.upstreamWaitSec.Load()) * time.Second
}
// SetUpstreamWait 持久化并即时生效上游无响应预算;sec 限定 30..900。
func (s *AiGatewayService) SetUpstreamWait(ctx context.Context, sec int) error {
if sec < 30 || sec > 900 {
return fmt.Errorf("上游无响应预算须在 30..900 秒, 收到 %d", sec)
}
err := s.db.WithContext(ctx).
Save(&model.Setting{Key: settingAiUpstreamWaitSec, Value: strconv.Itoa(sec)}).Error
if err != nil {
return fmt.Errorf("保存上游无响应预算: %w", err)
}
s.upstreamWaitSec.Store(int64(sec))
return nil
}
// GrokSearch 返回 grok 服务端搜索工具默认注入开关(web_search, x_search)。
func (s *AiGatewayService) GrokSearch() (bool, bool) {
return s.grokWebSearch.Load(), s.grokXSearch.Load()