Skip to content
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

fix: executeWithRetry, save the request body and write the body to th… #296

Merged
merged 5 commits into from
Feb 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
"net/http/httputil"
Expand Down Expand Up @@ -197,8 +198,21 @@ func executeWithRetry(
startTime := time.Now()
var since float64

// keep the request body
body, err := ioutil.ReadAll(req.Body)
req.Body.Close()
if err != nil {
since = time.Since(startTime).Seconds()

return since, err
}

numRetry := 0
for {
// update body
req.Body = ioutil.NopCloser(bytes.NewBuffer(body))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't you need to close this req.Body at some point in the loop? (If not, why doing the req.Body.Close() line 208?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, you are right, I need to close the body of the request. I change the code and use defer req.Body.Close(). The request Body needs to be closed immediately after reading it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think you should defer it because you're in the infinite loop that can override multiple times req.Body

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, since the defer is executed when the surrounding function returns, it will not execute when the body is updated. I will close the req.Body after reading or updating it.

req.Body.Close()

rp(rw, req)

err := ctx.Err()
Expand Down
35 changes: 30 additions & 5 deletions proxyretry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package main
import (
"context"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"
"time"

Expand All @@ -25,16 +27,22 @@ type mockStatResponseWriter struct {
}

type mockHosts struct {
t *testing.T
b string
hs []string
hst []string
}

// TestQueryWithRetryFail function statResponseWriter's statusCode will not be 200, but the query has been proxied
// The request will be retried 1 time with a different host in the same replica
func TestQueryWithRetryFail(t *testing.T) {
req := newRequest("http://localhost:8080")
body := "foo query"

req := newRequest("http://localhost:8080", body)

mhs := &mockHosts{
t: t,
b: body,
hs: []string{"localhost:8080", "localhost:8081"},
}

Expand Down Expand Up @@ -71,9 +79,13 @@ func TestQueryWithRetryFail(t *testing.T) {
// TestRunQuerySuccessOnce function statResponseWriter's statusCode will be StatusOK after executeWithRetry, the query has been proxied
// The execution will succeeded without retry
func TestQuerySuccessOnce(t *testing.T) {
req := newRequest("http://localhost:8090")
body := "foo query"

req := newRequest("http://localhost:8090", body)

mhs := &mockHosts{
t: t,
b: body,
hs: []string{"localhost:8080", "localhost:8090"},
}

Expand Down Expand Up @@ -108,9 +120,13 @@ func TestQuerySuccessOnce(t *testing.T) {
// TestQueryWithRetrySuccess function statResponseWriter's statusCode will be StatusOK after executeWithRetry, the query has been proxied
// The execution will succeeded after retry
func TestQueryWithRetrySuccess(t *testing.T) {
req := newRequest("http://localhost:8080")
body := "foo query"

req := newRequest("http://localhost:8080", body)

mhs := &mockHosts{
t: t,
b: body,
hs: []string{"localhost:8080", "localhost:8090"},
}

Expand Down Expand Up @@ -148,12 +164,21 @@ func (mhs *mockHosts) mockReverseProxy(rw http.ResponseWriter, req *http.Request
} else {
rw.WriteHeader(http.StatusOK)
}

b, err := ioutil.ReadAll(req.Body)
if err != nil {
mhs.t.Errorf("The req body cannot be read: %v", err)
}
req.Body.Close()

assert.Equal(mhs.t, mhs.b, string(b))

mhs.hst = append(mhs.hst, req.URL.Host)
}

func newRequest(host string) *http.Request {
func newRequest(host, body string) *http.Request {
// create a new req
req := httptest.NewRequest(http.MethodGet, host, nil)
req := httptest.NewRequest(http.MethodPost, host, strings.NewReader(body))

ctx := context.Background()

Expand Down