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

[suggestion] Add helper interface for ProxyBalancer interface #2316

Merged
merged 6 commits into from Oct 29, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 17 additions & 1 deletion middleware/proxy.go
Expand Up @@ -72,6 +72,11 @@ type (
Next(echo.Context) *ProxyTarget
}

// TargetProvider defines an interface that gives the opportunity for balancer to return custom errors when selecting target.
TargetProvider interface {
NextTarget(echo.Context) (*ProxyTarget, error)
}

commonBalancer struct {
targets []*ProxyTarget
mutex sync.RWMutex
Expand Down Expand Up @@ -223,6 +228,7 @@ func ProxyWithConfig(config ProxyConfig) echo.MiddlewareFunc {
}
}

provider, isTargetProvider := config.Balancer.(TargetProvider)
return func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) (err error) {
if config.Skipper(c) {
Expand All @@ -231,7 +237,17 @@ func ProxyWithConfig(config ProxyConfig) echo.MiddlewareFunc {

req := c.Request()
res := c.Response()
tgt := config.Balancer.Next(c)

var tgt *ProxyTarget
if isTargetProvider {
tgt, err = provider.NextTarget(c)
if err != nil {
c.String(http.StatusInternalServerError, err.Error())
aldas marked this conversation as resolved.
Show resolved Hide resolved
return
}
} else {
tgt = config.Balancer.Next(c)
}
c.Set(config.ContextKey, tgt)

if err := rewriteURL(config.RegexRewrite, req); err != nil {
Expand Down
53 changes: 51 additions & 2 deletions middleware/proxy_test.go
Expand Up @@ -18,7 +18,7 @@ import (
"github.com/stretchr/testify/assert"
)

//Assert expected with url.EscapedPath method to obtain the path.
// Assert expected with url.EscapedPath method to obtain the path.
func TestProxy(t *testing.T) {
// Setup
t1 := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
Expand All @@ -31,7 +31,6 @@ func TestProxy(t *testing.T) {
}))
defer t2.Close()
url2, _ := url.Parse(t2.URL)

targets := []*ProxyTarget{
{
Name: "target 1",
Expand Down Expand Up @@ -122,6 +121,56 @@ func TestProxy(t *testing.T) {
e.ServeHTTP(rec, req)
}

type testProvider struct {
*commonBalancer
target *ProxyTarget
err error
}

func (p *testProvider) Next(c echo.Context) *ProxyTarget {
return &ProxyTarget{}
}

func (p *testProvider) NextTarget(c echo.Context) (*ProxyTarget, error) {
return p.target, p.err
}

func TestTargetProvider(t *testing.T) {
t1 := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "target 1")
}))
defer t1.Close()
url1, _ := url.Parse(t1.URL)

e := echo.New()
tp := &testProvider{commonBalancer: new(commonBalancer)}
tp.target = &ProxyTarget{Name: "target 1", URL: url1}
e.Use(Proxy(tp))
rec := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodGet, "/", nil)
e.ServeHTTP(rec, req)
body := rec.Body.String()
assert.Equal(t, "target 1", body)
}

func TestFailNextTarget(t *testing.T) {
url1, err := url.Parse("http://dummy:8080")
assert.Nil(t, err)

e := echo.New()
msg := "method could not select target"
tp := &testProvider{commonBalancer: new(commonBalancer)}
tp.target = &ProxyTarget{Name: "target 1", URL: url1}
tp.err = fmt.Errorf(msg)

e.Use(Proxy(tp))
rec := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodGet, "/", nil)
e.ServeHTTP(rec, req)
body := rec.Body.String()
assert.Equal(t, msg, body)
}

func TestProxyRealIPHeader(t *testing.T) {
// Setup
upstream := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}))
Expand Down