mirror of
https://github.com/Wei-Shaw/sub2api.git
synced 2026-12-06 08:06:50 +00:00
Root cause: - The OpenAI proxy stream circuit was keyed only on proxy_id. One account whose Responses SSE streams kept disconnecting quarantined every healthy account sharing that egress proxy, emptying the pool for 10 minutes (#5010). - Every 'no available accounts' response called DiagnoseModelAvailabilityForPlatform, which ran a candidates query per request; retrying clients turned that into a database query storm. Change: - Key the circuit on (account_id, proxy_id) and pass the owning account at every record/clear/check call site. The failure threshold (2 in 60s) and 10-minute quarantine TTL are unchanged, as is fail-open selection. - Add a bounded in-process TTL (5s) cache with singleflight for the model availability diagnosis on both gateway services. Keys include platform, group, model and, for the context-sensitive generic check, request-scoped composite-route/thinking inputs. Lookup failures are not cached so callers keep the conservative 503 fallback and retry the database. - Update the circuit config doc comment and the scheduler tests that relied on proxy-wide quarantine. Tests: - go test ./internal/service -run 'TestOpenAIProxyStream' -count=1 - go test -tags=unit ./internal/service -run 'TestDiagnoseModelAvailabilityForPlatform|TestOpenAIDiagnoseModelAvailability|TestGatewayDiagnoseModelAvailability|TestModelAvailabilityDiagnosisCache|TestOpenAIProxyStream|TestOpenAIGatewayService_SelectAccountWithScheduler_(SkipsQuarantinedSharedProxy|QuarantinedAccountKeepsPeerOnSharedProxy|FailsOpenWhenAllProxiesQuarantined)|TestSelectAccountWithSchedulerForImages_UnspecifiedSizePrefersHealthyProxyOverQuarantinedUnrestricted' -count=1 - go test -tags=unit ./internal/handler -run 'TestClassifyNoAccount|TestClassifySelectionFailure|TestGPT55Retirement' -count=1 - go build ./... and gofmt -l are clean.