Skip to content

feat(gateway): P0-B 网关转发 + 路由故障转移 + 计量账本闭环 (v0.2.1) - #73

Merged
argszero merged 1 commit into
mainfrom
feat/backend-p0b-gateway
Aug 18, 2026
Merged

argszero merged 1 commit into
mainfrom
feat/backend-p0b-gateway

Conversation

@argszero

Copy link
Copy Markdown
Owner

Summary

P0-B backend implementation (rant 2026-08-18T09:55:57 — 网关转发 + 路由故障转移 + 计量账本闭环). Completes the core loop: request in → route to key → forward upstream → meter points → write ledger per architecture v0.2 §4.1/4.2/4.3/4.4. Non-streaming only (SSE deferred to P0-C).

Changes

  • Gateway endpoints (src/gateway.rs):
    • POST /v1/chat/completions (OpenAI-compatible, Bearer atk_ API key auth)
    • POST /anthropic/v1/messages (Anthropic-compatible)
    • GET /api/models (marketplace list with key availability, auth required)
    • Upstream response passthrough (status + body); usage parsed per protocol (prompt/completion_tokens vs input/output_tokens)
  • Routing & failover (src/router.rs, §4.2.1): initial random pick from healthy keys (status='on', not in 5s cooldown), sticky reuse per user+model (in-memory HashMap), unhealthy marking on 401/403/429/5xx/network error, max 3 switches → 503 {"error":{"message":"该模型暂无可用 key"}}; other 4xx passthrough without switching
  • Metering & ledger (src/billing.rs, §4.3/4.4): cost = tokens × per-M price (currency converted to points.anchor_currency), points = cost × points_per_unit; consumer balance deducted (pre-check → 402 when ≤0), sharer earns 90% (10% platform cut); transactions consume+earn pair, usage_records, keys.used += tokens; settle is transactional — rolls back on failure (phantom key refused)
  • Models seeding (src/db.rs): startup upsert from data/models.example.json + config.price_overrides
  • axum macros feature enabled (debug_handler)

Tests

  • 33 tests pass: router (random/sticky/cooldown/3-switch limit), billing (points math, 90/10 split, 402, rollback), gateway (401 no bearer / 400 missing model / 402 insufficient balance / 503 dead-upstream failover / e2e fake-upstream success + ledger + sticky / models endpoint)
  • cargo build + cargo clippy --all-targets -- -D warnings + cargo fmt --check clean
  • Live smoke: no token → 401; demo key + placeholder upstream → 503 (failover path); GET /api/models returns list

Notes

  • Version bumped to v0.2.1
  • Upstream keys still plaintext placeholders (encryption deferred to P0-C with SSE streaming)

Closes rant 2026-08-18T09:55:57.804810 (P0-B).

- 网关端点:POST /v1/chat/completions(OpenAI 兼容)+ POST /anthropic/v1/messages
  + GET /api/models(市场页,需认证)
- 路由故障转移(src/router.rs,architecture §4.2.1):初始随机 / 粘性复用 /
  401/403/429/5xx 与网络错误冷却 5s / 上限 3 次切换 → 503
- 计量账本(src/billing.rs,§4.3/4.4):单价折算点数、消费者扣点、分享者 90%
  分成、transactions consume/earn 双写、usage_records、keys.used 累计;
  settle 事务性回滚(幽灵 key 拒绝入账)
- models 表启动种子(data/models.example.json + config.price_overrides upsert)
- 测试 33 个全过(路由 6 / 计费 6 / 网关 7 + 既有 14),clippy + fmt 干净;
  冒烟:无 token 401、placeholder 上游 503、/api/models 列表

Rant: 2026-08-18T09:55:57(P0-B 网关转发 + 路由故障转移 + 计量账本闭环)
@argszero
argszero merged commit 975238b into main Aug 18, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant