aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorJason Yuan <[email protected]>2024-06-15 09:50:31 -0400
committerGitHub <[email protected]>2024-06-15 07:50:31 -0600
commitfab6375a8bebd952abc80e63fa31b648ae1ebc0b (patch)
treece8f6b30b05a1320e1d34012ed91282450f1a897
parentaca4002fd8ed890f29f74bb7e8d629496f9a6e07 (diff)
downloadcaddy-fab6375a8bebd952abc80e63fa31b648ae1ebc0b.tar.gz
caddy-fab6375a8bebd952abc80e63fa31b648ae1ebc0b.zip
reverseproxy: add Max-Age option to sticky cookie (#6398)
* reverseproxy: add Max-Age option to sticky cookie * Update selectionpolicies.go Co-authored-by: Francis Lavoie <[email protected]> * Update selectionpolicies.go Co-authored-by: Francis Lavoie <[email protected]> --------- Co-authored-by: Francis Lavoie <[email protected]>
-rw-r--r--modules/caddyhttp/reverseproxy/selectionpolicies.go25
1 files changed, 25 insertions, 0 deletions
diff --git a/modules/caddyhttp/reverseproxy/selectionpolicies.go b/modules/caddyhttp/reverseproxy/selectionpolicies.go
index e61b3e0f4..293ff75e2 100644
--- a/modules/caddyhttp/reverseproxy/selectionpolicies.go
+++ b/modules/caddyhttp/reverseproxy/selectionpolicies.go
@@ -26,6 +26,7 @@ import (
"strconv"
"strings"
"sync/atomic"
+ "time"
"github.com/cespare/xxhash/v2"
@@ -613,6 +614,8 @@ type CookieHashSelection struct {
Name string `json:"name,omitempty"`
// Secret to hash (Hmac256) chosen upstream in cookie
Secret string `json:"secret,omitempty"`
+ // The cookie's Max-Age before it expires. Default is no expiry.
+ MaxAge caddy.Duration `json:"max_age,omitempty"`
// The fallback policy to use if the cookie is not present. Defaults to `random`.
FallbackRaw json.RawMessage `json:"fallback,omitempty" caddy:"namespace=http.reverse_proxy.selection_policies inline_key=policy"`
@@ -671,6 +674,9 @@ func (s CookieHashSelection) Select(pool UpstreamPool, req *http.Request, w http
cookie.Secure = true
cookie.SameSite = http.SameSiteNoneMode
}
+ if s.MaxAge > 0 {
+ cookie.MaxAge = int(time.Duration(s.MaxAge).Seconds())
+ }
http.SetCookie(w, cookie)
return upstream
}
@@ -699,6 +705,7 @@ func (s CookieHashSelection) Select(pool UpstreamPool, req *http.Request, w http
//
// lb_policy cookie [<name> [<secret>]] {
// fallback <policy>
+// max_age <duration>
// }
//
// By default name is `lb`
@@ -728,6 +735,24 @@ func (s *CookieHashSelection) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
return err
}
s.FallbackRaw = mod
+ case "max_age":
+ if !d.NextArg() {
+ return d.ArgErr()
+ }
+ if s.MaxAge != 0 {
+ return d.Err("cookie max_age already specified")
+ }
+ maxAge, err := caddy.ParseDuration(d.Val())
+ if err != nil {
+ return d.Errf("invalid duration: %s", d.Val())
+ }
+ if maxAge <= 0 {
+ return d.Errf("invalid duration: %s, max_age should be non-zero and positive", d.Val())
+ }
+ if d.NextArg() {
+ return d.ArgErr()
+ }
+ s.MaxAge = caddy.Duration(maxAge)
default:
return d.Errf("unrecognized option '%s'", d.Val())
}