mirror of
https://github.com/Wei-Shaw/sub2api.git
synced 2026-12-06 16:04:24 +00:00
fix(models): 模型列表刷新进入 singleflight 后复查缓存,避免并发冷启动重复请求上游
- 调用方读缓存未命中后才进入 singleflight;上一轮刷新若在两者之间完成并写入缓存, 后到的调用会再发起一次上游请求 - 刷新函数先复查缓存,已是新鲜结果则直接复用,只有缺失或过期时才请求上游 - 修复 TestPinnedOpenAIModelsListMixedAccountsShareColdCacheAcrossGroups 偶发上游调用 2 次 (-race -count=3000 -cpu 1,2,4:修复前 34/9000 失败,修复后 0)
This commit is contained in:
1 parent
ba81c7fd75
commit
b2b1bcf260
1 file changed
+5
-1
@@ -1890,9 +1890,13 @@ func (s *OpenAIGatewayService) fetchCachedOpenAIModels(ctx context.Context, requ
|
||||
|
||||
func (s *OpenAIGatewayService) refreshCachedOpenAIModels(cacheKey string, request openAIModelsRequest, fetch func(ctx context.Context, ifNoneMatch string) (*OpenAIModelsResponse, error)) <-chan singleflight.Result {
|
||||
return s.openAIModelsCache.refresh.DoChan(cacheKey, func() (any, error) {
|
||||
cached, state := s.openAIModelsCache.get(cacheKey, time.Now())
|
||||
// 调用方读缓存之后、进入 singleflight 之前,上一轮刷新可能已写入新鲜结果:直接复用,不重复请求上游。
|
||||
if state == openAIModelsCacheFresh {
|
||||
return cached, nil
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(context.Background(), codexModelsManifestRequestTimeout)
|
||||
defer cancel()
|
||||
cached, _ := s.openAIModelsCache.get(cacheKey, time.Now())
|
||||
ifNoneMatch := ""
|
||||
if cached != nil {
|
||||
ifNoneMatch = cached.upstreamETag
|
||||
|
||||
Reference in new issue
Block a user