-
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.
+637
−117
Open
Changes from 2 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 ./apitest/... | ||
|
github-advanced-security[bot] marked this conversation as resolved.
Fixed
|
||
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,139 @@ | ||
| // 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 apitest holds black-box API tests that exercise the public server SDK | ||
| // against the shared mock LiveKit API server (livekit/livekit cmd/test-server). | ||
| // Point them at a running instance with LK_TEST_SERVER_URL (default | ||
| // http://127.0.0.1:9999); they skip when no server is reachable. In CI the | ||
| // server is booted as a Docker container. | ||
| // | ||
| // See cmd/test-server/README.md for the X-Lk-Mock-* control protocol. Mock | ||
| // directives are passed to the SDK via twirp request headers, which the | ||
| // failover transport forwards to the discovery fetch and every retry. | ||
| package apitest | ||
|
|
||
| import ( | ||
| "context" | ||
| "net/http" | ||
| "os" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
| "github.com/twitchtv/twirp" | ||
|
|
||
| "github.com/livekit/protocol/livekit" | ||
| lksdk "github.com/livekit/server-sdk-go/v2" | ||
| ) | ||
|
|
||
| const ( | ||
| hdrFailRegions = "X-Lk-Mock-Fail-Regions" | ||
| hdrFailMode = "X-Lk-Mock-Fail-Mode" | ||
| hdrFailStatus = "X-Lk-Mock-Fail-Status" | ||
| hdrRegionsStat = "X-Lk-Mock-Regions-Status" | ||
| ) | ||
|
|
||
| func testServerURL(t *testing.T) string { | ||
| url := os.Getenv("LK_TEST_SERVER_URL") | ||
| if url == "" { | ||
| url = "http://127.0.0.1:9999" | ||
| } | ||
| resp, err := http.Get(url + "/settings/regions") | ||
| if err != nil { | ||
| t.Skipf("mock test server not reachable at %s (set LK_TEST_SERVER_URL): %v", url, err) | ||
| } | ||
| _ = resp.Body.Close() | ||
| return url | ||
| } | ||
|
|
||
| // failoverCtx returns a context that forces failover on (the mock is not a | ||
| // cloud host) with a tiny backoff, carrying the given X-Lk-Mock-* directives as | ||
| // twirp request headers. | ||
| func failoverCtx(t *testing.T, mode lksdk.FailoverMode, directives map[string]string) context.Context { | ||
| ctx := lksdk.WithFailoverConfig(context.Background(), lksdk.FailoverConfig{ | ||
| Mode: mode, | ||
| BackoffBase: time.Millisecond, | ||
| }) | ||
| if len(directives) > 0 { | ||
| h := make(http.Header) | ||
| for k, v := range directives { | ||
| h.Set(k, v) | ||
| } | ||
| var err error | ||
| ctx, err = twirp.WithHTTPRequestHeaders(ctx, h) | ||
| require.NoError(t, err) | ||
| } | ||
| return ctx | ||
| } | ||
|
|
||
| func TestAPI_Healthy(t *testing.T) { | ||
| client := lksdk.NewRoomServiceClient(testServerURL(t), "devkey", "secret") | ||
| room, err := client.CreateRoom(failoverCtx(t, lksdk.FailoverOn, nil), &livekit.CreateRoomRequest{Name: "api-test"}) | ||
| require.NoError(t, err) | ||
| require.Equal(t, "api-test", room.Name, "the mock echoes the request name") | ||
| require.NotEmpty(t, room.Sid) | ||
| } | ||
|
|
||
| func TestAPI_PrimaryUnavailable(t *testing.T) { | ||
| client := lksdk.NewRoomServiceClient(testServerURL(t), "devkey", "secret") | ||
| ctx := failoverCtx(t, lksdk.FailoverOn, map[string]string{hdrFailRegions: "0"}) | ||
| _, err := client.CreateRoom(ctx, &livekit.CreateRoomRequest{Name: "api-test"}) | ||
| require.NoError(t, err, "should fail over to a healthy region") | ||
| } | ||
|
|
||
| func TestAPI_TwoRegionsUnavailable(t *testing.T) { | ||
| client := lksdk.NewRoomServiceClient(testServerURL(t), "devkey", "secret") | ||
| ctx := failoverCtx(t, lksdk.FailoverOn, map[string]string{hdrFailRegions: "0,1"}) | ||
| _, err := client.CreateRoom(ctx, &livekit.CreateRoomRequest{Name: "api-test"}) | ||
| require.NoError(t, err, "should fail over to the third region") | ||
| } | ||
|
|
||
| func TestAPI_AllUnavailable(t *testing.T) { | ||
| client := lksdk.NewRoomServiceClient(testServerURL(t), "devkey", "secret") | ||
| ctx := failoverCtx(t, lksdk.FailoverOn, map[string]string{hdrFailRegions: "0,1,2,3"}) | ||
| _, err := client.CreateRoom(ctx, &livekit.CreateRoomRequest{Name: "api-test"}) | ||
| require.Error(t, err) | ||
| } | ||
|
|
||
| func TestAPI_ClientErrorNotRetried(t *testing.T) { | ||
| client := lksdk.NewRoomServiceClient(testServerURL(t), "devkey", "secret") | ||
| ctx := failoverCtx(t, lksdk.FailoverOn, map[string]string{hdrFailRegions: "0", hdrFailStatus: "400"}) | ||
| _, err := client.CreateRoom(ctx, &livekit.CreateRoomRequest{Name: "api-test"}) | ||
| require.Error(t, err) | ||
| var terr twirp.Error | ||
| require.ErrorAs(t, err, &terr) | ||
| require.Equal(t, twirp.InvalidArgument, terr.Code(), "a 4xx must surface as a typed error, not fail over") | ||
| } | ||
|
|
||
| func TestAPI_TransportError(t *testing.T) { | ||
| client := lksdk.NewRoomServiceClient(testServerURL(t), "devkey", "secret") | ||
| ctx := failoverCtx(t, lksdk.FailoverOn, map[string]string{hdrFailRegions: "0", hdrFailMode: "drop"}) | ||
| _, err := client.CreateRoom(ctx, &livekit.CreateRoomRequest{Name: "api-test"}) | ||
| require.NoError(t, err, "a dropped connection should fail over to a healthy region") | ||
| } | ||
|
|
||
| func TestAPI_RegionDiscoveryUnreachable(t *testing.T) { | ||
| client := lksdk.NewRoomServiceClient(testServerURL(t), "devkey", "secret") | ||
| ctx := failoverCtx(t, lksdk.FailoverOn, map[string]string{hdrFailRegions: "0", hdrRegionsStat: "500"}) | ||
| _, err := client.CreateRoom(ctx, &livekit.CreateRoomRequest{Name: "api-test"}) | ||
| require.Error(t, err, "no fallback hosts means the original error is surfaced") | ||
| } | ||
|
|
||
| func TestAPI_FailoverDisabledForNonCloud(t *testing.T) { | ||
| client := lksdk.NewRoomServiceClient(testServerURL(t), "devkey", "secret") | ||
| // Auto mode against 127.0.0.1 (non-cloud) must not fail over. | ||
| ctx := failoverCtx(t, lksdk.FailoverAuto, map[string]string{hdrFailRegions: "0"}) | ||
| _, err := client.CreateRoom(ctx, &livekit.CreateRoomRequest{Name: "api-test"}) | ||
| require.Error(t, err) | ||
| } |
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
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.