feat(config): Add DcRegion for x-dc-region data-center routing#554
Open
huacnlee wants to merge 2 commits into
Open
feat(config): Add DcRegion for x-dc-region data-center routing#554huacnlee wants to merge 2 commits into
huacnlee wants to merge 2 commits into
Conversation
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>
a7a4af1 to
d35f634
Compare
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Adds first-class support for the
x-dc-regiondata-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 thex-dc-regionheader; an absent header defaults toap.Changes
regionmodule:DcRegionenum withfrom_access_token(prefixus_→Us, otherwiseAp) andas_str("us"/"ap"), plus theDC_REGION_HEADERconstant.Config::dc_region()convenience that setsx-dc-regionon every HTTP and WebSocket upgrade request (via the existingcustom_headerspath).DcRegionandDC_REGION_HEADERat 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
DcRegion::from_access_token(incl.Bearertolerance and unprefixed →apdefault) andas_str.cargo clippy --all-featuresclean;cargo +nightly fmtapplied.🤖 Generated with Claude Code