Sync shared snapshot from 097442f
Docker / Docker Build & Push (push) Successful in 3m20s

This commit is contained in:
Gabriel20xx committed 2026-09-11 11:57:31 +02:00
1 parent a2a96f6a46
commit 439ef02496
2 files changed
+96 -31

No files matched your search

@@ -77,12 +77,12 @@ func New(internalServiceSecret string, baseURL func(ctx context.Context) string)
// The candidates are used differently depending on the operation: // The candidates are used differently depending on the operation:
// - ListFiles (discovery) queries every instance and merges the results, // - ListFiles (discovery) queries every instance and merges the results,
// so files living on distinct storage backends are all found. // so files living on distinct storage backends are all found.
// - Read operations tied to one file (Probe, FetchToScratch, ProbeMedia) // - Read operations tied to one file (Proxy, Probe, FetchToScratch,
// try each instance in order and use the first one that actually has the // ProbeMedia) try each instance in order and use the first one that
// file, so a replica standing in for a downed primary (or a file that // actually has the file, so playback/thumbnails/metadata for a file
// simply lives on a non-primary storage backend) is still found. // living on a non-primary storage backend still work, and a replica
// - Writes/streaming that don't originate from a discovered file // standing in for a downed primary is still found.
// (Proxy, PushThumbnail, DeleteThumbnail, PushOutput, PushInput, // - Writes (PushThumbnail, DeleteThumbnail, PushOutput, PushInput,
// PushCopy, UploadDownload, StoreDownload) always target the primary // PushCopy, UploadDownload, StoreDownload) always target the primary
// instance (the first candidate), matching single-instance behavior. // instance (the first candidate), matching single-instance behavior.
func NewMulti(internalServiceSecret string, baseURLs func(ctx context.Context) []string) *Client { func NewMulti(internalServiceSecret string, baseURLs func(ctx context.Context) []string) *Client {
@@ -131,33 +131,55 @@ func (c *Client) newRequest(ctx context.Context, baseURL, method, path string, b
// Proxy streams a CarrierNode response straight through to the original // Proxy streams a CarrierNode response straight through to the original
// client, preserving range/conditional-request semantics so video scrubbing // client, preserving range/conditional-request semantics so video scrubbing
// keeps working without reimplementing http.ServeContent on this side. // keeps working without reimplementing http.ServeContent on this side. Every
// Always targets the primary instance - see NewMulti's doc comment. // configured instance is tried in order (see NewMulti's doc comment); the
// first one that doesn't answer with an error is the one actually streamed,
// so a file living on a non-primary instance still plays and a downed
// primary doesn't block a replica from serving it. The last candidate's
// response (success or error) is always the one forwarded, matching
// single-instance Proxy's behavior of passing even an error status through
// to the caller rather than swallowing it.
func (c *Client) Proxy(w http.ResponseWriter, r *http.Request, carrierNodePath string) { func (c *Client) Proxy(w http.ResponseWriter, r *http.Request, carrierNodePath string) {
request, err := c.newRequest(r.Context(), c.primaryBaseURL(r.Context()), r.Method, carrierNodePath, nil) candidates := c.candidateBaseURLs(r.Context())
if err != nil { if len(candidates) == 0 {
writeError(w, http.StatusBadGateway, "media service unavailable") candidates = []string{""}
return
} }
for _, header := range forwardRequestHeaders { for i, baseURL := range candidates {
if value := r.Header.Get(header); value != "" { last := i == len(candidates)-1
request.Header.Set(header, value) request, err := c.newRequest(r.Context(), baseURL, r.Method, carrierNodePath, nil)
if err != nil {
if last {
writeError(w, http.StatusBadGateway, "media service unavailable")
}
continue
} }
} for _, header := range forwardRequestHeaders {
response, err := httpClient.Do(request) if value := r.Header.Get(header); value != "" {
if err != nil { request.Header.Set(header, value)
writeError(w, http.StatusBadGateway, "media service unavailable") }
return
}
defer response.Body.Close()
for _, header := range forwardResponseHeaders {
if value := response.Header.Get(header); value != "" {
w.Header().Set(header, value)
} }
} response, err := httpClient.Do(request)
w.WriteHeader(response.StatusCode) if err != nil {
if r.Method != http.MethodHead { if last {
_, _ = io.Copy(w, response.Body) writeError(w, http.StatusBadGateway, "media service unavailable")
}
continue
}
if response.StatusCode >= 400 && !last {
response.Body.Close()
continue
}
defer response.Body.Close()
for _, header := range forwardResponseHeaders {
if value := response.Header.Get(header); value != "" {
w.Header().Set(header, value)
}
}
w.WriteHeader(response.StatusCode)
if r.Method != http.MethodHead {
_, _ = io.Copy(w, response.Body)
}
return
} }
} }
@@ -105,7 +105,7 @@ func TestProbeExistsFalseWhenNoInstanceHasFile(t *testing.T) {
} }
} }
func TestProxyAlwaysTargetsPrimaryInstance(t *testing.T) { func TestProxyPrefersPrimaryInstanceWhenItHasTheFile(t *testing.T) {
var primaryHit, secondaryHit bool var primaryHit, secondaryHit bool
primary := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { primary := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
primaryHit = true primaryHit = true
@@ -128,6 +128,49 @@ func TestProxyAlwaysTargetsPrimaryInstance(t *testing.T) {
t.Fatal("Proxy did not hit the primary instance") t.Fatal("Proxy did not hit the primary instance")
} }
if secondaryHit { if secondaryHit {
t.Fatal("Proxy hit the secondary instance, want primary-only") t.Fatal("Proxy hit the secondary instance even though the primary had the file")
}
}
func TestProxyFallsBackToSecondaryInstanceWhenPrimaryMisses(t *testing.T) {
primary := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound)
}))
defer primary.Close()
secondary := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "video/mp4")
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte("video bytes"))
}))
defer secondary.Close()
client := NewMulti("", func(ctx context.Context) []string { return []string{primary.URL, secondary.URL} })
req := httptest.NewRequest(http.MethodGet, "/media/a.mp4", nil)
rec := httptest.NewRecorder()
client.Proxy(rec, req, "/media/a.mp4")
if rec.Code != http.StatusOK {
t.Fatalf("status = %d, want 200 from the secondary instance", rec.Code)
}
if rec.Body.String() != "video bytes" {
t.Fatalf("body = %q, want secondary instance's bytes", rec.Body.String())
}
}
func TestProxyForwardsLastInstanceErrorWhenNoneHaveTheFile(t *testing.T) {
notFound := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound)
}))
defer notFound.Close()
client := NewMulti("", func(ctx context.Context) []string { return []string{notFound.URL, notFound.URL} })
req := httptest.NewRequest(http.MethodGet, "/media/missing.mp4", nil)
rec := httptest.NewRecorder()
client.Proxy(rec, req, "/media/missing.mp4")
if rec.Code != http.StatusNotFound {
t.Fatalf("status = %d, want 404 forwarded from the last instance", rec.Code)
} }
} }