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

13
examples/file5.yaml Normal file
View File

@@ -0,0 +1,13 @@
---
graph: mygraph
resources:
file:
- name: file1
meta:
limit: .inf
burst: 0
path: "/tmp/mgmt/hello"
content: |
i am a file
state: exists
edges: []

View File

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