mirror of
https://github.com/v2fly/v2ray-core.git
synced 2025-01-17 14:57:44 -05:00
refactor config cache
This commit is contained in:
parent
d38e62932d
commit
8f6a972970
@ -10,21 +10,21 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type JSONConfigLoader struct {
|
type JSONConfigLoader struct {
|
||||||
*BaseConfigLoader
|
cache ConfigCreatorCache
|
||||||
idKey string
|
idKey string
|
||||||
configKey string
|
configKey string
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewJSONConfigLoader(idKey string, configKey string) *JSONConfigLoader {
|
func NewJSONConfigLoader(cache ConfigCreatorCache, idKey string, configKey string) *JSONConfigLoader {
|
||||||
return &JSONConfigLoader{
|
return &JSONConfigLoader{
|
||||||
idKey: idKey,
|
idKey: idKey,
|
||||||
configKey: configKey,
|
configKey: configKey,
|
||||||
BaseConfigLoader: NewBaseConfigLoader(),
|
cache: cache,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *JSONConfigLoader) LoadWithID(raw []byte, id string) (interface{}, error) {
|
func (this *JSONConfigLoader) LoadWithID(raw []byte, id string) (interface{}, error) {
|
||||||
config, err := this.CreateConfig(id)
|
config, err := this.cache.CreateConfig(id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
39
common/loader/json_conf_test.go
Normal file
39
common/loader/json_conf_test.go
Normal file
@ -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")
|
||||||
|
}
|
@ -11,36 +11,26 @@ var (
|
|||||||
|
|
||||||
type ConfigCreator func() interface{}
|
type ConfigCreator func() interface{}
|
||||||
|
|
||||||
type ConfigLoader interface {
|
type ConfigCreatorCache map[string]ConfigCreator
|
||||||
RegisterCreator(string, ConfigCreator) error
|
|
||||||
CreateConfig(string) (interface{}, error)
|
|
||||||
Load([]byte) (interface{}, string, error)
|
|
||||||
LoadWithID([]byte, string) (interface{}, error)
|
|
||||||
}
|
|
||||||
|
|
||||||
type BaseConfigLoader struct {
|
func (this ConfigCreatorCache) RegisterCreator(id string, creator ConfigCreator) error {
|
||||||
creators map[string]ConfigCreator
|
if _, found := this[id]; found {
|
||||||
}
|
|
||||||
|
|
||||||
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 {
|
|
||||||
return common.ErrDuplicatedName
|
return common.ErrDuplicatedName
|
||||||
}
|
}
|
||||||
|
|
||||||
this.creators[id] = creator
|
this[id] = creator
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *BaseConfigLoader) CreateConfig(id string) (interface{}, error) {
|
func (this ConfigCreatorCache) CreateConfig(id string) (interface{}, error) {
|
||||||
creator, found := this.creators[id]
|
creator, found := this[id]
|
||||||
if !found {
|
if !found {
|
||||||
return nil, ErrUnknownConfigID
|
return nil, ErrUnknownConfigID
|
||||||
}
|
}
|
||||||
return creator(), nil
|
return creator(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type ConfigLoader interface {
|
||||||
|
Load([]byte) (interface{}, string, error)
|
||||||
|
LoadWithID([]byte, string) (interface{}, error)
|
||||||
|
}
|
||||||
|
@ -21,7 +21,7 @@ func (this *Config) UnmarshalJSON(data []byte) error {
|
|||||||
|
|
||||||
this.Response = new(NoneResponse)
|
this.Response = new(NoneResponse)
|
||||||
if jsonConfig.Response != nil {
|
if jsonConfig.Response != nil {
|
||||||
loader := loader.NewJSONConfigLoader("type", "")
|
loader := loader.NewJSONConfigLoader(loader.ConfigCreatorCache{}, "type", "")
|
||||||
loader.RegisterCreator("none", func() interface{} { return new(NoneResponse) })
|
loader.RegisterCreator("none", func() interface{} { return new(NoneResponse) })
|
||||||
loader.RegisterCreator("http", func() interface{} { return new(HTTPResponse) })
|
loader.RegisterCreator("http", func() interface{} { return new(HTTPResponse) })
|
||||||
response, _, err := loader.Load(jsonConfig.Response)
|
response, _, err := loader.Load(jsonConfig.Response)
|
||||||
|
@ -7,6 +7,6 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
inboundConfigCache = loader.NewJSONConfigLoader("protocol", "settings")
|
inboundConfigCache = loader.NewJSONConfigLoader(loader.ConfigCreatorCache{}, "protocol", "settings")
|
||||||
outboundConfigCache = loader.NewJSONConfigLoader("protocol", "settings")
|
outboundConfigCache = loader.NewJSONConfigLoader(loader.ConfigCreatorCache{}, "protocol", "settings")
|
||||||
}
|
}
|
||||||
|
@ -6,11 +6,7 @@ import (
|
|||||||
"v2ray.com/core/common/loader"
|
"v2ray.com/core/common/loader"
|
||||||
)
|
)
|
||||||
|
|
||||||
func RegisterAuthenticatorConfig(name string, configCreator loader.ConfigCreator) error {
|
func CreateAuthenticatorConfig(rawConfig []byte) (string, interface{}, error) {
|
||||||
return configCache.RegisterCreator(name, configCreator)
|
|
||||||
}
|
|
||||||
|
|
||||||
func CreateAuthenticatorConfig(rawConfig []byte) (string, AuthenticatorConfig, error) {
|
|
||||||
config, name, err := configCache.Load(rawConfig)
|
config, name, err := configCache.Load(rawConfig)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return name, nil, err
|
return name, nil, err
|
||||||
@ -18,6 +14,6 @@ func CreateAuthenticatorConfig(rawConfig []byte) (string, AuthenticatorConfig, e
|
|||||||
return name, config, nil
|
return name, config, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
func init() {
|
||||||
configCache = loader.NewJSONConfigLoader("type", "")
|
configCache = loader.NewJSONConfigLoader(loader.ConfigCreatorCache{}, "type", "")
|
||||||
)
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user