-
Notifications
You must be signed in to change notification settings - Fork 323
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* qnd cookie hacking * Add work in progress sticky session support Sticky sessions are set through an HTTP cookie. If the cookie: * is not present, use the next server & set that as sticky * is present, * but is no longer valid, use the next server & set that as sticky * and valid, use that server without advancing .next. * fix misleading comment layer 7, that is... layer 8 is something different (https://en.wikipedia.org/wiki/Layer_8) * Add sticky session support to rebalancing rr * Fix incorrect test to match the actual sticky session actions * Setting the cookie path to '/' * Format and imports * Fix log * Fix error when invalid cookie * fix variables name * refactor: code review.
- Loading branch information
1 parent
5dfac99
commit 856ab51
Showing
5 changed files
with
457 additions
and
32 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
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,56 @@ | ||
// 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(cookieName string) *StickySession { | ||
return &StickySession{cookieName} | ||
} | ||
|
||
// 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 | ||
} | ||
|
||
serverURL, err := url.Parse(cookie.Value) | ||
if err != nil { | ||
return nil, false, err | ||
} | ||
|
||
if s.isBackendAlive(serverURL, servers) { | ||
return serverURL, true, nil | ||
} else { | ||
return nil, false, nil | ||
} | ||
} | ||
|
||
func (s *StickySession) StickBackend(backend *url.URL, w *http.ResponseWriter) { | ||
cookie := &http.Cookie{Name: s.cookieName, Value: backend.String(), Path: "/"} | ||
http.SetCookie(*w, cookie) | ||
} | ||
|
||
func (s *StickySession) isBackendAlive(needle *url.URL, haystack []*url.URL) bool { | ||
if len(haystack) == 0 { | ||
return false | ||
} | ||
|
||
for _, serverURL := range haystack { | ||
if sameURL(needle, serverURL) { | ||
return true | ||
} | ||
} | ||
return false | ||
} |
Oops, something went wrong.