fix(playback): finish segment acknowledgements
This commit is contained in:
@@ -85,7 +85,11 @@ func Acknowledge(ctx context.Context, client *http.Client, targetURL, jwtSecret,
|
||||
if client == nil {
|
||||
client = http.DefaultClient
|
||||
}
|
||||
ackCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
|
||||
// The downstream request is commonly canceled as soon as its response body
|
||||
// reaches the client, while the handler is still issuing this acknowledgement.
|
||||
// Preserve request-scoped values, but give the post-response hop its own
|
||||
// bounded lifetime so a successful delivery is not mistaken for a disconnect.
|
||||
ackCtx, cancel := context.WithTimeout(context.WithoutCancel(ctx), 5*time.Second)
|
||||
defer cancel()
|
||||
req, err := http.NewRequestWithContext(ackCtx, http.MethodPost, targetURL+"/downloaded", nil)
|
||||
if err != nil {
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
package transcodeproxy
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestAcknowledgeSurvivesCompletedDownstreamRequest(t *testing.T) {
|
||||
const (
|
||||
jwtSecret = "test-secret"
|
||||
generation = "incarnation:generation"
|
||||
)
|
||||
|
||||
acknowledged := make(chan struct{}, 1)
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodPost {
|
||||
t.Errorf("method = %q, want POST", r.Method)
|
||||
}
|
||||
if got := r.Header.Get("Authorization"); got != "Bearer "+jwtSecret {
|
||||
t.Errorf("Authorization = %q", got)
|
||||
}
|
||||
if got := r.Header.Get(GenerationHeader); got != generation {
|
||||
t.Errorf("generation = %q, want %q", got, generation)
|
||||
}
|
||||
acknowledged <- struct{}{}
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
}))
|
||||
t.Cleanup(server.Close)
|
||||
|
||||
requestCtx, cancelRequest := context.WithCancel(context.Background())
|
||||
cancelRequest()
|
||||
if err := Acknowledge(requestCtx, server.Client(), server.URL+"/segment", jwtSecret, generation); err != nil {
|
||||
t.Fatalf("Acknowledge() error = %v", err)
|
||||
}
|
||||
select {
|
||||
case <-acknowledged:
|
||||
default:
|
||||
t.Fatal("acknowledgement request was not delivered")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user