This commit is contained in:
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:
|
||||
// - ListFiles (discovery) queries every instance and merges the results,
|
||||
// so files living on distinct storage backends are all found.
|
||||
// - Read operations tied to one file (Probe, FetchToScratch, ProbeMedia)
|
||||
// try each instance in order and use the first one that actually has the
|
||||
// file, so a replica standing in for a downed primary (or a file that
|
||||
// simply lives on a non-primary storage backend) is still found.
|
||||
// - Writes/streaming that don't originate from a discovered file
|
||||
// (Proxy, PushThumbnail, DeleteThumbnail, PushOutput, PushInput,
|
||||
// - Read operations tied to one file (Proxy, Probe, FetchToScratch,
|
||||
// ProbeMedia) try each instance in order and use the first one that
|
||||
// actually has the file, so playback/thumbnails/metadata for a file
|
||||
// living on a non-primary storage backend still work, and a replica
|
||||
// standing in for a downed primary is still found.
|
||||
// - Writes (PushThumbnail, DeleteThumbnail, PushOutput, PushInput,
|
||||
// PushCopy, UploadDownload, StoreDownload) always target the primary
|
||||
// instance (the first candidate), matching single-instance behavior.
|
||||
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
|
||||
// client, preserving range/conditional-request semantics so video scrubbing
|
||||
// keeps working without reimplementing http.ServeContent on this side.
|
||||
// Always targets the primary instance - see NewMulti's doc comment.
|
||||
// keeps working without reimplementing http.ServeContent on this side. Every
|
||||
// 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) {
|
||||
request, err := c.newRequest(r.Context(), c.primaryBaseURL(r.Context()), r.Method, carrierNodePath, nil)
|
||||
if err != nil {
|
||||
writeError(w, http.StatusBadGateway, "media service unavailable")
|
||||
return
|
||||
candidates := c.candidateBaseURLs(r.Context())
|
||||
if len(candidates) == 0 {
|
||||
candidates = []string{""}
|
||||
}
|
||||
for _, header := range forwardRequestHeaders {
|
||||
if value := r.Header.Get(header); value != "" {
|
||||
request.Header.Set(header, value)
|
||||
for i, baseURL := range candidates {
|
||||
last := i == len(candidates)-1
|
||||
request, err := c.newRequest(r.Context(), baseURL, r.Method, carrierNodePath, nil)
|
||||
if err != nil {
|
||||
if last {
|
||||
writeError(w, http.StatusBadGateway, "media service unavailable")
|
||||
}
|
||||
continue
|
||||
}
|
||||
}
|
||||
response, err := httpClient.Do(request)
|
||||
if err != nil {
|
||||
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)
|
||||
for _, header := range forwardRequestHeaders {
|
||||
if value := r.Header.Get(header); value != "" {
|
||||
request.Header.Set(header, value)
|
||||
}
|
||||
}
|
||||
}
|
||||
w.WriteHeader(response.StatusCode)
|
||||
if r.Method != http.MethodHead {
|
||||
_, _ = io.Copy(w, response.Body)
|
||||
response, err := httpClient.Do(request)
|
||||
if err != nil {
|
||||
if last {
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user