Skip to content

feat(config): Add DcRegion for x-dc-region data-center routing#554

Open
huacnlee wants to merge 2 commits into
mainfrom
feat/us-dc-region
Open

feat(config): Add DcRegion for x-dc-region data-center routing#554
huacnlee wants to merge 2 commits into
mainfrom
feat/us-dc-region

Conversation

@huacnlee

Copy link
Copy Markdown
Member

What

Adds first-class support for the x-dc-region data-center routing header.

Longbridge access tokens are prefixed with their region (us_… / ap_…). The API gateway routes a request to the matching data center based on the x-dc-region header; an absent header defaults to ap.

Changes

  • New region module: DcRegion enum with from_access_token (prefix us_Us, otherwise Ap) and as_str ("us" / "ap"), plus the DC_REGION_HEADER constant.
  • Config::dc_region() convenience that sets x-dc-region on every HTTP and WebSocket upgrade request (via the existing custom_headers path).
  • Re-exported DcRegion and DC_REGION_HEADER at the crate root.

Consumers (longbridge-terminal, longbridge-mcp) derive the region from their access token and attach the header so US-region tokens reach the US data center. US and AP share the same OAuth endpoints — only the routing header differs.

Test

  • Unit tests for DcRegion::from_access_token (incl. Bearer tolerance and unprefixed → ap default) and as_str.
  • cargo clippy --all-features clean; cargo +nightly fmt applied.

🤖 Generated with Claude Code

Longbridge credentials are prefixed with their data center: `us_…` for
the US data center and `ap_…` for Asia-Pacific. This applies to the OAuth
access token and, in legacy API-key mode, to the `app_key`, `app_secret`,
and `access_token`. The API gateway routes a request to the matching data
center via the `x-dc-region` header, defaulting to `ap` when absent.

- `longbridge-geo`: add `DcRegion` (`from_credential` / `from_credentials`
  / `as_str`) and the `DC_REGION_HEADER` constant, re-exported through
  `longbridge-httpcli` and the crate root.
- The HTTP client and the quote/trade WebSocket upgrade now auto-inject
  `x-dc-region` derived from the auth credentials, so US-region tokens
  reach the US data center with no caller changes. An explicitly-set
  header (e.g. via a custom header or `Config::dc_region`) is preserved.
- `Config::dc_region()` remains as an explicit override.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@huacnlee huacnlee force-pushed the feat/us-dc-region branch from a7a4af1 to d35f634 Compare June 30, 2026 04:07
The `us_`/`ap_` prefix on an access token is region metadata used to derive
the `x-dc-region` routing header — it is not part of the verifiable bearer
credential. Sending the full `us_…` token in `Authorization: Bearer` makes
the gateway reject it (401102 token verification failed).

Strip the region prefix before building the `Authorization` header (deriving
the region from the prefix first), so the gateway verifies the bare token and
routes by `x-dc-region`. WebSocket auth is unaffected: it uses an OTP fetched
over this same HTTP path. Add `DcRegion::strip_region_prefix`.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
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