resources: Parse YAML infinity specifications correctly

This makes it easier to specify an infinite rate.
This commit is contained in:
James Shubin
2017-02-03 17:39:51 -05:00
parent 2aff8709a5
commit 68a8649292
2 changed files with 19 additions and 1 deletions

View File

@@ -24,6 +24,7 @@ import (
"encoding/gob"
"fmt"
"log"
"math"
"os"
"path"
"sync"
@@ -272,7 +273,8 @@ func (obj *BaseUID) Reversed() bool {
// Validate reports any problems with the struct definition.
func (obj *BaseRes) Validate() error {
if obj.Meta().Burst == 0 && !(obj.Meta().Limit == rate.Inf) { // blocked
isInf := obj.Meta().Limit == rate.Inf || math.IsInf(float64(obj.Meta().Limit), 1)
if obj.Meta().Burst == 0 && !isInf { // blocked
return fmt.Errorf("Permanently limited (rate != Inf, burst: 0)")
}
return nil
@@ -291,6 +293,9 @@ func (obj *BaseRes) Init() error {
if obj.Meta().Burst == 0 && obj.Meta().Limit == 0 { // blocked
obj.Meta().Limit = rate.Inf
}
if math.IsInf(float64(obj.Meta().Limit), 1) { // yaml `.inf` -> rate.Inf
obj.Meta().Limit = rate.Inf
}
//dir, err := obj.VarDir("")
//if err != nil {