From 9ecc49e59261eb1246a0639b5665d6204212aefb Mon Sep 17 00:00:00 2001 From: James Shubin Date: Tue, 24 Jan 2017 14:11:31 -0500 Subject: [PATCH] resources: Force a sane default for zero value limiting The default UnmarshalYAML on *BaseRes doesn't work properly at the moment, so hack in a default so that we don't need to specify one if the MetaParams struct isn't specified. The problem is that if there isn't a meta value added, its UnmarshalYAML doesn't get a chance to run. --- resources/resources.go | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/resources/resources.go b/resources/resources.go index e27503f7..94cb08fb 100644 --- a/resources/resources.go +++ b/resources/resources.go @@ -206,6 +206,29 @@ type BaseRes struct { //refreshState StatefulBool // TODO: future stateful bool } +// UnmarshalYAML is the custom unmarshal handler for the BaseRes struct. It is +// primarily useful for setting the defaults, in particular if meta is absent! +// FIXME: uncommenting this seems to block the graph from exiting, how come??? +//func (obj *BaseRes) UnmarshalYAML(unmarshal func(interface{}) error) error { +// DefaultBaseRes := BaseRes{ +// // without specifying a default here, if we don't specify *any* +// // meta parameters in the yaml file, then the UnmarshalYAML for +// // the MetaParams struct won't run, and we won't get defaults! +// MetaParams: DefaultMetaParams, // force a default +// } + +// type rawBaseRes BaseRes // indirection to avoid infinite recursion +// raw := rawBaseRes(DefaultBaseRes) // convert; the defaults go here +// //raw := rawBaseRes{} + +// if err := unmarshal(&raw); err != nil { +// return err +// } + +// *obj = BaseRes(raw) // restore from indirection with type conversion! +// return nil +//} + // UIDExistsInUIDs wraps the IFF method when used with a list of UID's. func UIDExistsInUIDs(uid ResUID, uids []ResUID) bool { for _, u := range uids { @@ -263,6 +286,12 @@ func (obj *BaseRes) Init() error { obj.mutex = &sync.Mutex{} obj.events = make(chan *event.Event) // unbuffered chan to avoid stale events obj.started = make(chan struct{}) // closes when started + + // FIXME: force a sane default until UnmarshalYAML on *BaseRes works... + if obj.Meta().Burst == 0 && obj.Meta().Limit == 0 { // blocked + obj.Meta().Limit = rate.Inf + } + //dir, err := obj.VarDir("") //if err != nil { // return errwrap.Wrapf(err, "VarDir failed in Init()")