-
Notifications
You must be signed in to change notification settings - Fork 353
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: httpclient supporting retry net.Client.Retry()
feature: retry() filter Signed-off-by: Sandor Szücs <[email protected]>
- Loading branch information
Showing
10 changed files
with
687 additions
and
8 deletions.
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,18 @@ | ||
package retry | ||
|
||
import ( | ||
"github.com/zalando/skipper/filters" | ||
) | ||
|
||
type retry struct{} | ||
|
||
// NewRetry creates a filter specification for the retry() filter | ||
func NewRetry() filters.Spec { return retry{} } | ||
|
||
func (retry) Name() string { return filters.RetryName } | ||
func (retry) CreateFilter([]interface{}) (filters.Filter, error) { return retry{}, nil } | ||
func (retry) Response(filters.FilterContext) {} | ||
|
||
func (retry) Request(ctx filters.FilterContext) { | ||
ctx.StateBag()[filters.RetryName] = struct{}{} | ||
} |
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,95 @@ | ||
package retry | ||
|
||
import ( | ||
"bytes" | ||
"io" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/AlexanderYastrebov/noleak" | ||
"github.com/zalando/skipper/eskip" | ||
"github.com/zalando/skipper/filters" | ||
"github.com/zalando/skipper/proxy/proxytest" | ||
) | ||
|
||
func TestRetry(t *testing.T) { | ||
for _, tt := range []struct { | ||
name string | ||
method string | ||
body string | ||
}{ | ||
{ | ||
name: "test GET", | ||
method: "GET", | ||
}, | ||
{ | ||
name: "test POST", | ||
method: "POST", | ||
body: "hello POST", | ||
}, | ||
{ | ||
name: "test PATCH", | ||
method: "PATCH", | ||
body: "hello PATCH", | ||
}, | ||
{ | ||
name: "test PUT", | ||
method: "PUT", | ||
body: "hello PUT", | ||
}} { | ||
t.Run(tt.name, func(t *testing.T) { | ||
i := 0 | ||
backend := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
if i == 0 { | ||
i++ | ||
w.WriteHeader(http.StatusBadGateway) | ||
return | ||
} | ||
|
||
got, err := io.ReadAll(r.Body) | ||
if err != nil { | ||
t.Fatalf("got no data") | ||
} | ||
s := string(got) | ||
if tt.body != s { | ||
t.Fatalf("Failed to get the right data want: %q, got: %q", tt.body, s) | ||
} | ||
|
||
w.WriteHeader(http.StatusOK) | ||
})) | ||
defer backend.Close() | ||
|
||
noleak.Check(t) | ||
|
||
fr := make(filters.Registry) | ||
retry := NewRetry() | ||
fr.Register(retry) | ||
r := &eskip.Route{ | ||
Filters: []*eskip.Filter{ | ||
{Name: retry.Name()}, | ||
}, | ||
Backend: backend.URL, | ||
} | ||
|
||
proxy := proxytest.New(fr, r) | ||
defer proxy.Close() | ||
|
||
buf := bytes.NewBufferString(tt.body) | ||
req, err := http.NewRequest(tt.method, proxy.URL, buf) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
rsp, err := http.DefaultClient.Do(req) | ||
if err != nil { | ||
t.Fatalf("Failed to execute retry: %v", err) | ||
} | ||
|
||
if rsp.StatusCode != http.StatusOK { | ||
t.Fatalf("unexpected status code: %s", rsp.Status) | ||
} | ||
rsp.Body.Close() | ||
}) | ||
} | ||
} |
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,49 @@ | ||
package io | ||
|
||
import ( | ||
"io" | ||
) | ||
|
||
type ReadWriterLen interface { | ||
io.ReadWriter | ||
Len() int | ||
} | ||
|
||
type CopyBodyStream struct { | ||
left int | ||
buf ReadWriterLen | ||
input io.ReadCloser | ||
} | ||
|
||
func NewCopyBodyStream(left int, buf ReadWriterLen, rc io.ReadCloser) *CopyBodyStream { | ||
return &CopyBodyStream{ | ||
left: left, | ||
buf: buf, | ||
input: rc, | ||
} | ||
} | ||
|
||
func (cb *CopyBodyStream) Len() int { | ||
return cb.buf.Len() | ||
} | ||
|
||
func (cb *CopyBodyStream) Read(p []byte) (n int, err error) { | ||
n, err = cb.input.Read(p) | ||
if cb.left > 0 && n > 0 { | ||
m := min(n, cb.left) | ||
written, err := cb.buf.Write(p[:m]) | ||
if err != nil { | ||
return 0, err | ||
} | ||
cb.left -= written | ||
} | ||
return n, err | ||
} | ||
|
||
func (cb *CopyBodyStream) Close() error { | ||
return cb.input.Close() | ||
} | ||
|
||
func (cb *CopyBodyStream) GetBody() io.ReadCloser { | ||
return io.NopCloser(cb.buf) | ||
} |
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,50 @@ | ||
package io | ||
|
||
import ( | ||
"bytes" | ||
"io" | ||
"testing" | ||
) | ||
|
||
func TestCopyBodyStream(t *testing.T) { | ||
s := "content" | ||
bbuf := io.NopCloser(bytes.NewBufferString(s)) | ||
cbs := NewCopyBodyStream(len(s), &bytes.Buffer{}, bbuf) | ||
|
||
buf := make([]byte, len(s)) | ||
_, err := cbs.Read(buf) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if cbs.Len() != len(buf) { | ||
t.Fatalf("Failed to have the same buf buffer size want: %d, got: %d", cbs.Len(), len(buf)) | ||
} | ||
|
||
got, err := io.ReadAll(cbs.GetBody()) | ||
if err != nil { | ||
t.Fatalf("Failed to read: %v", err) | ||
} | ||
if gotStr := string(got); s != gotStr { | ||
t.Fatalf("Failed to get the right content: %s != %s", s, gotStr) | ||
} | ||
|
||
if err = cbs.Close(); err != nil { | ||
t.Fatal(err) | ||
} | ||
} | ||
|
||
func TestCopyBodyStreamFailedRead(t *testing.T) { | ||
s := "content" | ||
bbuf := io.NopCloser(bytes.NewBufferString(s)) | ||
|
||
failingBuf := &failingWriter{buf: &bytes.Buffer{}} | ||
|
||
cbs := NewCopyBodyStream(len(s), failingBuf, bbuf) | ||
|
||
buf := make([]byte, len(s)) | ||
_, err := cbs.Read(buf) | ||
if err == nil { | ||
t.Fatal("Want to have failing buffer write inside Read()") | ||
} | ||
} |
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,22 @@ | ||
package io | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
) | ||
|
||
type failingWriter struct { | ||
buf *bytes.Buffer | ||
} | ||
|
||
func (*failingWriter) Write([]byte) (int, error) { | ||
return 0, fmt.Errorf("failed to write") | ||
} | ||
|
||
func (fw *failingWriter) Read(p []byte) (int, error) { | ||
return fw.buf.Read(p) | ||
} | ||
|
||
func (fw *failingWriter) Len() int { | ||
return fw.buf.Len() | ||
} |
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.