This repository has been archived by the owner on Feb 15, 2019. It is now read-only.
forked from vulcand/oxy
-
Notifications
You must be signed in to change notification settings - Fork 17
Add sticky session support to containous/oxy's round robin #4
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,57 @@ | ||
// package stickysession is a mixin for load balancers that implements layer 7 (http cookie) session affinity | ||
package roundrobin | ||
|
||
import ( | ||
"net/http" | ||
"net/url" | ||
) | ||
|
||
type StickySession struct { | ||
cookiename string | ||
} | ||
|
||
func NewStickySession(c string) *StickySession { | ||
return &StickySession{c} | ||
} | ||
|
||
// GetBackend returns the backend URL stored in the sticky cookie, iff the backend is still in the valid list of servers. | ||
func (s *StickySession) GetBackend(req *http.Request, servers []*url.URL) (*url.URL, bool, error) { | ||
cookie, err := req.Cookie(s.cookiename) | ||
switch err { | ||
case nil: | ||
case http.ErrNoCookie: | ||
return nil, false, nil | ||
default: | ||
return nil, false, err | ||
} | ||
|
||
s_url, err := url.Parse(cookie.Value) | ||
if err != nil { | ||
return nil, false, err | ||
} | ||
|
||
if s.isBackendAlive(s_url, servers) { | ||
return s_url, true, nil | ||
} else { | ||
return nil, false, nil | ||
} | ||
} | ||
|
||
func (s *StickySession) StickBackend(backend *url.URL, w *http.ResponseWriter) { | ||
c := &http.Cookie{Name: s.cookiename, Value: backend.String()} | ||
http.SetCookie(*w, c) | ||
return | ||
} | ||
|
||
func (s *StickySession) isBackendAlive(needle *url.URL, haystack []*url.URL) bool { | ||
if len(haystack) == 0 { | ||
return false | ||
} | ||
|
||
for _, s := range haystack { | ||
if sameURL(needle, s) { | ||
return true | ||
} | ||
} | ||
return false | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hum, there is an issue here I think.
You don't call
url, err := r.NextServer()
ifstuck
is false.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Glad I saw your comment in my other PR, I didn't see this comment!
I've retraced the logic of that section. The flow is this:
** if so, pull the server out of the cookie and store it.
** if not, fall through to the next session
** call NextServer() to get the approp. URL
** if we have sticky sessions, stick that backend
So we only call NextServer() if we have failed to retrieve a server URL from the cookie - that's in the
if !stuck {
stanza. We won't call it ifstuck = true
, which is only if we have retrieved the server from the cookie -- and, validated that it is still a valid server, which we handle inss.GetBackend
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@emilevauge let me know if that clears it up! alternatively, if I'm missing something here - which I may be ;) - let me know and I can revise