-
Notifications
You must be signed in to change notification settings - Fork 157
feat: auto failover for API with LK Cloud #936
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
davidzhao
wants to merge
4
commits into
main
Choose a base branch
from
dz/api-failover
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| # Copyright 2026 LiveKit, Inc. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| name: Test API | ||
|
|
||
| permissions: | ||
| contents: read | ||
|
|
||
| on: | ||
| workflow_dispatch: | ||
| push: | ||
| branches: [main] | ||
| pull_request: | ||
| branches: [main] | ||
|
|
||
| jobs: | ||
| failover: | ||
| runs-on: ubuntu-latest | ||
| services: | ||
| mock-server: | ||
| image: livekit/test-server:latest | ||
| ports: | ||
| - 9999:9999 | ||
| - 10000:10000 | ||
| - 10001:10001 | ||
| - 10002:10002 | ||
| steps: | ||
| - uses: actions/checkout@1af3b93b6815bc44a9784bd300feb67ff0d1eeb3 # v6 | ||
|
|
||
| - name: Set up Opus | ||
| run: | | ||
| sudo apt-get update | ||
| sudo apt-get install -y libsoxr-dev libopus-dev libopusfile-dev | ||
|
|
||
| - name: Set up Go | ||
| uses: actions/setup-go@4a3601121dd01d1626a1e23e37211e3254c1c06c # v6 | ||
| with: | ||
| go-version: 1.26.0 | ||
|
|
||
| - name: Wait for mock server | ||
| run: | | ||
| for i in $(seq 1 30); do | ||
| curl -sf http://127.0.0.1:9999/settings/regions >/dev/null && exit 0 | ||
| sleep 1 | ||
| done | ||
| echo "mock server did not become ready" && exit 1 | ||
|
|
||
| - name: Run API tests | ||
| run: go test -v -count=1 -run '^TestAPI_' . | ||
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
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,247 @@ | ||
| // Copyright 2026 LiveKit, Inc. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package lksdk | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "context" | ||
| "io" | ||
| "net/http" | ||
| "net/url" | ||
| "strings" | ||
| "time" | ||
|
|
||
| "github.com/livekit/protocol/livekit" | ||
|
|
||
| "github.com/livekit/server-sdk-go/v2/signalling" | ||
| ) | ||
|
|
||
| // Total attempts (the original request plus fallback regions) and the base | ||
| // retry backoff are fixed, not user-configurable, so retries can't be tuned to | ||
| // values that could overwhelm the server. | ||
| const ( | ||
| failoverMaxAttempts = 3 | ||
| failoverBackoffBase = 200 * time.Millisecond | ||
| ) | ||
|
|
||
| // failoverConfig is the resolved per-request region-failover configuration. The | ||
| // public API exposes only the enabled toggle (default true); force and | ||
| // backoffBase are internal test-only knobs. | ||
| type failoverConfig struct { | ||
| enabled bool | ||
| // force bypasses the cloud-host check. Internal testing only. | ||
| force bool | ||
| // backoffBase overrides the retry backoff base. Internal testing only. | ||
| backoffBase time.Duration | ||
| } | ||
|
|
||
| // attempts returns the total request attempts for a host; 1 means no failover. | ||
| // Failover only engages when enabled and the host is a LiveKit Cloud domain. | ||
| // force bypasses the cloud-host check and is for internal testing only. | ||
| func (c failoverConfig) attempts(hostname string) int { | ||
| if c.enabled && (c.force || isCloud(hostname)) { | ||
| return failoverMaxAttempts | ||
| } | ||
| return 1 | ||
| } | ||
|
|
||
| type failoverEnabledKey struct{} | ||
| type failoverForceKey struct{} | ||
|
|
||
| // WithFailover returns a context that enables or disables region failover for | ||
| // API requests made with it (enabled by default). Failover only engages for | ||
| // LiveKit Cloud hosts. Pass the returned context to any service client method. | ||
| func WithFailover(ctx context.Context, enabled bool) context.Context { | ||
| return context.WithValue(ctx, failoverEnabledKey{}, enabled) | ||
| } | ||
|
|
||
| // withFailoverForce returns a context that forces failover on regardless of | ||
| // host and overrides the retry backoff. Internal, test-only. | ||
| func withFailoverForce(ctx context.Context, backoff time.Duration) context.Context { | ||
| return context.WithValue(ctx, failoverForceKey{}, backoff) | ||
| } | ||
|
|
||
| func failoverConfigFromContext(ctx context.Context) failoverConfig { | ||
| cfg := failoverConfig{enabled: true, backoffBase: failoverBackoffBase} | ||
| if enabled, ok := ctx.Value(failoverEnabledKey{}).(bool); ok { | ||
| cfg.enabled = enabled | ||
| } | ||
| if backoff, ok := ctx.Value(failoverForceKey{}).(time.Duration); ok { | ||
| cfg.force = true | ||
| cfg.backoffBase = backoff | ||
| } | ||
| return cfg | ||
| } | ||
|
|
||
| // newAPIHTTPClient returns the *http.Client used by every API service client. | ||
| // It wraps the default transport with region-failover retries. | ||
| func newAPIHTTPClient() *http.Client { | ||
| return &http.Client{ | ||
| Transport: &failoverTransport{base: http.DefaultTransport, regions: sharedAPIRegions}, | ||
| } | ||
| } | ||
|
|
||
| // failoverTransport orchestrates region failover for a single API request. On | ||
| // a retryable failure (any transport error or HTTP 5xx) it discovers | ||
| // alternative regions via /settings/regions and replays the request — body and | ||
| // headers intact — against the next region, with exponential backoff. 4xx | ||
| // responses are returned immediately. | ||
| type failoverTransport struct { | ||
| base http.RoundTripper | ||
| regions *regionCache | ||
| } | ||
|
|
||
| func (t *failoverTransport) RoundTrip(req *http.Request) (*http.Response, error) { | ||
| cfg := failoverConfigFromContext(req.Context()) | ||
| maxAttempts := cfg.attempts(req.URL.Hostname()) | ||
|
|
||
| // Buffer the body once so it can be replayed against each region. | ||
| var body []byte | ||
| if req.Body != nil { | ||
| b, err := io.ReadAll(req.Body) | ||
| _ = req.Body.Close() | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| body = b | ||
| } | ||
|
|
||
| originalScheme, originalHost := req.URL.Scheme, req.URL.Host | ||
| attempted := map[string]struct{}{strings.ToLower(originalHost): {}} | ||
|
|
||
| var ( | ||
| settings *livekit.RegionSettings | ||
| fetchedRegions bool | ||
| resp *http.Response | ||
| err error | ||
| ) | ||
| scheme, host := originalScheme, originalHost | ||
|
|
||
| for attempt := 0; attempt < maxAttempts; attempt++ { | ||
| if ctxErr := req.Context().Err(); ctxErr != nil { | ||
| return nil, ctxErr | ||
| } | ||
|
|
||
| r := req.Clone(req.Context()) | ||
| r.URL.Scheme = scheme | ||
| r.URL.Host = host | ||
| r.Host = host | ||
| if body != nil { | ||
| buf := body | ||
| r.Body = io.NopCloser(bytes.NewReader(buf)) | ||
| r.ContentLength = int64(len(buf)) | ||
| r.GetBody = func() (io.ReadCloser, error) { return io.NopCloser(bytes.NewReader(buf)), nil } | ||
| } | ||
|
|
||
| resp, err = t.base.RoundTrip(r) | ||
| if !isRetryable(resp, err) { | ||
| return resp, err | ||
| } | ||
| if attempt == maxAttempts-1 { | ||
| break // out of attempts; surface the last result | ||
| } | ||
|
|
||
| // discover regions lazily; honor the request scheme (so it works against | ||
| // an http mock) and forward the caller's headers to the discovery fetch. | ||
| if !fetchedRegions { | ||
| discoveryURL := url.URL{Scheme: originalScheme, Host: originalHost, Path: "/settings/regions"} | ||
| settings, _ = t.regions.get(originalHost, discoveryURL.String(), req.Header, 0) | ||
| fetchedRegions = true | ||
| } | ||
| nextScheme, nextHost, ok := nextRegion(settings, attempted) | ||
| if !ok { | ||
| break // no untried region left | ||
| } | ||
|
|
||
| status := 0 | ||
| if resp != nil { | ||
| status = resp.StatusCode | ||
| } | ||
| logger.Warnw("livekit API request failed, retrying with fallback url", err, | ||
| "failedUrl", scheme+"://"+host, "fallbackUrl", nextScheme+"://"+nextHost, | ||
| "attempt", attempt+1, "maxAttempts", maxAttempts, "status", status) | ||
|
|
||
| drainResponse(resp) | ||
| if !sleepCtx(req.Context(), cfg.backoffBase<<uint(attempt)) { | ||
| return resp, err // context cancelled during backoff | ||
| } | ||
| scheme, host = nextScheme, nextHost | ||
| attempted[strings.ToLower(host)] = struct{}{} | ||
| } | ||
| return resp, err | ||
| } | ||
|
|
||
| // isRetryable reports whether a region failover should be attempted: any | ||
| // transport error, or an HTTP 5xx response. 4xx and success are terminal. | ||
| func isRetryable(resp *http.Response, err error) bool { | ||
| if err != nil { | ||
| return true | ||
| } | ||
| return resp != nil && resp.StatusCode >= 500 | ||
| } | ||
|
|
||
| // nextRegion returns the first region whose host has not yet been attempted. | ||
| func nextRegion(settings *livekit.RegionSettings, attempted map[string]struct{}) (scheme, host string, ok bool) { | ||
| if settings == nil { | ||
| return "", "", false | ||
| } | ||
| for _, region := range settings.Regions { | ||
| u, err := url.Parse(signalling.ToHttpURL(region.Url)) | ||
| if err != nil || u.Host == "" { | ||
| continue | ||
| } | ||
| if _, seen := attempted[strings.ToLower(u.Host)]; seen { | ||
| continue | ||
| } | ||
| return u.Scheme, u.Host, true | ||
| } | ||
| return "", "", false | ||
| } | ||
|
|
||
| func drainResponse(resp *http.Response) { | ||
| if resp != nil && resp.Body != nil { | ||
| _, _ = io.Copy(io.Discard, resp.Body) | ||
| _ = resp.Body.Close() | ||
| } | ||
| } | ||
|
|
||
| func sleepCtx(ctx context.Context, d time.Duration) bool { | ||
| if d <= 0 { | ||
| return ctx.Err() == nil | ||
| } | ||
| timer := time.NewTimer(d) | ||
| defer timer.Stop() | ||
| select { | ||
| case <-ctx.Done(): | ||
| return false | ||
| case <-timer.C: | ||
| return true | ||
| } | ||
| } | ||
|
|
||
| // sharedAPIRegions is the process-wide region cache used by the API failover | ||
| // path. The RTC signaling path has its own regionURLProvider over a separate | ||
| // cache, but both share the same regionCache discovery/fetch/TTL logic. | ||
| var sharedAPIRegions = newRegionCache() | ||
|
|
||
| // TwirpRegionError indicates the /settings/regions endpoint returned a non-200 | ||
| // status while attempting region failover. | ||
| type TwirpRegionError struct { | ||
| StatusCode int | ||
| } | ||
|
|
||
| func (e *TwirpRegionError) Error() string { | ||
| return "failed to fetch region settings: status " + http.StatusText(e.StatusCode) | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.