From 8f6a97297048bd9711948ddfdd91e4476dc3da5c Mon Sep 17 00:00:00 2001 From: Darien Raymond Date: Wed, 21 Sep 2016 13:52:16 +0200 Subject: [PATCH] refactor config cache --- common/loader/json_conf.go | 12 ++++---- common/loader/json_conf_test.go | 39 ++++++++++++++++++++++++ common/loader/loader.go | 32 +++++++------------ proxy/blackhole/config_json.go | 2 +- proxy/registry/config_cache_json.go | 4 +-- transport/internet/authenticator_json.go | 12 +++----- 6 files changed, 63 insertions(+), 38 deletions(-) create mode 100644 common/loader/json_conf_test.go diff --git a/common/loader/json_conf.go b/common/loader/json_conf.go index 7305cd1fd..a58db060c 100644 --- a/common/loader/json_conf.go +++ b/common/loader/json_conf.go @@ -10,21 +10,21 @@ import ( ) type JSONConfigLoader struct { - *BaseConfigLoader + cache ConfigCreatorCache idKey string configKey string } -func NewJSONConfigLoader(idKey string, configKey string) *JSONConfigLoader { +func NewJSONConfigLoader(cache ConfigCreatorCache, idKey string, configKey string) *JSONConfigLoader { return &JSONConfigLoader{ - idKey: idKey, - configKey: configKey, - BaseConfigLoader: NewBaseConfigLoader(), + idKey: idKey, + configKey: configKey, + cache: cache, } } func (this *JSONConfigLoader) LoadWithID(raw []byte, id string) (interface{}, error) { - config, err := this.CreateConfig(id) + config, err := this.cache.CreateConfig(id) if err != nil { return nil, err } diff --git a/common/loader/json_conf_test.go b/common/loader/json_conf_test.go new file mode 100644 index 000000000..840d63100 --- /dev/null +++ b/common/loader/json_conf_test.go @@ -0,0 +1,39 @@ +// +build json + +package loader_test + +import ( + "testing" + + . "v2ray.com/core/common/loader" + "v2ray.com/core/testing/assert" +) + +type TestConfigA struct { + V int +} + +type TestConfigB struct { + S string +} + +func TestCreatorCache(t *testing.T) { + assert := assert.On(t) + + cache := ConfigCreatorCache{} + creator1 := func() interface{} { return &TestConfigA{} } + creator2 := func() interface{} { return &TestConfigB{} } + cache.RegisterCreator("1", creator1) + + loader := NewJSONConfigLoader(cache, "test", "") + rawA, err := loader.LoadWithID([]byte(`{"V": 2}`), "1") + assert.Error(err).IsNil() + instA := rawA.(*TestConfigA) + assert.Int(instA.V).Equals(2) + + cache.RegisterCreator("2", creator2) + rawB, err := loader.LoadWithID([]byte(`{"S": "a"}`), "2") + assert.Error(err).IsNil() + instB := rawB.(*TestConfigB) + assert.String(instB.S).Equals("a") +} diff --git a/common/loader/loader.go b/common/loader/loader.go index 07cb54e22..756f033fb 100644 --- a/common/loader/loader.go +++ b/common/loader/loader.go @@ -11,36 +11,26 @@ var ( type ConfigCreator func() interface{} -type ConfigLoader interface { - RegisterCreator(string, ConfigCreator) error - CreateConfig(string) (interface{}, error) - Load([]byte) (interface{}, string, error) - LoadWithID([]byte, string) (interface{}, error) -} +type ConfigCreatorCache map[string]ConfigCreator -type BaseConfigLoader struct { - creators map[string]ConfigCreator -} - -func NewBaseConfigLoader() *BaseConfigLoader { - return &BaseConfigLoader{ - creators: make(map[string]ConfigCreator), - } -} - -func (this *BaseConfigLoader) RegisterCreator(id string, creator ConfigCreator) error { - if _, found := this.creators[id]; found { +func (this ConfigCreatorCache) RegisterCreator(id string, creator ConfigCreator) error { + if _, found := this[id]; found { return common.ErrDuplicatedName } - this.creators[id] = creator + this[id] = creator return nil } -func (this *BaseConfigLoader) CreateConfig(id string) (interface{}, error) { - creator, found := this.creators[id] +func (this ConfigCreatorCache) CreateConfig(id string) (interface{}, error) { + creator, found := this[id] if !found { return nil, ErrUnknownConfigID } return creator(), nil } + +type ConfigLoader interface { + Load([]byte) (interface{}, string, error) + LoadWithID([]byte, string) (interface{}, error) +} diff --git a/proxy/blackhole/config_json.go b/proxy/blackhole/config_json.go index 33b352847..a64d44506 100644 --- a/proxy/blackhole/config_json.go +++ b/proxy/blackhole/config_json.go @@ -21,7 +21,7 @@ func (this *Config) UnmarshalJSON(data []byte) error { this.Response = new(NoneResponse) if jsonConfig.Response != nil { - loader := loader.NewJSONConfigLoader("type", "") + loader := loader.NewJSONConfigLoader(loader.ConfigCreatorCache{}, "type", "") loader.RegisterCreator("none", func() interface{} { return new(NoneResponse) }) loader.RegisterCreator("http", func() interface{} { return new(HTTPResponse) }) response, _, err := loader.Load(jsonConfig.Response) diff --git a/proxy/registry/config_cache_json.go b/proxy/registry/config_cache_json.go index 526e088ee..5a17a85dc 100644 --- a/proxy/registry/config_cache_json.go +++ b/proxy/registry/config_cache_json.go @@ -7,6 +7,6 @@ import ( ) func init() { - inboundConfigCache = loader.NewJSONConfigLoader("protocol", "settings") - outboundConfigCache = loader.NewJSONConfigLoader("protocol", "settings") + inboundConfigCache = loader.NewJSONConfigLoader(loader.ConfigCreatorCache{}, "protocol", "settings") + outboundConfigCache = loader.NewJSONConfigLoader(loader.ConfigCreatorCache{}, "protocol", "settings") } diff --git a/transport/internet/authenticator_json.go b/transport/internet/authenticator_json.go index 2f45437bc..31163b7e0 100644 --- a/transport/internet/authenticator_json.go +++ b/transport/internet/authenticator_json.go @@ -6,11 +6,7 @@ import ( "v2ray.com/core/common/loader" ) -func RegisterAuthenticatorConfig(name string, configCreator loader.ConfigCreator) error { - return configCache.RegisterCreator(name, configCreator) -} - -func CreateAuthenticatorConfig(rawConfig []byte) (string, AuthenticatorConfig, error) { +func CreateAuthenticatorConfig(rawConfig []byte) (string, interface{}, error) { config, name, err := configCache.Load(rawConfig) if err != nil { return name, nil, err @@ -18,6 +14,6 @@ func CreateAuthenticatorConfig(rawConfig []byte) (string, AuthenticatorConfig, e return name, config, nil } -var ( - configCache = loader.NewJSONConfigLoader("type", "") -) +func init() { + configCache = loader.NewJSONConfigLoader(loader.ConfigCreatorCache{}, "type", "") +}