-
Notifications
You must be signed in to change notification settings - Fork 43
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
Spike: Use retry middleware with reverse proxy #64
Closed
Closed
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
9107aef
Start retry for vanished backends
alpe 4424ef7
Add retry middleware
alpe 6001e97
Better naming
alpe 9b99c6c
Apply review feedback; refactorings
alpe e71df9f
Minor cleanup
alpe b74a395
Lazy buffer request body for retry + reuse; refactorings
alpe 296c6c4
Configurable number of retries
alpe 4299373
Remove panic
alpe d0b684a
Fixes and tests
alpe 5fb2e66
Better status code capturing
alpe 9011105
Review feedback
alpe a5a39ab
Simplify middleware
alpe 723e417
Demo retry middleware with reverse proxy
alpe 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
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,75 @@ | ||
package proxy | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httptest" | ||
"net/http/httputil" | ||
"net/url" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"github.com/substratusai/lingo/pkg/endpoints" | ||
"github.com/substratusai/lingo/pkg/queue" | ||
) | ||
|
||
func TestProxy(t *testing.T) { | ||
specs := map[string]struct { | ||
request *http.Request | ||
expCode int | ||
}{ | ||
"ok": { | ||
request: httptest.NewRequest(http.MethodGet, "/", strings.NewReader(`{"model":"my_model"}`)), | ||
expCode: http.StatusOK, | ||
}, | ||
} | ||
for name, spec := range specs { | ||
t.Run(name, func(t *testing.T) { | ||
deplMgr := mockDeploymentSource{ | ||
ResolveDeploymentFn: func(model string) (string, bool) { | ||
return "my-deployment", true | ||
}, | ||
AtLeastOneFn: func(deploymentName string) {}, | ||
} | ||
em, err := endpoints.NewManager(&fakeManager{}, func(deploymentName string, replicas int) {}) | ||
require.NoError(t, err) | ||
em.SetEndpoints("my-deployment", map[string]struct{}{"my-ip": {}}, map[string]int32{"my-port": 8080}) | ||
h := NewHandler(deplMgr, em, queue.NewManager(10), 1) | ||
|
||
svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
em.SetEndpoints("my-deployment", map[string]struct{}{"my-other-ip": {}}, map[string]int32{"my-other-port": 8080}) | ||
w.WriteHeader(http.StatusOK) | ||
})) | ||
recorder := httptest.NewRecorder() | ||
|
||
AdditionalProxyRewrite = func(r *httputil.ProxyRequest) { | ||
r.SetURL(&url.URL{Scheme: "http", Host: svr.Listener.Addr().String()}) | ||
} | ||
|
||
// when | ||
h.ServeHTTP(recorder, spec.request) | ||
// then | ||
assert.Equal(t, spec.expCode, recorder.Code) | ||
}) | ||
} | ||
} | ||
|
||
type mockDeploymentSource struct { | ||
ResolveDeploymentFn func(model string) (string, bool) | ||
AtLeastOneFn func(deploymentName string) | ||
} | ||
|
||
func (m mockDeploymentSource) ResolveDeployment(model string) (string, bool) { | ||
if m.ResolveDeploymentFn == nil { | ||
panic("not expected to be called") | ||
} | ||
return m.ResolveDeploymentFn(model) | ||
} | ||
|
||
func (m mockDeploymentSource) AtLeastOne(deploymentName string) { | ||
if m.AtLeastOneFn == nil { | ||
panic("not expected to be called") | ||
} | ||
m.AtLeastOneFn(deploymentName) | ||
} |
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
Oops, something went wrong.
Oops, something went wrong.
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.
This is how the middleware is integrated with the reverse proxy