* middleware/erratic: allow TC bit to be set

Add `truncate` as an option.

Fixes #593
This commit is contained in:
Miek Gieben 2017-04-16 07:49:13 +01:00 committed by GitHub
parent a83d97a5c4
commit 73397e4667
5 changed files with 110 additions and 21 deletions

View file

@ -33,6 +33,8 @@ func setupErratic(c *caddy.Controller) error {
func parseErratic(c *caddy.Controller) (*Erratic, error) {
e := &Erratic{drop: 2}
drop := false // true if we've seen the drop keyword
for c.Next() { // 'erratic'
for c.NextBlock() {
switch c.Val() {
@ -54,6 +56,7 @@ func parseErratic(c *caddy.Controller) (*Erratic, error) {
return nil, fmt.Errorf("illegal amount value given %q", args[0])
}
e.drop = uint64(amount)
drop = true
case "delay":
args := c.RemainingArgs()
if len(args) > 2 {
@ -83,8 +86,30 @@ func parseErratic(c *caddy.Controller) (*Erratic, error) {
}
e.duration = duration
}
case "truncate":
args := c.RemainingArgs()
if len(args) > 1 {
return nil, c.ArgErr()
}
if len(args) == 0 {
continue
}
amount, err := strconv.ParseInt(args[0], 10, 32)
if err != nil {
return nil, err
}
if amount < 0 {
return nil, fmt.Errorf("illegal amount value given %q", args[0])
}
e.truncate = uint64(amount)
}
}
}
if (e.delay > 0 || e.truncate > 0) && !drop { // delay is set, but we've haven't seen a drop keyword, remove default drop stuff
e.drop = 0
}
return e, nil
}