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
read_timeout: 10s
write_timeout: 10s
client_store: memory
ip_store: memory
string_store: memory
peer_store: memory
peer_store_config:
gcAfter: 30m
shards: 1
client_store:
name: memory
ip_store:
name: memory
string_store:
name: memory
peer_store:
name: memory
config:
gcAfter: 30m
shards: 1
- name: prometheus
config:

View file

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

View file

@ -21,7 +21,7 @@ type ClientStore interface {
// ClientStoreDriver represents an interface for creating a handle to the
// storage of swarms.
type ClientStoreDriver interface {
New(*Config) (ClientStore, error)
New(*DriverConfig) (ClientStore, error)
}
// 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.
func OpenClientStore(cfg *Config) (ClientStore, error) {
driver, ok := clientStoreDrivers[cfg.ClientStore]
func OpenClientStore(cfg *DriverConfig) (ClientStore, error) {
driver, ok := clientStoreDrivers[cfg.Name]
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)

View file

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

View file

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

View file

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

View file

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

View file

@ -22,7 +22,7 @@ func init() {
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)
if err != nil {
return nil, err
@ -37,8 +37,8 @@ type peerStoreConfig struct {
Shards int `yaml:"shards"`
}
func newPeerStoreConfig(storecfg *store.Config) (*peerStoreConfig, error) {
bytes, err := yaml.Marshal(storecfg.PeerStoreConfig)
func newPeerStoreConfig(storecfg *store.DriverConfig) (*peerStoreConfig, error) {
bytes, err := yaml.Marshal(storecfg.Config)
if err != nil {
return nil, err
}

View file

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

View file

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

View file

@ -29,7 +29,7 @@ type PeerStore interface {
// PeerStoreDriver represents an interface for creating a handle to the storage
// of peers.
type PeerStoreDriver interface {
New(*Config) (PeerStore, error)
New(*DriverConfig) (PeerStore, error)
}
// 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.
func OpenPeerStore(cfg *Config) (PeerStore, error) {
driver, ok := peerStoreDrivers[cfg.PeerStore]
func OpenPeerStore(cfg *DriverConfig) (PeerStore, error) {
driver, ok := peerStoreDrivers[cfg.Name]
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)

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())
}
cs, err := OpenClientStore(cfg)
cs, err := OpenClientStore(&cfg.ClientStore)
if err != nil {
return nil, err
}
ps, err := OpenPeerStore(cfg)
ps, err := OpenPeerStore(&cfg.PeerStore)
if err != nil {
return nil, err
}
ips, err := OpenIPStore(cfg)
ips, err := OpenIPStore(&cfg.IPStore)
if err != nil {
return nil, err
}
ss, err := OpenStringStore(cfg)
ss, err := OpenStringStore(&cfg.StringStore)
if err != nil {
return nil, err
}
@ -64,19 +64,20 @@ func constructor(srvcfg *chihaya.ServerConfig, tkr *tracker.Tracker) (server.Ser
}
type Config struct {
Addr string `yaml:"addr"`
RequestTimeout time.Duration `yaml:"request_timeout"`
ReadTimeout time.Duration `yaml:"read_timeout"`
WriteTimeout time.Duration `yaml:"write_timeout"`
GCAfter time.Duration `yaml:"gc_after"`
ClientStore string `yaml:"client_store"`
ClientStoreConfig interface{} `yaml:"client_store_config"`
PeerStore string `yaml:"peer_store"`
PeerStoreConfig interface{} `yaml:"peer_store_config"`
IPStore string `yaml:"ip_store"`
IPStoreConfig interface{} `yaml:"ip_store_config"`
StringStore string `yaml:"string_store"`
StringStoreConfig interface{} `yaml:"string_store_config"`
Addr string `yaml:"addr"`
RequestTimeout time.Duration `yaml:"request_timeout"`
ReadTimeout time.Duration `yaml:"read_timeout"`
WriteTimeout time.Duration `yaml:"write_timeout"`
GCAfter time.Duration `yaml:"gc_after"`
ClientStore DriverConfig `yaml:"client_store"`
PeerStore DriverConfig `yaml:"peer_store"`
IPStore DriverConfig `yaml:"ip_store"`
StringStore DriverConfig `yaml:"string_store"`
}
type DriverConfig struct {
Name string `yaml:"name"`
Config interface{} `yaml:"config"`
}
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
// storage of swarms.
type StringStoreDriver interface {
New(*Config) (StringStore, error)
New(*DriverConfig) (StringStore, error)
}
// 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.
func OpenStringStore(cfg *Config) (StringStore, error) {
driver, ok := stringStoreDrivers[cfg.StringStore]
func OpenStringStore(cfg *DriverConfig) (StringStore, error) {
driver, ok := stringStoreDrivers[cfg.Name]
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)