component.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. package mhayaMongo
  2. import (
  3. "context"
  4. "crypto/tls"
  5. "fmt"
  6. "time"
  7. cfacade "github.com/mhaya/facade"
  8. clog "github.com/mhaya/logger"
  9. cprofile "github.com/mhaya/profile"
  10. "go.mongodb.org/mongo-driver/mongo"
  11. "go.mongodb.org/mongo-driver/mongo/options"
  12. "go.mongodb.org/mongo-driver/mongo/readpref"
  13. )
  14. const (
  15. Name = "mongo_component"
  16. )
  17. type (
  18. Component struct {
  19. cfacade.Component
  20. dbMap map[string]map[string]*mongo.Database
  21. }
  22. // HashDb hash by group id
  23. HashDb func(dbMaps map[string]*mongo.Database) string
  24. )
  25. func NewComponent() *Component {
  26. return &Component{
  27. dbMap: make(map[string]map[string]*mongo.Database),
  28. }
  29. }
  30. func (*Component) Name() string {
  31. return Name
  32. }
  33. func (s *Component) Init() {
  34. // load only the database contained in the `db_id_list`
  35. mongoIdList := s.App().Settings().Get("db_id_list")
  36. if mongoIdList.LastError() != nil || mongoIdList.Size() < 1 {
  37. clog.Warnf("[nodeId = %s] `mongo_id_list` property not exists.", s.App().NodeId())
  38. return
  39. }
  40. mongoConfig := cprofile.GetConfig("mongo")
  41. if mongoConfig.LastError() != nil {
  42. panic("`mongo` property not exists in profile file.")
  43. }
  44. for _, groupId := range mongoConfig.Keys() {
  45. s.dbMap[groupId] = make(map[string]*mongo.Database)
  46. dbGroup := mongoConfig.GetConfig(groupId)
  47. for i := 0; i < dbGroup.Size(); i++ {
  48. item := dbGroup.GetConfig(i)
  49. var (
  50. enable = item.GetBool("enable", true)
  51. id = item.GetString("db_id")
  52. dbName = item.GetString("db_name")
  53. uri = item.GetString("uri")
  54. timeout = time.Duration(item.GetInt64("timeout", 3)) * time.Second
  55. tlsEnable = item.GetInt("tls")
  56. maxPoolSize = item.GetInt("maxPoolSize")
  57. minPoolSize = item.GetInt("minPoolSize")
  58. maxConnIdleTime = item.GetInt("maxConnIdleTime")
  59. connectTimeout = item.GetInt("connectTimeout")
  60. socketTimeout = item.GetInt("socketTimeout")
  61. )
  62. for _, key := range mongoIdList.Keys() {
  63. dbId := mongoIdList.Get(key).ToString()
  64. if id != dbId {
  65. continue
  66. }
  67. if !enable {
  68. panic(fmt.Sprintf("[dbName = %s] is disabled!", dbName))
  69. }
  70. db, err := CreateDatabase(uri, dbName, tlsEnable, uint64(maxPoolSize), uint64(minPoolSize), maxConnIdleTime, connectTimeout, socketTimeout, timeout)
  71. if err != nil {
  72. panic(fmt.Sprintf("[dbName = %s] create mongodb fail. error = %s", dbName, err))
  73. }
  74. s.dbMap[groupId][id] = db
  75. clog.Infof("[dbGroup =%s, dbName = %s] is connected.", groupId, id)
  76. }
  77. }
  78. }
  79. }
  80. func CreateDatabase(uri, dbName string, tlsEnable int, maxPoolSize uint64, minPoolSize uint64, maxConnIdleTime int, connectTimeout, socketTimeout int, timeout ...time.Duration) (*mongo.Database, error) {
  81. tt := 5 * time.Second
  82. if len(timeout) > 0 && timeout[0].Seconds() > 3 {
  83. tt = timeout[0]
  84. }
  85. var o *options.ClientOptions
  86. if tlsEnable == 1 {
  87. tlsConfig := &tls.Config{
  88. MinVersion: tls.VersionTLS12,
  89. PreferServerCipherSuites: true,
  90. }
  91. o = options.Client().ApplyURI(uri).SetMaxPoolSize(maxPoolSize). //最大连接
  92. SetMinPoolSize(minPoolSize). //最小连接
  93. SetMaxConnIdleTime(time.Duration(maxConnIdleTime) * time.Second). //连接空闲时间
  94. SetConnectTimeout(time.Duration(connectTimeout) * time.Second). //连接超时时间
  95. SetSocketTimeout(time.Duration(socketTimeout) * time.Second).SetTLSConfig(tlsConfig) //套接字超时时间
  96. } else {
  97. o = options.Client().ApplyURI(uri).SetMaxPoolSize(maxPoolSize). //最大连接
  98. SetMinPoolSize(minPoolSize). //最小连接
  99. SetMaxConnIdleTime(time.Duration(maxConnIdleTime) * time.Second). //连接空闲时间
  100. SetConnectTimeout(time.Duration(connectTimeout) * time.Second). //连接超时时间
  101. SetSocketTimeout(time.Duration(socketTimeout) * time.Second) //套接字超时时间
  102. }
  103. if err := o.Validate(); err != nil {
  104. return nil, err
  105. }
  106. ctx, cancel := context.WithTimeout(context.Background(), tt)
  107. defer cancel()
  108. client, err := mongo.Connect(ctx, o)
  109. if err != nil {
  110. return nil, err
  111. }
  112. err = client.Ping(context.Background(), readpref.Primary())
  113. if err != nil {
  114. return nil, err
  115. }
  116. clog.Infof("ping db [uri = %s] is ok", uri)
  117. return client.Database(dbName), nil
  118. }
  119. func (s *Component) GetDb(id string) *mongo.Database {
  120. for _, group := range s.dbMap {
  121. for k, v := range group {
  122. if k == id {
  123. return v
  124. }
  125. }
  126. }
  127. return nil
  128. }
  129. func (s *Component) GetHashDb(groupId string, hashFn HashDb) (*mongo.Database, bool) {
  130. dbGroup, found := s.GetDbMap(groupId)
  131. if !found {
  132. clog.Warnf("groupId = %s not found.", groupId)
  133. return nil, false
  134. }
  135. dbId := hashFn(dbGroup)
  136. db, found := dbGroup[dbId]
  137. return db, found
  138. }
  139. func (s *Component) GetDbMap(groupId string) (map[string]*mongo.Database, bool) {
  140. dbGroup, found := s.dbMap[groupId]
  141. return dbGroup, found
  142. }