Skip to content

Tích hợp AccountPool vào proxy handler (failover-ready routing) #4

Closed

Description

@longphamdev

Tích hợp AccountPool vào handler

What to build

Nối AccountPool vào request path của proxy, giữ behavior cũ cho 1 acc:

  • Handler pick acc qua pool.reserve() thay vì auth.getCredential() trực tiếp; finally + res.on('close') release — mọi đường thoát: stream xong, lỗi upstream, client abort, response hoàn thành bình thường. Client cancel KHÔNG ảnh hưởng health acc.
  • Header debug x-zcode-account: <id> chỉ khi debug: true (không leak acc cho client ngoài).
  • 1 acc duy nhất → behavior y như cũ: không thêm queue, không thêm header trừ khi debug, tests cũ của handler phải pass không sửa (trừ chỗ credential inject).
  • Pool wiring: AuthManager giữ vai trò trước store cho compat; handler đọc qua pool.
  • Nền cho failover (ticket 3): pick → reserve → inject credential → forward. Chưa có retry sang acc khác trong ticket này.

Acceptance criteria

  • 2 acc healthy → 2 request liên tiếp dùng 2 acc khác nhau (xác nhận qua header debug)
  • 1 acc → behavior cũ: tests handler hiện tại pass, không regression
  • Release chạy trên mọi đường thoát: test stream xong / upstream lỗi / client abort giữa SSE / client abort trước headers — inFlight về 0 sau mỗi case
  • Client abort không làm acc cooling
  • Header x-zcode-account chỉ xuất hiện khi debug bật
  • AuthManager compat: các consumers cũ (quota, TUI, Android, claim) vẫn chạy được

Activity

  1. longphamdev commented on Nov 9, 2026

    @longphamdev
    OwnerAuthor

    Bổ sung acceptance (từ review #5):

  2. longphamdev commented on Nov 9, 2026

    @longphamdev
    OwnerAuthor

    Prerequisite từ review #1 (bắt buộc trước khi nối pool):

    2 test sendOrderedUpstreamRequest abort đang fail sẵn (đã verify pre-existing tại #1). Nhưng reserve/release abort-safe (#3) và test release-khi-client-abort ở ticket này dựa vào đúng hành vi abort của sendOrderedUpstreamRequest — nền hiện tại không tin cậy.

    Bước đầu của ticket này: điều tra 2 test fail đó — sửa test (nếu flaky timing) hoặc sửa code (nếu bug abort propagation thật) — trước khi viết bất kỳ logic pool-integration nào. Chấp nhận: test abort pass ổn định hoặc có phân tích rõ ràng vì sao hành vi hiện tại vẫn an toàn cho pool.

  3. longphamdev commented on Nov 9, 2026

    @longphamdev
    OwnerAuthor

    Tích hợp AccountPool hoàn thành.

    Prereq (abort tests): root cause là Bun 1.3.x regression — server-side http socket không phát close event khi client destroy đột ngột (verify bằng raw net.Socket trên Node 23: cùng code phát close đúng). Code proxy đúng (promise reject + destroy client-side + postWrite flag). Fix: tests pin contract có thể quan sát (reject + đúng 1 upstream dispatch, không retry); comment ghi rõ để re-assert khi Bun sửa.

    Integration:

    • 1 acc = pass-through: test 20 concurrent requests, không queue/429, inFlight về 0
    • Credential per-request: handler + /v1/responses đều reserve từ pool; highspeed auth dùng đúng cred của acc được pick (đi qua biến cred chung)
    • Release mọi đường thoát: stream end (pipeTo finally), cancel (client abort), upstream error, captcha failure, translation error — release() idempotent đỡ double-wire
    • Error mapping: all-cooling → 429 + Retry-After; accounts busy → 429 + Retry-After 1s; all-exhausted → 503
    • x-zcode-account chỉ khi debug: true
    • 429+Retry-After → cooling acc; 401 start-plan giữ behavior cũ; unknown error log status + headers redacted + body preview 400 chars (tích lũy mẫu cho Failover theo phân loại lỗi (retry trước headers, capture mẫu lỗi thật) #5)
    • Hot-reload: startServer sync pool mỗi 5s — test thêm acc mid-run dùng được ngay

    Suite: 1181 pass / 0 fail (baseline 1140/2). Frontier: #5 và #6 (song song).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions