1
0
Fork 0

Set sameSite field for wrr load balancer sticky cookie

This commit is contained in:
Yakun Sun 2023-08-08 21:12:06 +08:00 committed by GitHub
parent ca2b9e8e77
commit d6457e6cbb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 41 additions and 3 deletions

View file

@ -22,6 +22,20 @@ type stickyCookie struct {
name string
secure bool
httpOnly bool
sameSite string
}
func convertSameSite(sameSite string) http.SameSite {
switch sameSite {
case "none":
return http.SameSiteNoneMode
case "lax":
return http.SameSiteLaxMode
case "strict":
return http.SameSiteStrictMode
default:
return http.SameSiteDefaultMode
}
}
// Balancer is a WeightedRoundRobin load balancer based on Earliest Deadline First (EDF).
@ -57,6 +71,7 @@ func New(sticky *dynamic.Sticky, wantHealthCheck bool) *Balancer {
name: sticky.Cookie.Name,
secure: sticky.Cookie.Secure,
httpOnly: sticky.Cookie.HTTPOnly,
sameSite: sticky.Cookie.SameSite,
}
}
return balancer
@ -214,7 +229,14 @@ func (b *Balancer) ServeHTTP(w http.ResponseWriter, req *http.Request) {
}
if b.stickyCookie != nil {
cookie := &http.Cookie{Name: b.stickyCookie.name, Value: server.name, Path: "/", HttpOnly: b.stickyCookie.httpOnly, Secure: b.stickyCookie.secure}
cookie := &http.Cookie{
Name: b.stickyCookie.name,
Value: server.name,
Path: "/",
HttpOnly: b.stickyCookie.httpOnly,
Secure: b.stickyCookie.secure,
SameSite: convertSameSite(b.stickyCookie.sameSite),
}
http.SetCookie(w, cookie)
}