-
Notifications
You must be signed in to change notification settings - Fork 351
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
9 changed files
with
680 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,48 @@ | ||
package io | ||
|
||
import ( | ||
"bytes" | ||
"io" | ||
) | ||
|
||
type bodyBuffer struct{ *bytes.Buffer } | ||
|
||
func (buf *bodyBuffer) Close() error { | ||
return nil | ||
} | ||
|
||
type CopyBodyStream struct { | ||
left int | ||
buf *bodyBuffer | ||
input io.ReadCloser | ||
} | ||
|
||
func NewCopyBodyStream(left int, buf *bytes.Buffer, rc io.ReadCloser) *CopyBodyStream { | ||
return &CopyBodyStream{ | ||
left: left, | ||
buf: &bodyBuffer{Buffer: 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) | ||
cb.buf.Write(p[:m]) | ||
cb.left -= m | ||
} | ||
return n, err | ||
} | ||
|
||
func (cb *CopyBodyStream) Close() error { | ||
return cb.input.Close() | ||
} | ||
|
||
func (cb *CopyBodyStream) GetBody() io.ReadCloser { | ||
return 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,41 @@ | ||
package io | ||
|
||
import ( | ||
"bytes" | ||
"io" | ||
"testing" | ||
) | ||
|
||
type tbuf struct{ *bytes.Buffer } | ||
|
||
func (tb *tbuf) Read(p []byte) (int, error) { | ||
return tb.Buffer.Read(p) | ||
} | ||
func (tb *tbuf) Close() error { | ||
return nil | ||
} | ||
|
||
func TestCopyBodyStream(t *testing.T) { | ||
s := "content" | ||
bbuf := &tbuf{bytes.NewBufferString(s)} | ||
cbs := NewCopyBodyStream(bbuf.Len(), &bytes.Buffer{}, bbuf) | ||
|
||
buf := make([]byte, len(s)) | ||
cbs.Read(buf) | ||
|
||
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) | ||
} | ||
} |
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.