Merge pull request #138 from mrd0ll4r/config-normalize

Config normalize
This commit is contained in:
Jimmy Zelinskie 2016-03-11 17:12:10 -05:00
commit 05a34c6059
13 changed files with 59 additions and 57 deletions

View file

@ -23,13 +23,17 @@ chihaya:
request_timeout: 10s request_timeout: 10s
read_timeout: 10s read_timeout: 10s
write_timeout: 10s write_timeout: 10s
client_store: memory client_store:
ip_store: memory name: memory
string_store: memory ip_store:
peer_store: memory name: memory
peer_store_config: string_store:
gcAfter: 30m name: memory
shards: 1 peer_store:
name: memory
config:
gcAfter: 30m
shards: 1
- name: prometheus - name: prometheus
config: config:

View file

@ -39,10 +39,7 @@ func Register(name string, con Constructor) {
func New(cfg *chihaya.ServerConfig, tkr *tracker.Tracker) (Server, error) { func New(cfg *chihaya.ServerConfig, tkr *tracker.Tracker) (Server, error) {
con, ok := constructors[cfg.Name] con, ok := constructors[cfg.Name]
if !ok { if !ok {
return nil, fmt.Errorf( return nil, fmt.Errorf("server: unknown Constructor %q (forgotten import?)", cfg.Name)
"server: unknown Constructor %q (forgotten import?)",
cfg.Name,
)
} }
return con(cfg, tkr) return con(cfg, tkr)
} }

View file

@ -21,7 +21,7 @@ type ClientStore interface {
// ClientStoreDriver represents an interface for creating a handle to the // ClientStoreDriver represents an interface for creating a handle to the
// storage of swarms. // storage of swarms.
type ClientStoreDriver interface { type ClientStoreDriver interface {
New(*Config) (ClientStore, error) New(*DriverConfig) (ClientStore, error)
} }
// RegisterClientStoreDriver makes a driver available by the provided name. // RegisterClientStoreDriver makes a driver available by the provided name.
@ -39,10 +39,10 @@ func RegisterClientStoreDriver(name string, driver ClientStoreDriver) {
} }
// OpenClientStore returns a ClientStore specified by a configuration. // OpenClientStore returns a ClientStore specified by a configuration.
func OpenClientStore(cfg *Config) (ClientStore, error) { func OpenClientStore(cfg *DriverConfig) (ClientStore, error) {
driver, ok := clientStoreDrivers[cfg.ClientStore] driver, ok := clientStoreDrivers[cfg.Name]
if !ok { if !ok {
return nil, fmt.Errorf("store: unknown driver %q (forgotten import?)", cfg.ClientStore) return nil, fmt.Errorf("store: unknown ClientStoreDriver %q (forgotten import?)", cfg)
} }
return driver.New(cfg) return driver.New(cfg)

View file

@ -51,7 +51,7 @@ type IPStore interface {
// IPStoreDriver represents an interface for creating a handle to the // IPStoreDriver represents an interface for creating a handle to the
// storage of IPs. // storage of IPs.
type IPStoreDriver interface { type IPStoreDriver interface {
New(*Config) (IPStore, error) New(*DriverConfig) (IPStore, error)
} }
// RegisterIPStoreDriver makes a driver available by the provided name. // RegisterIPStoreDriver makes a driver available by the provided name.
@ -60,19 +60,19 @@ type IPStoreDriver interface {
// it panics. // it panics.
func RegisterIPStoreDriver(name string, driver IPStoreDriver) { func RegisterIPStoreDriver(name string, driver IPStoreDriver) {
if driver == nil { if driver == nil {
panic("store: could not register nil ClientStoreDriver") panic("store: could not register nil IPStoreDriver")
} }
if _, dup := ipStoreDrivers[name]; dup { if _, dup := ipStoreDrivers[name]; dup {
panic("store: could not register duplicate ClientStoreDriver: " + name) panic("store: could not register duplicate IPStoreDriver: " + name)
} }
ipStoreDrivers[name] = driver ipStoreDrivers[name] = driver
} }
// OpenIPStore returns an IPStore specified by a configuration. // OpenIPStore returns an IPStore specified by a configuration.
func OpenIPStore(cfg *Config) (IPStore, error) { func OpenIPStore(cfg *DriverConfig) (IPStore, error) {
driver, ok := ipStoreDrivers[cfg.IPStore] driver, ok := ipStoreDrivers[cfg.Name]
if !ok { if !ok {
return nil, fmt.Errorf("store: unknown driver %q (forgotten import?)", cfg.IPStore) return nil, fmt.Errorf("store: unknown IPStoreDriver %q (forgotten import?)", cfg)
} }
return driver.New(cfg) return driver.New(cfg)

View file

@ -18,7 +18,7 @@ func init() {
type clientStoreDriver struct{} type clientStoreDriver struct{}
func (d *clientStoreDriver) New(cfg *store.Config) (store.ClientStore, error) { func (d *clientStoreDriver) New(_ *store.DriverConfig) (store.ClientStore, error) {
return &clientStore{ return &clientStore{
clientIDs: make(map[string]struct{}), clientIDs: make(map[string]struct{}),
}, nil }, nil

View file

@ -19,7 +19,7 @@ func init() {
type ipStoreDriver struct{} type ipStoreDriver struct{}
func (d *ipStoreDriver) New(cfg *store.Config) (store.IPStore, error) { func (d *ipStoreDriver) New(_ *store.DriverConfig) (store.IPStore, error) {
return &ipStore{ return &ipStore{
ips: make(map[[16]byte]struct{}), ips: make(map[[16]byte]struct{}),
networks: netmatch.New(), networks: netmatch.New(),

View file

@ -37,7 +37,7 @@ func TestKey(t *testing.T) {
func TestIPStore(t *testing.T) { func TestIPStore(t *testing.T) {
var d = &ipStoreDriver{} var d = &ipStoreDriver{}
s, err := d.New(&store.Config{}) s, err := d.New(&store.DriverConfig{})
assert.Nil(t, err) assert.Nil(t, err)
assert.NotNil(t, s) assert.NotNil(t, s)
@ -91,7 +91,7 @@ func TestIPStore(t *testing.T) {
func TestHasAllHasAny(t *testing.T) { func TestHasAllHasAny(t *testing.T) {
var d = &ipStoreDriver{} var d = &ipStoreDriver{}
s, err := d.New(&store.Config{}) s, err := d.New(&store.DriverConfig{})
assert.Nil(t, err) assert.Nil(t, err)
assert.NotNil(t, s) assert.NotNil(t, s)
@ -143,7 +143,7 @@ func TestNetworks(t *testing.T) {
excludedIP = net.ParseIP("192.168.23.22") excludedIP = net.ParseIP("192.168.23.22")
) )
s, err := d.New(&store.Config{}) s, err := d.New(&store.DriverConfig{})
assert.Nil(t, err) assert.Nil(t, err)
match, err := s.HasIP(includedIP) match, err := s.HasIP(includedIP)
@ -195,7 +195,7 @@ func TestHasAllHasAnyNetworks(t *testing.T) {
inNet2 = net.ParseIP("192.168.23.123") inNet2 = net.ParseIP("192.168.23.123")
excluded = net.ParseIP("10.154.243.22") excluded = net.ParseIP("10.154.243.22")
) )
s, err := d.New(&store.Config{}) s, err := d.New(&store.DriverConfig{})
assert.Nil(t, err) assert.Nil(t, err)
match, err := s.HasAnyIP([]net.IP{inNet1, inNet2, excluded}) match, err := s.HasAnyIP([]net.IP{inNet1, inNet2, excluded})

View file

@ -22,7 +22,7 @@ func init() {
type peerStoreDriver struct{} type peerStoreDriver struct{}
func (d *peerStoreDriver) New(storecfg *store.Config) (store.PeerStore, error) { func (d *peerStoreDriver) New(storecfg *store.DriverConfig) (store.PeerStore, error) {
cfg, err := newPeerStoreConfig(storecfg) cfg, err := newPeerStoreConfig(storecfg)
if err != nil { if err != nil {
return nil, err return nil, err
@ -37,8 +37,8 @@ type peerStoreConfig struct {
Shards int `yaml:"shards"` Shards int `yaml:"shards"`
} }
func newPeerStoreConfig(storecfg *store.Config) (*peerStoreConfig, error) { func newPeerStoreConfig(storecfg *store.DriverConfig) (*peerStoreConfig, error) {
bytes, err := yaml.Marshal(storecfg.PeerStoreConfig) bytes, err := yaml.Marshal(storecfg.Config)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View file

@ -16,7 +16,7 @@ func init() {
type stringStoreDriver struct{} type stringStoreDriver struct{}
func (d *stringStoreDriver) New(cfg *store.Config) (store.StringStore, error) { func (d *stringStoreDriver) New(_ *store.DriverConfig) (store.StringStore, error) {
return &stringStore{ return &stringStore{
strings: make(map[string]struct{}), strings: make(map[string]struct{}),
}, nil }, nil

View file

@ -17,7 +17,7 @@ var (
) )
func TestStringStore(t *testing.T) { func TestStringStore(t *testing.T) {
ss, err := driver.New(&store.Config{}) ss, err := driver.New(&store.DriverConfig{})
assert.Nil(t, err) assert.Nil(t, err)
assert.NotNil(t, ss) assert.NotNil(t, ss)

View file

@ -29,7 +29,7 @@ type PeerStore interface {
// PeerStoreDriver represents an interface for creating a handle to the storage // PeerStoreDriver represents an interface for creating a handle to the storage
// of peers. // of peers.
type PeerStoreDriver interface { type PeerStoreDriver interface {
New(*Config) (PeerStore, error) New(*DriverConfig) (PeerStore, error)
} }
// RegisterPeerStoreDriver makes a driver available by the provided name. // RegisterPeerStoreDriver makes a driver available by the provided name.
@ -49,10 +49,10 @@ func RegisterPeerStoreDriver(name string, driver PeerStoreDriver) {
} }
// OpenPeerStore returns a PeerStore specified by a configuration. // OpenPeerStore returns a PeerStore specified by a configuration.
func OpenPeerStore(cfg *Config) (PeerStore, error) { func OpenPeerStore(cfg *DriverConfig) (PeerStore, error) {
driver, ok := peerStoreDrivers[cfg.PeerStore] driver, ok := peerStoreDrivers[cfg.Name]
if !ok { if !ok {
return nil, fmt.Errorf("storage: unknown driver %q (forgotten import?)", cfg.PeerStore) return nil, fmt.Errorf("storage: unknown PeerStoreDriver %q (forgotten import?)", cfg)
} }
return driver.New(cfg) return driver.New(cfg)

View file

@ -30,22 +30,22 @@ func constructor(srvcfg *chihaya.ServerConfig, tkr *tracker.Tracker) (server.Ser
return nil, errors.New("store: invalid store config: " + err.Error()) return nil, errors.New("store: invalid store config: " + err.Error())
} }
cs, err := OpenClientStore(cfg) cs, err := OpenClientStore(&cfg.ClientStore)
if err != nil { if err != nil {
return nil, err return nil, err
} }
ps, err := OpenPeerStore(cfg) ps, err := OpenPeerStore(&cfg.PeerStore)
if err != nil { if err != nil {
return nil, err return nil, err
} }
ips, err := OpenIPStore(cfg) ips, err := OpenIPStore(&cfg.IPStore)
if err != nil { if err != nil {
return nil, err return nil, err
} }
ss, err := OpenStringStore(cfg) ss, err := OpenStringStore(&cfg.StringStore)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -64,19 +64,20 @@ func constructor(srvcfg *chihaya.ServerConfig, tkr *tracker.Tracker) (server.Ser
} }
type Config struct { type Config struct {
Addr string `yaml:"addr"` Addr string `yaml:"addr"`
RequestTimeout time.Duration `yaml:"request_timeout"` RequestTimeout time.Duration `yaml:"request_timeout"`
ReadTimeout time.Duration `yaml:"read_timeout"` ReadTimeout time.Duration `yaml:"read_timeout"`
WriteTimeout time.Duration `yaml:"write_timeout"` WriteTimeout time.Duration `yaml:"write_timeout"`
GCAfter time.Duration `yaml:"gc_after"` GCAfter time.Duration `yaml:"gc_after"`
ClientStore string `yaml:"client_store"` ClientStore DriverConfig `yaml:"client_store"`
ClientStoreConfig interface{} `yaml:"client_store_config"` PeerStore DriverConfig `yaml:"peer_store"`
PeerStore string `yaml:"peer_store"` IPStore DriverConfig `yaml:"ip_store"`
PeerStoreConfig interface{} `yaml:"peer_store_config"` StringStore DriverConfig `yaml:"string_store"`
IPStore string `yaml:"ip_store"` }
IPStoreConfig interface{} `yaml:"ip_store_config"`
StringStore string `yaml:"string_store"` type DriverConfig struct {
StringStoreConfig interface{} `yaml:"string_store_config"` Name string `yaml:"name"`
Config interface{} `yaml:"config"`
} }
func newConfig(srvcfg *chihaya.ServerConfig) (*Config, error) { func newConfig(srvcfg *chihaya.ServerConfig) (*Config, error) {

View file

@ -21,7 +21,7 @@ type StringStore interface {
// StringStoreDriver represents an interface for creating a handle to the // StringStoreDriver represents an interface for creating a handle to the
// storage of swarms. // storage of swarms.
type StringStoreDriver interface { type StringStoreDriver interface {
New(*Config) (StringStore, error) New(*DriverConfig) (StringStore, error)
} }
// RegisterStringStoreDriver makes a driver available by the provided name. // RegisterStringStoreDriver makes a driver available by the provided name.
@ -39,10 +39,10 @@ func RegisterStringStoreDriver(name string, driver StringStoreDriver) {
} }
// OpenStringStore returns a StringStore specified by a configuration. // OpenStringStore returns a StringStore specified by a configuration.
func OpenStringStore(cfg *Config) (StringStore, error) { func OpenStringStore(cfg *DriverConfig) (StringStore, error) {
driver, ok := stringStoreDrivers[cfg.StringStore] driver, ok := stringStoreDrivers[cfg.Name]
if !ok { if !ok {
return nil, fmt.Errorf("store: unknown driver %q (forgotten import?)", cfg.StringStore) return nil, fmt.Errorf("store: unknown StringStoreDriver %q (forgotten import?)", cfg)
} }
return driver.New(cfg) return driver.New(cfg)