diff options
author | Matthew Holt <[email protected]> | 2019-11-04 12:54:46 -0700 |
---|---|---|
committer | Matthew Holt <[email protected]> | 2019-11-04 12:54:46 -0700 |
commit | bf363f061d03f33a8301466c9c0e2a89d75542c0 (patch) | |
tree | 496009e72f51f12153498316ffb48f67ffdedc28 /modules/caddyhttp/reverseproxy/selectionpolicies.go | |
parent | 7129f6c1c00103a403717a68a0d91965b818db5b (diff) | |
download | caddy-bf363f061d03f33a8301466c9c0e2a89d75542c0.tar.gz caddy-bf363f061d03f33a8301466c9c0e2a89d75542c0.zip |
reverse_proxy: Add UnmarshalCaddyfile for random_choose selection policy
Also allow caddy.Duration to be given integer values which are treated
like regular time.Duration values (nanoseconds).
Fixes #2856
Diffstat (limited to 'modules/caddyhttp/reverseproxy/selectionpolicies.go')
-rw-r--r-- | modules/caddyhttp/reverseproxy/selectionpolicies.go | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/modules/caddyhttp/reverseproxy/selectionpolicies.go b/modules/caddyhttp/reverseproxy/selectionpolicies.go index 5bb2d62dc..e34dd9b3e 100644 --- a/modules/caddyhttp/reverseproxy/selectionpolicies.go +++ b/modules/caddyhttp/reverseproxy/selectionpolicies.go @@ -20,10 +20,12 @@ import ( weakrand "math/rand" "net" "net/http" + "strconv" "sync/atomic" "time" "github.com/caddyserver/caddy/v2" + "github.com/caddyserver/caddy/v2/caddyconfig/caddyfile" ) func init() { @@ -87,6 +89,22 @@ func (RandomChoiceSelection) CaddyModule() caddy.ModuleInfo { } } +// UnmarshalCaddyfile sets up the module from Caddyfile tokens. +func (r *RandomChoiceSelection) UnmarshalCaddyfile(d *caddyfile.Dispenser) error { + for d.Next() { + if !d.NextArg() { + return d.ArgErr() + } + chooseStr := d.Val() + choose, err := strconv.Atoi(chooseStr) + if err != nil { + return d.Errf("invalid choice value '%s': %v", chooseStr, err) + } + r.Choose = choose + } + return nil +} + // Provision sets up r. func (r *RandomChoiceSelection) Provision(ctx caddy.Context) error { if r.Choose == 0 { @@ -350,4 +368,6 @@ var ( _ caddy.Validator = (*RandomChoiceSelection)(nil) _ caddy.Provisioner = (*RandomChoiceSelection)(nil) + + _ caddyfile.Unmarshaler = (*RandomChoiceSelection)(nil) ) |