From 7be747fbe922573269cdaf83f15d1ec2245d257c Mon Sep 17 00:00:00 2001 From: Francis Lavoie Date: Mon, 6 Apr 2020 15:08:42 -0400 Subject: [PATCH] caddyhttp: Add missing LB policy Caddyfile unmarshalers (#3230) --- .../reverseproxy/selectionpolicies.go | 71 +++++++++++++++++++ 1 file changed, 71 insertions(+) diff --git a/modules/caddyhttp/reverseproxy/selectionpolicies.go b/modules/caddyhttp/reverseproxy/selectionpolicies.go index 330dbdc99..8a3213524 100644 --- a/modules/caddyhttp/reverseproxy/selectionpolicies.go +++ b/modules/caddyhttp/reverseproxy/selectionpolicies.go @@ -74,6 +74,16 @@ func (r RandomSelection) Select(pool UpstreamPool, request *http.Request) *Upstr return randomHost } +// UnmarshalCaddyfile sets up the module from Caddyfile tokens. +func (r *RandomSelection) UnmarshalCaddyfile(d *caddyfile.Dispenser) error { + for d.Next() { + if d.NextArg() { + return d.ArgErr() + } + } + return nil +} + // RandomChoiceSelection is a policy that selects // two or more available hosts at random, then // chooses the one with the least load. @@ -192,6 +202,16 @@ func (LeastConnSelection) Select(pool UpstreamPool, _ *http.Request) *Upstream { return bestHost } +// UnmarshalCaddyfile sets up the module from Caddyfile tokens. +func (r *LeastConnSelection) UnmarshalCaddyfile(d *caddyfile.Dispenser) error { + for d.Next() { + if d.NextArg() { + return d.ArgErr() + } + } + return nil +} + // RoundRobinSelection is a policy that selects // a host based on round-robin ordering. type RoundRobinSelection struct { @@ -222,6 +242,16 @@ func (r *RoundRobinSelection) Select(pool UpstreamPool, _ *http.Request) *Upstre return nil } +// UnmarshalCaddyfile sets up the module from Caddyfile tokens. +func (r *RoundRobinSelection) UnmarshalCaddyfile(d *caddyfile.Dispenser) error { + for d.Next() { + if d.NextArg() { + return d.ArgErr() + } + } + return nil +} + // FirstSelection is a policy that selects // the first available host. type FirstSelection struct{} @@ -244,6 +274,16 @@ func (FirstSelection) Select(pool UpstreamPool, _ *http.Request) *Upstream { return nil } +// UnmarshalCaddyfile sets up the module from Caddyfile tokens. +func (r *FirstSelection) UnmarshalCaddyfile(d *caddyfile.Dispenser) error { + for d.Next() { + if d.NextArg() { + return d.ArgErr() + } + } + return nil +} + // IPHashSelection is a policy that selects a host // based on hashing the remote IP of the request. type IPHashSelection struct{} @@ -265,6 +305,16 @@ func (IPHashSelection) Select(pool UpstreamPool, req *http.Request) *Upstream { return hostByHashing(pool, clientIP) } +// UnmarshalCaddyfile sets up the module from Caddyfile tokens. +func (r *IPHashSelection) UnmarshalCaddyfile(d *caddyfile.Dispenser) error { + for d.Next() { + if d.NextArg() { + return d.ArgErr() + } + } + return nil +} + // URIHashSelection is a policy that selects a // host by hashing the request URI. type URIHashSelection struct{} @@ -282,6 +332,16 @@ func (URIHashSelection) Select(pool UpstreamPool, req *http.Request) *Upstream { return hostByHashing(pool, req.RequestURI) } +// UnmarshalCaddyfile sets up the module from Caddyfile tokens. +func (r *URIHashSelection) UnmarshalCaddyfile(d *caddyfile.Dispenser) error { + for d.Next() { + if d.NextArg() { + return d.ArgErr() + } + } + return nil +} + // HeaderHashSelection is a policy that selects // a host based on a given request header. type HeaderHashSelection struct { @@ -309,6 +369,17 @@ func (s HeaderHashSelection) Select(pool UpstreamPool, req *http.Request) *Upstr return hostByHashing(pool, val) } +// UnmarshalCaddyfile sets up the module from Caddyfile tokens. +func (r *HeaderHashSelection) UnmarshalCaddyfile(d *caddyfile.Dispenser) error { + for d.Next() { + if !d.NextArg() { + return d.ArgErr() + } + r.Field = d.Val() + } + return nil +} + // leastRequests returns the host with the // least number of active requests to it. // If more than one host has the same