mirror of
https://github.com/caddyserver/caddy.git
synced 2024-12-27 14:13:48 +03:00
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
This commit is contained in:
parent
7129f6c1c0
commit
bf363f061d
2 changed files with 31 additions and 5 deletions
16
caddy.go
16
caddy.go
|
@ -437,12 +437,18 @@ type Duration time.Duration
|
||||||
|
|
||||||
// UnmarshalJSON satisfies json.Unmarshaler.
|
// UnmarshalJSON satisfies json.Unmarshaler.
|
||||||
func (d *Duration) UnmarshalJSON(b []byte) error {
|
func (d *Duration) UnmarshalJSON(b []byte) error {
|
||||||
dd, err := time.ParseDuration(strings.Trim(string(b), `"`))
|
if len(b) == 0 {
|
||||||
if err != nil {
|
return io.EOF
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
*d = Duration(dd)
|
var dur time.Duration
|
||||||
return nil
|
var err error
|
||||||
|
if b[0] == byte('"') && b[len(b)-1] == byte('"') {
|
||||||
|
dur, err = time.ParseDuration(strings.Trim(string(b), `"`))
|
||||||
|
} else {
|
||||||
|
err = json.Unmarshal(b, &dur)
|
||||||
|
}
|
||||||
|
*d = Duration(dur)
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// GoModule returns the build info of this Caddy
|
// GoModule returns the build info of this Caddy
|
||||||
|
|
|
@ -20,10 +20,12 @@ import (
|
||||||
weakrand "math/rand"
|
weakrand "math/rand"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"strconv"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/caddyserver/caddy/v2"
|
"github.com/caddyserver/caddy/v2"
|
||||||
|
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
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.
|
// Provision sets up r.
|
||||||
func (r *RandomChoiceSelection) Provision(ctx caddy.Context) error {
|
func (r *RandomChoiceSelection) Provision(ctx caddy.Context) error {
|
||||||
if r.Choose == 0 {
|
if r.Choose == 0 {
|
||||||
|
@ -350,4 +368,6 @@ var (
|
||||||
|
|
||||||
_ caddy.Validator = (*RandomChoiceSelection)(nil)
|
_ caddy.Validator = (*RandomChoiceSelection)(nil)
|
||||||
_ caddy.Provisioner = (*RandomChoiceSelection)(nil)
|
_ caddy.Provisioner = (*RandomChoiceSelection)(nil)
|
||||||
|
|
||||||
|
_ caddyfile.Unmarshaler = (*RandomChoiceSelection)(nil)
|
||||||
)
|
)
|
||||||
|
|
Loading…
Reference in a new issue