github.com/wfusion/gofusion@v1.1.14/mongo/construct.go (about) 1 package mongo 2 3 import ( 4 "context" 5 "log" 6 "reflect" 7 "syscall" 8 9 "github.com/wfusion/gofusion/common/di" 10 "github.com/wfusion/gofusion/common/infra/drivers/mongo" 11 "github.com/wfusion/gofusion/common/utils" 12 "github.com/wfusion/gofusion/common/utils/inspect" 13 "github.com/wfusion/gofusion/config" 14 15 mgoEvt "go.mongodb.org/mongo-driver/event" 16 mgoDrv "go.mongodb.org/mongo-driver/mongo" 17 18 fusLog "github.com/wfusion/gofusion/log" 19 20 _ "github.com/wfusion/gofusion/log/customlogger" 21 ) 22 23 func Construct(ctx context.Context, confs map[string]*Conf, opts ...utils.OptionExtender) func() { 24 opt := utils.ApplyOptions[config.InitOption](opts...) 25 optU := utils.ApplyOptions[useOption](opts...) 26 if opt.AppName == "" { 27 opt.AppName = optU.appName 28 } 29 for name, conf := range confs { 30 addInstance(ctx, name, conf, opt) 31 } 32 33 return func() { 34 rwlock.Lock() 35 defer rwlock.Unlock() 36 37 pid := syscall.Getpid() 38 app := config.Use(opt.AppName).AppName() 39 if appInstances != nil { 40 for name, instance := range appInstances[opt.AppName] { 41 if err := instance.GetProxy().Disconnect(nil); err != nil { 42 log.Printf("%v [Gofusion] %s %s %s disconnect error: %s", 43 pid, app, config.ComponentMongo, name, err) 44 } 45 } 46 delete(appInstances, opt.AppName) 47 } 48 } 49 } 50 51 func addInstance(ctx context.Context, name string, conf *Conf, opt *config.InitOption) { 52 var monitor *mgoEvt.CommandMonitor 53 if utils.IsStrNotBlank(conf.LoggerConfig.Logger) { 54 loggerType := inspect.TypeOf(conf.LoggerConfig.Logger) 55 loggerValue := reflect.New(loggerType) 56 if loggerValue.Type().Implements(customLoggerType) { 57 l := fusLog.Use(conf.LoggerConfig.LogInstance, fusLog.AppName(opt.AppName)) 58 loggerValue.Interface().(customLogger).Init(l, opt.AppName, name) 59 } 60 monitor = loggerValue.Interface().(logger).GetMonitor() 61 } 62 63 // conf.Option.Password = config.CryptoDecryptFunc()(conf.Option.Password) 64 mgoCli, err := mongo.Default.New(ctx, conf.Option, 65 mongo.WithMonitor(monitor), 66 mongo.WithPoolMonitor(&mgoEvt.PoolMonitor{Event: metricsPoolMonitor(opt.AppName, name)})) 67 if err != nil { 68 panic(err) 69 } 70 71 rwlock.Lock() 72 defer rwlock.Unlock() 73 if appInstances == nil { 74 appInstances = make(map[string]map[string]*instance) 75 } 76 if appInstances[opt.AppName] == nil { 77 appInstances[opt.AppName] = make(map[string]*instance) 78 } 79 if _, ok := appInstances[opt.AppName][name]; ok { 80 panic(ErrDuplicatedName) 81 } 82 appInstances[opt.AppName][name] = &instance{mongo: mgoCli, name: name, database: conf.DB} 83 84 // ioc 85 if opt.DI != nil { 86 opt.DI.MustProvide( 87 func() *mgoDrv.Database { 88 return Use(name, AppName(opt.AppName)).Database 89 }, 90 di.Name(name), 91 ) 92 } 93 94 go startDaemonRoutines(ctx, opt.AppName, name, conf) 95 } 96 97 func init() { 98 config.AddComponent(config.ComponentMongo, Construct, config.WithFlag(&flagString)) 99 }