Phase 3-A 完整实现,包含: Gateway (lijiaoqiao/gateway): - RemoteTokenRuntime 缓存实现: active=30s/expired=2m/revoked=10m TTL淘汰 - LRU 容量淘汰 (max_entries=10000,插入顺序淘汰) - HTTPTimeoutConfig: 4个环境变量 (Dial/KeepAlive/Read/Write/MaxIdle) - 缓存命中率指标: GetCacheHitRate() + 实例级别统计 - 上游延迟指标: RecordTokenRuntime() histogram - buildTimeoutClient: 基于 HTTPTimeoutConfig 的 HTTP 客户端工厂 - 新增测试: 22个矩阵测试 (remote_runtime_matrix_test.go, config_test.go) Platform Token Runtime (lijiaoqiao/platform-token-runtime): - metrics/metrics.go: GetCacheHitRate() 方法 - inmemory_runtime.go: GetCacheHitRate() 实现 变更文件 (8 modified + 5 new): - gateway/internal/middleware/remote_runtime.go # 核心缓存实现 - gateway/internal/middleware/remote_runtime_test.go - gateway/internal/middleware/remote_runtime_cache_test.go - gateway/internal/middleware/remote_runtime_matrix_test.go - gateway/internal/middleware/remote_runtime_metrics_test.go - gateway/internal/metrics/metrics.go # 新增 - gateway/internal/config/config.go # HTTPTimeoutConfig - gateway/internal/config/config_test.go - gateway/internal/app/bootstrap.go # 初始化顺序 - gateway/internal/router/router.go # 指标注入 - platform-token-runtime/internal/metrics/metrics.go # 新增 - platform-token-runtime/internal/app/bootstrap.go - platform-token-runtime/internal/auth/service/inmemory_runtime.go
Gateway
OpenAI 兼容入口网关,负责接入、鉴权、限流、上游路由与基础审计。
当前真实状态
- 服务入口是
cmd/gateway/main.go。 - 当前对外暴露的主要接口是
/v1/chat/completions、/v1/completions、/v1/models,以及对应的/api/v1/*兼容路径。 /v1/models现在返回当前已注册 provider 的模型并集,按模型 ID 去重后排序,不再返回硬编码静态列表。- 鉴权运行时支持两种模式:
inmemoryremote_introspection
inmemory只允许用于dev本地联调;非dev环境必须使用remote_introspection,不得在gateway本地承载 token authority。- provider 注册已经从配置装配;如果未显式配置 provider,启动时会基于环境变量生成默认 OpenAI provider。
- 审计发射器支持 PostgreSQL 与内存实现;数据库未配置时会显式回退到内存实现。
- 生产环境必须显式提供
PASSWORD_ENCRYPTION_KEY与GATEWAY_CORS_ALLOW_ORIGINS;缺省密钥和*CORS 只允许在开发态使用。
与其他服务的边界
gateway不签发业务 token。gateway在remote_introspection模式下依赖platform-token-runtime提供 token introspection。gateway只承载入口控制,不承载supply-api或platform-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 环境约束:
GATEWAY_ENV只要不是dev,GATEWAY_TOKEN_RUNTIME_MODE就必须设置为remote_introspection。staging、production和其他共享环境不得使用inmemory。- 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。
路由策略说明
- 主启动链路当前只接入
latency、round_robin、weighted、availability四种策略。 internal/router/strategy/cost_based.go、internal/router/strategy/cost_aware.go与internal/router/fallback/仍属于实验性模块,没有接入BuildServer的运行时装配。- 如果配置里填入未接入的策略名,启动链路会回退到
latency。