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 2 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
17 changes: 16 additions & 1 deletion middleware/proxy.go
Original file line number Diff line number Diff line change
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 {
GetTarget(echo.Context) (*ProxyTarget, error)
aldas marked this conversation as resolved.
Show resolved Hide resolved
}

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

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

var tgt *ProxyTarget
provider, ok := config.Balancer.(TargetProvider)
aldas marked this conversation as resolved.
Show resolved Hide resolved
if ok {
tgt, err = provider.GetTarget(c)
if err != nil {
return err
}
} else {
tgt = config.Balancer.Next(c)
}
c.Set(config.ContextKey, tgt)

if err := rewriteURL(config.RegexRewrite, req); err != nil {
Expand Down
25 changes: 23 additions & 2 deletions middleware/proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,19 @@ import (
"github.com/stretchr/testify/assert"
)

//Assert expected with url.EscapedPath method to obtain the path.
type testProvider struct {
*commonBalancer
}

func (p *testProvider) Next(c echo.Context) *ProxyTarget {
return p.commonBalancer.targets[0]
}

func (p *testProvider) GetTarget(c echo.Context) (*ProxyTarget, error) {
return p.commonBalancer.targets[1], nil
}

// 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 +43,6 @@ func TestProxy(t *testing.T) {
}))
defer t2.Close()
url2, _ := url.Parse(t2.URL)

targets := []*ProxyTarget{
{
Name: "target 1",
Expand Down Expand Up @@ -89,6 +100,16 @@ func TestProxy(t *testing.T) {
body = rec.Body.String()
assert.Equal(t, "target 2", body)

// Provider
aldas marked this conversation as resolved.
Show resolved Hide resolved
e = echo.New()
tp := &testProvider{commonBalancer: new(commonBalancer)}
tp.targets = targets
e.Use(Proxy(tp))
rec = httptest.NewRecorder()
e.ServeHTTP(rec, req)
body = rec.Body.String()
assert.Equal(t, "target 2", body)

// ModifyResponse
e = echo.New()
e.Use(ProxyWithConfig(ProxyConfig{
Expand Down