diff --git a/frame/gins/gins.go b/frame/gins/gins.go index d08ba08d7..5e8ff2e51 100644 --- a/frame/gins/gins.go +++ b/frame/gins/gins.go @@ -8,12 +8,7 @@ package gins import ( - "github.com/gogf/gf/internal/intlog" - "github.com/gogf/gf/os/gfile" - "github.com/gogf/gf/container/gmap" - "github.com/gogf/gf/os/gcfg" - "github.com/gogf/gf/os/gfsnotify" ) var ( @@ -59,15 +54,3 @@ func GetOrSetFuncLock(name string, f func() interface{}) interface{} { func SetIfNotExist(name string, instance interface{}) bool { return instances.SetIfNotExist(name, instance) } - -// addConfigMonitor adds fsnotify monitor for configuration file if it exists. -func addConfigMonitor(key string, config *gcfg.Config) { - if path := config.FilePath(); path != "" && gfile.Exists(path) { - _, err := gfsnotify.Add(path, func(event *gfsnotify.Event) { - instances.Remove(key) - }) - if err != nil { - intlog.Error(err) - } - } -} diff --git a/frame/gins/gins_database.go b/frame/gins/gins_database.go index 23dc4ffbd..92842b178 100644 --- a/frame/gins/gins_database.go +++ b/frame/gins/gins_database.go @@ -29,7 +29,7 @@ func Database(name ...string) gdb.DB { group = name[0] } instanceKey := fmt.Sprintf("%s.%s", gFRAME_CORE_COMPONENT_NAME_DATABASE, group) - db := instances.GetOrSetFunc(instanceKey, func() interface{} { + db := instances.GetOrSetFuncLock(instanceKey, func() interface{} { // Configuration already exists. if gdb.GetConfig(group) != nil { db, err := gdb.Instance(group) @@ -72,7 +72,6 @@ func Database(name ...string) gdb.DB { gdb.SetConfigGroup(gdb.DEFAULT_GROUP_NAME, cg) } } - addConfigMonitor(instanceKey, config) if db, err := gdb.New(name...); err == nil { // Initialize logger for ORM. diff --git a/frame/gins/gins_log.go b/frame/gins/gins_log.go index 081c39bc2..0b8f21b64 100644 --- a/frame/gins/gins_log.go +++ b/frame/gins/gins_log.go @@ -24,7 +24,7 @@ func Log(name ...string) *glog.Logger { instanceName = name[0] } instanceKey := fmt.Sprintf("%s.%s", gFRAME_CORE_COMPONENT_NAME_LOGGER, instanceName) - return instances.GetOrSetFunc(instanceKey, func() interface{} { + return instances.GetOrSetFuncLock(instanceKey, func() interface{} { logger := glog.Instance(instanceName) // To avoid file no found error while it's not necessary. if Config().Available() { diff --git a/frame/gins/gins_redis.go b/frame/gins/gins_redis.go index 1f480edc6..9130ae1a4 100644 --- a/frame/gins/gins_redis.go +++ b/frame/gins/gins_redis.go @@ -24,7 +24,7 @@ func Redis(name ...string) *gredis.Redis { group = name[0] } instanceKey := fmt.Sprintf("%s.%s", gFRAME_CORE_COMPONENT_NAME_REDIS, group) - result := instances.GetOrSetFunc(instanceKey, func() interface{} { + result := instances.GetOrSetFuncLock(instanceKey, func() interface{} { // If already configured, it returns the redis instance. if _, ok := gredis.GetConfig(group); ok { return gredis.Instance(group) @@ -36,7 +36,6 @@ func Redis(name ...string) *gredis.Redis { if err != nil { panic(err) } - addConfigMonitor(instanceKey, config) return gredis.New(redisConfig) } else { panic(fmt.Sprintf(`configuration for redis not found for group "%s"`, group)) diff --git a/frame/gins/gins_server.go b/frame/gins/gins_server.go index 309e516d3..58e7d986c 100644 --- a/frame/gins/gins_server.go +++ b/frame/gins/gins_server.go @@ -18,7 +18,7 @@ const ( // Server returns an instance of http server with specified name. func Server(name ...interface{}) *ghttp.Server { instanceKey := fmt.Sprintf("%s.%v", gFRAME_CORE_COMPONENT_NAME_SERVER, name) - return instances.GetOrSetFunc(instanceKey, func() interface{} { + return instances.GetOrSetFuncLock(instanceKey, func() interface{} { s := ghttp.GetServer(name...) // To avoid file no found error while it's not necessary. if Config().Available() { @@ -36,7 +36,7 @@ func Server(name ...interface{}) *ghttp.Server { } // As it might use template feature, // it initialize the view instance as well. - View() + _ = getViewInstance() } return s }).(*ghttp.Server) diff --git a/frame/gins/gins_view.go b/frame/gins/gins_view.go index fa14e0e49..62ee0f581 100644 --- a/frame/gins/gins_view.go +++ b/frame/gins/gins_view.go @@ -24,23 +24,31 @@ func View(name ...string) *gview.View { instanceName = name[0] } instanceKey := fmt.Sprintf("%s.%s", gFRAME_CORE_COMPONENT_NAME_VIEWER, instanceName) - return instances.GetOrSetFunc(instanceKey, func() interface{} { - view := gview.Instance(instanceName) - // To avoid file no found error while it's not necessary. - if Config().Available() { - var m map[string]interface{} - // It firstly searches the configuration of the instance name. - if m = Config().GetMap(fmt.Sprintf(`%s.%s`, gVIEWER_NODE_NAME, instanceName)); m == nil { - // If the configuration for the instance does not exist, - // it uses the default view configuration. - m = Config().GetMap(gVIEWER_NODE_NAME) - } - if m != nil { - if err := view.SetConfigWithMap(m); err != nil { - panic(err) - } - } - } - return view + return instances.GetOrSetFuncLock(instanceKey, func() interface{} { + return getViewInstance(instanceName) }).(*gview.View) } + +func getViewInstance(name ...string) *gview.View { + instanceName := gview.DEFAULT_NAME + if len(name) > 0 && name[0] != "" { + instanceName = name[0] + } + view := gview.Instance(instanceName) + // To avoid file no found error while it's not necessary. + if Config().Available() { + var m map[string]interface{} + // It firstly searches the configuration of the instance name. + if m = Config().GetMap(fmt.Sprintf(`%s.%s`, gVIEWER_NODE_NAME, name)); m == nil { + // If the configuration for the instance does not exist, + // it uses the default view configuration. + m = Config().GetMap(gVIEWER_NODE_NAME) + } + if m != nil { + if err := view.SetConfigWithMap(m); err != nil { + panic(err) + } + } + } + return view +}