Files
opencode2api/internal/gateway/refresh.go
T
Jason Xu f0f064f31d fix: align the v2 free-tier shape with OpenCode 2.0.32
- Use the genuine patchText description and keep the Linux/bash shell sentence.
- Preserve the client's child session id separately from the affinity.
- Scope the anonymous parent session id like every other derived identity.
- Append beta=true only on v2 Anthropic lanes and parse the query safely.
- Remove the redundant v1 project renderer and duplicate anthropic-version.
- Revert the formatting-only churn in opencode_tools.json and update lane docs.
2026-10-20 18:13:56 +08:00

318 lines
12 KiB
Go

package gateway
import (
"context"
"errors"
"net/http"
"sync"
"time"
"opencode2api/internal/config"
modelcatalog "opencode2api/internal/models"
)
const (
proxyHealthCheckURL = "https://cloudflare.com/cdn-cgi/trace"
proxyHealthCheckInterval = 15 * time.Minute
proxyHealthCheckTimeout = 10 * time.Second
)
// syncProxyResult updates proxy health from real traffic and reports whether
// it treated the outcome as a proxy-route failure; nil and #pool proxies are
// skipped and report false. Only timeouts and connection refusals mark a
// proxy unavailable: its keys are rebound elsewhere at once and a confirming
// health check starts in the background. Any upstream HTTP status (including
// one returned alongside a status error) proves the proxy route is reachable
// and restores a proxy evicted by an earlier transient failure. Errors without
// an HTTP response only start the background check, which never affects the
// return value.
func (g *Gateway) syncProxyResult(ctx context.Context, proxy *proxyTransport, status int, err error) bool {
if proxy == nil || proxy.pool {
return false
}
if status > 0 {
wasHealthy := proxy.healthy.Swap(true)
if !wasHealthy {
g.restoreProxy(proxy)
}
return false
}
if isProxyFailure(err) {
g.rebindFailedProxy(proxy)
g.verifyProxyAfterError(ctx, proxy, "proxy transport error", status)
return true
}
if err != nil {
g.verifyProxyAfterError(ctx, proxy, "upstream request error", status)
return false
}
return false
}
// verifyProxyAfterError starts a background health check of proxy, unless one
// is already in flight, and applies its result. source and status only label
// the log; the source distinguishes a missing HTTP response from another
// upstream request error.
func (g *Gateway) verifyProxyAfterError(ctx context.Context, proxy *proxyTransport, source string, status int) {
if !proxy.checking.CompareAndSwap(false, true) {
return
}
// The client request may finish or be canceled while the verification is
// running. Keep its values but give the proxy check an independent timeout.
checkCtx := context.WithoutCancel(ctx)
go func() {
result := g.transports.checkClaimedProxy(checkCtx, proxy, proxyHealthCheckURL, proxyHealthCheckTimeout)
g.applyProxyHealthResult(result, source, status)
}()
}
// rebindFailedProxy marks proxy unhealthy and moves the Zen and Go keys bound
// to it to other proxies (see rebindUnavailableProxy).
func (g *Gateway) rebindFailedProxy(proxy *proxyTransport) (zenMoved, goMoved int) {
if proxy == nil {
return 0, 0
}
wasHealthy := proxy.healthy.Swap(false)
return g.rebindUnavailableProxy(proxy, wasHealthy)
}
// rebindUnavailableProxy moves the Zen and Go keys bound to proxy to other
// proxies and logs the eviction when the proxy was healthy before or any key
// moved. It does not touch the healthy flag, and anonymous nodes are never
// rebound.
func (g *Gateway) rebindUnavailableProxy(proxy *proxyTransport, wasHealthy bool) (zenMoved, goMoved int) {
zenMoved = g.zenNodes.RebindProxy(proxy.index)
goMoved = g.goNodes.RebindProxy(proxy.index)
if wasHealthy || zenMoved+goMoved > 0 {
g.logger.Warn("proxy became unavailable", "component", "proxy", "event", "proxy_unavailable", "proxy", config.RedactURL(proxy.name), "zen_keys_moved", zenMoved, "go_keys_moved", goMoved)
}
return zenMoved, goMoved
}
// restoreProxy moves keys back to a recovered proxy they were originally
// assigned to and clears the anonymous cooldowns bound to it. It does not set
// the healthy flag; callers do that first.
func (g *Gateway) restoreProxy(proxy *proxyTransport) (zenMoved, goMoved int) {
if proxy == nil {
return 0, 0
}
zenMoved = g.zenNodes.RestoreProxy(proxy.index)
goMoved = g.goNodes.RestoreProxy(proxy.index)
// The anonymous lane carries its own per-proxy cooldowns; key recovery
// alone would leave it skipped after an outage-era failure.
anonCleared := g.anonymous.RestoreProxy(proxy)
if zenMoved+goMoved > 0 || anonCleared > 0 {
g.logger.Info("proxy connectivity restored", "component", "proxy", "event", "proxy_restored", "proxy", config.RedactURL(proxy.name), "zen_keys_moved", zenMoved, "go_keys_moved", goMoved, "anonymous_cleared", anonCleared)
}
return zenMoved, goMoved
}
// StartProxyHealthChecks rechecks unhealthy proxies every
// proxyHealthCheckInterval until ctx is canceled. There is no check at
// startup, since every proxy starts healthy.
func (g *Gateway) StartProxyHealthChecks(ctx context.Context) {
check := func() {
results := g.transports.CheckHealth(ctx, proxyHealthCheckURL, proxyHealthCheckTimeout)
for _, result := range results {
g.applyProxyHealthResult(result, "scheduled health check", 0)
}
}
go func() {
ticker := time.NewTicker(proxyHealthCheckInterval)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
return
case <-ticker.C:
check()
}
}
}()
}
// applyProxyHealthResult acts on one health check: a pass restores a proxy
// that was unhealthy, a proxy-route failure rebinds its keys only while some
// proxy is still healthy, and an inconclusive error changes nothing. source
// and upstreamStatus only label the log.
func (g *Gateway) applyProxyHealthResult(result proxyHealthResult, source string, upstreamStatus int) {
if result.err == nil {
if !result.wasHealthy {
g.restoreProxy(result.proxy)
}
g.logger.Debug("proxy health check passed", "component", "proxy", "event", "health_check_passed", "source", source, "upstream_status", upstreamStatus, "proxy", config.RedactURL(result.proxy.name))
return
}
if !result.failed {
g.logger.Debug("proxy health check was inconclusive", "component", "proxy", "event", "health_check_inconclusive", "source", source, "upstream_status", upstreamStatus, "proxy", config.RedactURL(result.proxy.name), "error", result.err)
return
}
if g.transports.hasHealthy() {
zenMoved, goMoved := g.rebindUnavailableProxy(result.proxy, result.wasHealthy)
if result.wasHealthy || zenMoved+goMoved > 0 {
g.logger.Warn("proxy health check failed", "component", "proxy", "event", "health_check_failed", "source", source, "upstream_status", upstreamStatus, "proxy", config.RedactURL(result.proxy.name), "zen_keys_moved", zenMoved, "go_keys_moved", goMoved, "error", result.err)
return
}
}
g.logger.Debug("proxy health check is still failing", "component", "proxy", "event", "health_check_still_failing", "source", source, "upstream_status", upstreamStatus, "proxy", config.RedactURL(result.proxy.name), "error", result.err)
}
// StartModelRefresh refreshes the model catalog at once and then every
// models.refresh_seconds until ctx is canceled. Each round concurrently lists
// the Zen models (through the anonymous lane when no Zen key can) and the Go
// models, and fetches OpenCode's protocol capabilities. The catalog is
// replaced only when at least one listing succeeded; the rotation groups are
// then resynced and the catalog cache saved.
func (g *Gateway) StartModelRefresh(ctx context.Context) {
refresh := func() {
var zen, goModels []string
var capabilities modelcatalog.Capabilities
var capabilitiesErr error
var wg sync.WaitGroup
wg.Add(3)
go func() { defer wg.Done(); zen = g.refreshZen(ctx) }()
go func() { defer wg.Done(); goModels = g.refreshTier(ctx, g.cfg.Upstream.Go, g.goNodes) }()
go func() {
defer wg.Done()
capabilityCtx, cancel := context.WithTimeout(ctx, 30*time.Second)
defer cancel()
capabilities, capabilitiesErr = g.refreshProtocolCapabilities(capabilityCtx)
}()
wg.Wait()
if ctx.Err() != nil {
return
}
if capabilitiesErr != nil {
g.logger.Warn("OpenCode capability catalog refresh failed", "component", "models", "event", "capability_refresh_failed", "error", capabilitiesErr)
}
if zen != nil || goModels != nil {
g.catalog.ReplaceWithCapabilities(zen, goModels, capabilities.Protocols, capabilities.Unsupported, capabilities.Metadata)
g.syncRotation()
if ctx.Err() == nil {
if err := g.catalog.SaveCache(); err != nil {
g.logger.Warn("model catalog cache write failed", "component", "models", "event", "catalog_cache_write_failed", "error", err)
}
}
g.logger.Info("model catalog refreshed", "component", "models", "event", "catalog_refreshed", "models", len(g.catalog.List()))
}
}
go func() {
refresh()
ticker := time.NewTicker(time.Duration(g.cfg.Models.RefreshSeconds) * time.Second)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
return
case <-ticker.C:
refresh()
}
}
}()
}
// refreshProtocolCapabilities fetches OpenCode's capability catalog through
// each healthy proxy in turn until one succeeds. Only a gateway without any
// transport falls back to a direct client.
func (g *Gateway) refreshProtocolCapabilities(ctx context.Context) (modelcatalog.Capabilities, error) {
if g.transports == nil || len(g.transports.items) == 0 {
return modelcatalog.FetchCapabilities(ctx, &http.Client{Timeout: 30 * time.Second}, modelcatalog.CapabilitiesURL)
}
var lastErr error
for _, proxy := range g.transports.items {
if proxy == nil || !proxy.healthy.Load() {
continue
}
capabilities, err := modelcatalog.FetchCapabilities(ctx, proxy.client, modelcatalog.CapabilitiesURL)
if err == nil {
return capabilities, nil
}
lastErr = err
}
if lastErr == nil {
lastErr = errors.New("no healthy proxy available for OpenCode capability catalog")
}
return modelcatalog.Capabilities{}, lastErr
}
// refreshZen lists the Zen models with the Zen keys and, when that fails and
// the anonymous tier is enabled, with the public credential instead.
func (g *Gateway) refreshZen(ctx context.Context) []string {
if models := g.refreshTier(ctx, g.cfg.Upstream.Zen, g.zenNodes); models != nil {
return models
}
if !g.cfg.Anonymous {
return nil
}
return g.refreshAnonymousTier(ctx, g.cfg.Upstream.Anonymous)
}
// refreshAnonymousTier lists models with the public credential, one anonymous
// node per attempt: one pass over the nodes, or up to retry.max_attempts
// attempts when #pool transports exist. base only labels the logs.
func (g *Gateway) refreshAnonymousTier(ctx context.Context, base string) []string {
cursor := g.anonymous.CursorFor("")
limit := g.anonymous.Len()
if g.anonymous.HasPool() && g.cfg.Retry.MaxAttempts > limit {
limit = g.cfg.Retry.MaxAttempts
}
for attempt := 1; attempt <= limit; attempt++ {
node := cursor.Next()
if node == nil {
break
}
refreshCtx, cancel := context.WithTimeout(ctx, 30*time.Second)
var models []string
var status int
var err error
// List models on the configured v2 /inference origin first, then fall
// back to the v1 origin (see anonymousUpstreams).
for _, upstream := range g.anonymousUpstreams() {
models, status, err = modelcatalog.FetchModels(refreshCtx, node.proxy.client, upstream, anonymousZenKey)
if err == nil || isProxyFailure(err) {
break
}
}
g.syncProxyResult(refreshCtx, node.proxy, status, err)
cancel()
if err == nil {
g.anonymous.MarkSuccess(node)
return models
}
g.anonymous.MarkFailure(node, nil, err)
g.logger.Debug("anonymous model catalog refresh attempt failed", "component", "models", "event", "anonymous_refresh_attempt_failed", "upstream", config.RedactURL(base), "attempt", attempt, "proxy", config.RedactURL(node.proxy.name), "error", err)
}
g.logger.Warn("anonymous model catalog refresh failed", "component", "models", "event", "anonymous_refresh_failed", "upstream", config.RedactURL(base))
return nil
}
// refreshTier lists the models at base with up to retry.max_attempts keys
// from nodes, updating proxy health and key cooldowns after every attempt. It
// returns nil when the pool has no keys or every attempt fails.
func (g *Gateway) refreshTier(ctx context.Context, base string, nodes *upstreamNodes) []string {
cursor := nodes.Cursor()
for attempt := 0; attempt < g.cfg.Retry.MaxAttempts; attempt++ {
node := cursor.Next()
if node == nil {
return nil
}
refreshCtx, cancel := context.WithTimeout(ctx, 30*time.Second)
proxy := nodes.Proxy(node)
if proxy == nil {
cancel()
return nil
}
models, status, err := modelcatalog.FetchModels(refreshCtx, proxy.client, base, node.key)
g.syncProxyResult(refreshCtx, proxy, status, err)
cancel()
if err == nil {
nodes.MarkSuccess(node)
return models
}
nodes.MarkFailure(node, nil, err)
g.logger.Debug("model catalog refresh attempt failed", "component", "models", "event", "refresh_attempt_failed", "upstream", config.RedactURL(base), "attempt", attempt+1, "error", err)
}
g.logger.Warn("model catalog refresh failed", "component", "models", "event", "refresh_failed", "upstream", config.RedactURL(base))
return nil
}