Last active
October 13, 2020 15:04
-
-
Save pior/581d900e75057e22a905d8f466d2896b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "strconv" | |
/////// pkg/dynconfig/key.go | |
type FloatKey struct { | |
name string | |
fallback string | |
} | |
func (k FloatKey) load(input string) (interface{}, error) { | |
return strconv.ParseFloat(input, 64) | |
} | |
/////// pkg/dynconfig/register.go | |
type Key interface { | |
load(input string) (interface{}, error) | |
} | |
type register struct { | |
m map[string]Key | |
} | |
func NewRegister() *register { | |
return ®ister{ | |
m: make(map[string]Key), | |
} | |
} | |
func (r *register) Float(name string, fallback string) FloatKey { | |
// check name | |
// check presence | |
key := FloatKey{name: name, fallback: fallback} | |
r.m[name] = &key | |
return key | |
} | |
/////// pkg/dynconfig/dynkeys/keys.go | |
var Register = NewRegister() | |
var RolloutProductionInternal = Register.Float("rollout.production.internal", "0.4") | |
/////// pkg/dynconfig/service.go | |
type FetchService struct { | |
store interface { | |
Load(name string) (string, error) | |
} | |
} | |
func (s *FetchService) get(name string, fallback string) string { | |
value, _ := s.store.Load(name) | |
// cache the value, | |
// fallback for error | |
// metrics | |
return value | |
} | |
/////// pkg/dynconfig/values.go | |
func Float(service *FetchService, key FloatKey) FloatValue { | |
return FloatValue{service, key} | |
} | |
type FloatValue struct { | |
service *FetchService | |
key FloatKey | |
} | |
func (v *FloatValue) Value() float64 { | |
str := v.service.get(v.key.name, v.key.fallback) | |
result, err := v.key.load(str) | |
if err != nil { | |
result, _ = v.key.load(v.key.fallback) | |
} | |
return result.(float64) | |
} | |
/////// pkg/some/client.go | |
type Client struct { | |
rollout FloatValue | |
} | |
func NewClient(dynservice *FetchService) *Client { | |
return &Client{ | |
rollout: Float(dynservice, RolloutProductionInternal), | |
} | |
} | |
func (c *Client) Run() { | |
for { | |
if c.rollout.Value() > 0.04 { | |
// ... | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment