-
Notifications
You must be signed in to change notification settings - Fork 2
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
Return an HTTP 429 when we're over capacity #232
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
ac1fca6
Return an HTTP 429 when we're over capacity
thomshutt ba1cc1b
Remove unused method
thomshutt 0e3c52e
Reduce max jobs in flight
thomshutt 6381348
Only take Mist Pipeline jobs into account for capacity
thomshutt f1528e2
Test MediaConvert jobs are ignored in capacity calculation
thomshutt f65f399
Revert "Test MediaConvert jobs are ignored in capacity calculation"
thomshutt 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 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 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 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,23 @@ | ||
package middleware | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/julienschmidt/httprouter" | ||
"github.com/livepeer/catalyst-api/pipeline" | ||
) | ||
|
||
// Somewhat arbitrary and conservative number of maximum Catalyst VOD jobs in the system | ||
// at one time. We can look at more sophisticated strategies for calculating capacity in | ||
// the future. | ||
const MAX_JOBS_IN_FLIGHT = 5 | ||
|
||
func HasCapacity(vodEngine *pipeline.Coordinator, next httprouter.Handle) httprouter.Handle { | ||
return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { | ||
if vodEngine.InFlightMistPipelineJobs() >= MAX_JOBS_IN_FLIGHT { | ||
w.WriteHeader(http.StatusTooManyRequests) | ||
return | ||
} | ||
next(w, r, ps) | ||
} | ||
} |
This file contains 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 @@ | ||
package middleware | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/julienschmidt/httprouter" | ||
"github.com/livepeer/catalyst-api/pipeline" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestItCallsNextMiddlewareWhenCapacityAvailable(t *testing.T) { | ||
// Create a next handler in the middleware chain, to confirm the request was passed onwards | ||
var nextCalled bool | ||
next := func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { | ||
nextCalled = true | ||
} | ||
|
||
// Set up the HTTP handler | ||
handler := HasCapacity(pipeline.NewStubCoordinator(), next) | ||
|
||
// Call the handler | ||
responseRecorder := httptest.NewRecorder() | ||
handler(responseRecorder, nil, nil) | ||
|
||
// Confirm we got a success reponse and that the handler called the next middleware | ||
require.Equal(t, http.StatusOK, responseRecorder.Code) | ||
require.True(t, nextCalled) | ||
} | ||
|
||
func TestItErrorsWhenNoCapacityAvailable(t *testing.T) { | ||
// Create a next handler in the middleware chain, to confirm the request was passed onwards | ||
var nextCalled bool | ||
next := func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { | ||
nextCalled = true | ||
} | ||
|
||
// Create a lot of in-flight jobs | ||
coordinator := pipeline.NewStubCoordinator() | ||
for x := 0; x < 5; x++ { | ||
coordinator.StartUploadJob(pipeline.UploadJobPayload{ | ||
RequestID: fmt.Sprintf("request-%d", x), | ||
}) | ||
} | ||
|
||
// Set up the HTTP handler | ||
handler := HasCapacity(coordinator, next) | ||
|
||
// Call the handler | ||
responseRecorder := httptest.NewRecorder() | ||
handler(responseRecorder, nil, nil) | ||
|
||
// Confirm we got an HTTP 429 response | ||
require.Equal(t, http.StatusTooManyRequests, responseRecorder.Code) | ||
|
||
// Confirm the handler didn't call the next middleware | ||
require.False(t, nextCalled) | ||
} |
This file contains 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm I wonder how racy this is since the jobs run in background and the stub handlers return immediately. Does it pass consistently?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, I think because the stub handlers never trigger the Mist callbacks right? So the jobs stay in flight
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure actually. I think they may return an error which finishes the job and sends a callback
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.