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

Pre request rewrite listener #211

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
25 changes: 20 additions & 5 deletions roundrobin/rr.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,26 @@ func RoundRobinRequestRewriteListener(rrl RequestRewriteListener) LBOption {
}
}

// RoundRobinPreRequestRewriteListener is a functional argument that sets error handler of the server
func RoundRobinPreRequestRewriteListener(rrl RequestRewriteListener) LBOption {
return func(s *RoundRobin) error {
s.requestPreRewriteListener = rrl
return nil
}
}

// RoundRobin implements dynamic weighted round robin load balancer http handler
type RoundRobin struct {
mutex *sync.Mutex
next http.Handler
errHandler utils.ErrorHandler
// Current index (starts from -1)
index int
servers []*server
currentWeight int
stickySession *StickySession
requestRewriteListener RequestRewriteListener
index int
servers []*server
currentWeight int
stickySession *StickySession
requestPreRewriteListener RequestRewriteListener
requestRewriteListener RequestRewriteListener

log *log.Logger
}
Expand Down Expand Up @@ -107,6 +116,11 @@ func (r *RoundRobin) ServeHTTP(w http.ResponseWriter, req *http.Request) {

// make shallow copy of request before chaning anything to avoid side effects
newReq := *req
// Emit event to a listener if one exists
if r.requestPreRewriteListener != nil {
r.requestPreRewriteListener(req, &newReq)
}

stuck := false
if r.stickySession != nil {
cookieURL, present, err := r.stickySession.GetBackend(&newReq, r.Servers())
Expand All @@ -116,6 +130,7 @@ func (r *RoundRobin) ServeHTTP(w http.ResponseWriter, req *http.Request) {
}

if present {
r.stickySession.StickBackend(cookieURL, &w)
newReq.URL = cookieURL
stuck = true
}
Expand Down
17 changes: 17 additions & 0 deletions roundrobin/rr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,23 @@ func TestRequestRewriteListener(t *testing.T) {
assert.NotNil(t, lb.requestRewriteListener)
}

func TestPreRequestRewriteListener(t *testing.T) {
a := testutils.NewResponder("a")
defer a.Close()

b := testutils.NewResponder("b")
defer b.Close()

fwd, err := forward.New()
require.NoError(t, err)

lb, err := New(fwd,
RoundRobinPreRequestRewriteListener(func(oldReq *http.Request, newReq *http.Request) {}))
require.NoError(t, err)

assert.NotNil(t, lb.requestPreRewriteListener)
}

func seq(t *testing.T, url string, repeat int) []string {
var out []string
for i := 0; i < repeat; i++ {
Expand Down