Files
lijiaoqiao/gateway
Your Name 687c4535f8 fix: P0-1 RateLimiter并发写安全 + P0-2工单操作错误码区分 + P1 rows.Close修复
P0-1 (limits.go): Allow()方法改为全程使用写锁保护counters map读写,避免RLock写入时的data race
P0-2 (ticket_workflow.go+ticket_handler.go): Assign/Resolve/Close操作先查询ticket存在性和状态,返回明确的CS_TICKET_4001/CS_TKT_4002/CS_TICKET_4092/CS_TICKET_4093错误码,handler根据错误前缀路由HTTP状态码
P1-1 (ticket_store.go): 移除GetStats中3处手动rows.Close(),只保留defer Close()
2026-05-01 20:56:25 +08:00
..

Gateway

OpenAI 兼容入口网关,负责接入、鉴权、限流、上游路由与基础审计。

当前真实状态

  • 服务入口是 cmd/gateway/main.go
  • 当前对外暴露的主要接口是 /v1/chat/completions/v1/completions/v1/models,以及对应的 /api/v1/* 兼容路径。
  • /v1/models 现在返回当前已注册 provider 的模型并集,按模型 ID 去重后排序,不再返回硬编码静态列表。
  • 鉴权运行时支持两种模式:
    • inmemory
    • remote_introspection
  • inmemory 只允许用于 dev 本地联调;非 dev 环境必须使用 remote_introspection,不得在 gateway 本地承载 token authority。
  • provider 注册已经从配置装配;如果未显式配置 provider启动时会基于环境变量生成默认 OpenAI provider。
  • 审计发射器支持 PostgreSQL 与内存实现;数据库未配置时会显式回退到内存实现。
  • 生产环境必须显式提供 PASSWORD_ENCRYPTION_KEYGATEWAY_CORS_ALLOW_ORIGINS;缺省密钥和 * CORS 只允许在开发态使用。

与其他服务的边界

  • gateway 不签发业务 token。
  • gatewayremote_introspection 模式下依赖 platform-token-runtime 提供 token introspection。
  • gateway 只承载入口控制,不承载 supply-apiplatform-token-runtime 的业务逻辑。

本地运行

cd "/home/long/project/立交桥/gateway"
export OPENAI_API_KEY="..."
export OPENAI_BASE_URL="https://api.openai.com"
export OPENAI_MODELS="gpt-4,gpt-3.5-turbo"
export GATEWAY_ENV="dev"
export GATEWAY_TOKEN_RUNTIME_MODE="inmemory"
go run ./cmd/gateway

如果要切到远程 token 校验:

export GATEWAY_TOKEN_RUNTIME_MODE="remote_introspection"
export GATEWAY_TOKEN_RUNTIME_URL="http://127.0.0.1:18081"

dev 环境约束:

  1. GATEWAY_ENV 只要不是 devGATEWAY_TOKEN_RUNTIME_MODE 就必须设置为 remote_introspection
  2. stagingproduction 和其他共享环境不得使用 inmemory
  3. token 生命周期、状态解释和 introspection 字段以 platform-token-runtime 为单一真源。

默认监听 0.0.0.0:8080

生产环境额外要求:

export GATEWAY_ENV="production"
export PASSWORD_ENCRYPTION_KEY="32-byte-production-secret........"
export GATEWAY_CORS_ALLOW_ORIGINS="https://console.example.com,https://app.example.com"

验证命令

模块级验证:

cd "/home/long/project/立交桥/gateway"
go test -count=1 ./...

仓库级统一验证:

cd "/home/long/project/立交桥"
bash scripts/ci/repo_integrity_check.sh

关键目录

  • internal/config/:环境变量配置与 provider 配置加载。
  • internal/handler/OpenAI 兼容 HTTP handler。
  • internal/middleware/鉴权、CORS、远程 introspection。
  • internal/router/provider 路由、打分与 fallback。

路由策略说明

  • 主启动链路当前只接入 latencyround_robinweightedavailability 四种策略。
  • internal/router/strategy/cost_based.gointernal/router/strategy/cost_aware.gointernal/router/fallback/ 仍属于实验性模块,没有接入 BuildServer 的运行时装配。
  • 如果配置里填入未接入的策略名,启动链路会回退到 latency