Skip to content

Commit a05ab5a

Browse files
committed
failover: enable retries for the LiveKit Cloud API hosts
cloud-api.livekit.io has a single origin and no region list, so a lost request was surfaced after one attempt. Treat cloud-api.<env>.livekit.io like a cloud project host so the transport's same-host retry applies.
1 parent cca3e5b commit a05ab5a

2 files changed

Lines changed: 48 additions & 4 deletions

File tree

‎failover.go‎

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,22 @@ type failoverConfig struct {
6969
}
7070

7171
// attempts returns the total request attempts for a host; 1 means no failover.
72-
// Failover engages only when enabled and the host is a LiveKit Cloud domain
73-
// (or force is set).
72+
// Failover engages only when enabled and the host is a LiveKit Cloud project
73+
// or Cloud API domain (or force is set).
7474
func (c failoverConfig) attempts(hostname string) int {
75-
if c.enabled && (c.force || isCloud(hostname)) {
75+
if c.enabled && (c.force || isCloud(hostname) || isCloudAPI(hostname)) {
7676
return failoverMaxAttempts
7777
}
7878
return 1
7979
}
8080

81+
// isCloudAPI reports whether the hostname is a LiveKit Cloud API endpoint
82+
// (cloud-api.livekit.io or a cloud-api.<env>.livekit.io variant).
83+
func isCloudAPI(hostname string) bool {
84+
hostname = strings.ToLower(hostname)
85+
return strings.HasPrefix(hostname, "cloud-api.") && strings.HasSuffix(hostname, ".livekit.io")
86+
}
87+
8188
type failoverEnabledKey struct{}
8289
type failoverForceKey struct{}
8390

@@ -238,6 +245,8 @@ func (t *failoverTransport) failover(req *http.Request, maxAttempts int, timeout
238245
scheme, host := req.URL.Scheme, req.URL.Host
239246
tried := map[string]struct{}{strings.ToLower(host): {}}
240247
var regions *livekit.RegionSettings // discovered lazily on the first failure
248+
// A Cloud API host has a single origin; region discovery is never consulted.
249+
discover := !isCloudAPI(req.URL.Hostname())
241250

242251
var resp *http.Response
243252
var err error
@@ -259,7 +268,7 @@ func (t *failoverTransport) failover(req *http.Request, maxAttempts int, timeout
259268
return terminate(resp, err, cancel)
260269
}
261270

262-
if regions == nil {
271+
if regions == nil && discover {
263272
u := url.URL{Scheme: req.URL.Scheme, Host: req.URL.Host, Path: "/settings/regions"}
264273
regions, _ = t.regions.get(req.URL.Host, u.String(), req.Header, 0)
265274
}

‎failover_internal_test.go‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ func TestFailoverAttempts(t *testing.T) {
3939
{failoverConfig{enabled: true}, "myproject.livekit.cloud", failoverMaxAttempts},
4040
{failoverConfig{enabled: true}, "myproject.region.livekit.cloud", failoverMaxAttempts},
4141
{failoverConfig{enabled: true}, "myproject.livekit.io", 1},
42+
// The LiveKit Cloud API hosts fail over too (same-host retry, see failover).
43+
{failoverConfig{enabled: true}, "cloud-api.livekit.io", failoverMaxAttempts},
44+
{failoverConfig{enabled: true}, "cloud-api.staging.livekit.io", failoverMaxAttempts},
45+
{failoverConfig{enabled: true}, "CLOUD-API.LIVEKIT.IO", failoverMaxAttempts},
46+
{failoverConfig{enabled: true}, "cloud-api.example.com", 1},
4247
{failoverConfig{enabled: true}, "example.com", 1},
4348
{failoverConfig{enabled: true}, "127.0.0.1", 1},
4449
{failoverConfig{enabled: true}, "notlivekit.cloud", 1},
@@ -470,3 +475,33 @@ func TestFailoverRetriesSameHostOn5xx(t *testing.T) {
470475
t.Fatalf("expected 2 attempts (5xx then success), got %d", n)
471476
}
472477
}
478+
479+
// A Cloud API host has a single origin: its retries never call region discovery.
480+
func TestFailoverCloudAPISkipsRegionDiscovery(t *testing.T) {
481+
const host = "cloud-api.livekit.io"
482+
483+
stub := &stubRoundTripper{behave: func(attempt int, _ context.Context) (*http.Response, error) {
484+
if attempt == 0 {
485+
return nil, errors.New("read: connection reset by peer")
486+
}
487+
return stubResponse(http.StatusOK), nil
488+
}}
489+
discovery := &stubRoundTripper{behave: func(int, context.Context) (*http.Response, error) {
490+
return nil, errors.New("discovery must not be called")
491+
}}
492+
rc := newRegionCache()
493+
rc.client = &http.Client{Transport: discovery}
494+
tr := &failoverTransport{base: stub, regions: rc}
495+
496+
resp, err := tr.RoundTrip(stubRequest(context.Background(), host))
497+
if err != nil {
498+
t.Fatalf("unexpected error: %v", err)
499+
}
500+
_ = resp.Body.Close()
501+
if n := stub.count(); n != 2 {
502+
t.Fatalf("expected 2 attempts, got %d", n)
503+
}
504+
if n := discovery.count(); n != 0 {
505+
t.Fatalf("expected no region discovery for a Cloud API host, got %d fetches", n)
506+
}
507+
}

0 commit comments

Comments
 (0)