mgo_config.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. package mgodb
  2. import (
  3. "context"
  4. "eta/eta_forum_admin/utils"
  5. "fmt"
  6. "go.mongodb.org/mongo-driver/bson"
  7. "go.mongodb.org/mongo-driver/event"
  8. "go.mongodb.org/mongo-driver/mongo"
  9. "go.mongodb.org/mongo-driver/mongo/options"
  10. "go.mongodb.org/mongo-driver/mongo/readpref"
  11. "sync"
  12. "time"
  13. )
  14. var (
  15. MaxPoolSize = uint64(50)
  16. MinPoolSize = uint64(0)
  17. )
  18. //func init() {
  19. // fmt.Println("start MgoNewClient")
  20. // MgoNewClient()
  21. // fmt.Println("end MgoNewClient")
  22. //}
  23. type MgoConfig struct {
  24. Url string `json:"url"`
  25. Username string `json:"username"`
  26. Password string `json:"password"`
  27. AuthMechanism string `json:"auth_mechanism"`
  28. Database string `json:"database"`
  29. }
  30. func NewMgoClient(mgoConfig MgoConfig) *mongo.Client {
  31. var err error
  32. ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
  33. defer cancel()
  34. credential := options.Credential{
  35. AuthMechanism: mgoConfig.AuthMechanism,
  36. Username: mgoConfig.Username,
  37. Password: mgoConfig.Password,
  38. }
  39. clientOpts := options.Client().ApplyURI(mgoConfig.Url).SetAuth(credential)
  40. startedCommands := sync.Map{} // map[int64]bson.Raw
  41. cmdMonitor := &event.CommandMonitor{
  42. Started: func(_ context.Context, evt *event.CommandStartedEvent) {
  43. startedCommands.Store(evt.RequestID, evt.Command)
  44. //startedCommands[evt.RequestID] = evt.Command
  45. },
  46. Succeeded: func(_ context.Context, evt *event.CommandSucceededEvent) {
  47. //log.Printf("Command: %v Reply: %v\n",
  48. // startedCommands[evt.RequestID],
  49. // evt.Reply,
  50. //)
  51. var commands bson.Raw
  52. v, ok := startedCommands.Load(evt.RequestID)
  53. if ok {
  54. commands = v.(bson.Raw)
  55. }
  56. utils.MongoLog.Info("\n【MongoDB】[%.3fms] [%v] \n", float64(evt.Duration)/1e6, commands)
  57. },
  58. Failed: func(_ context.Context, evt *event.CommandFailedEvent) {
  59. //log.Printf("Command: %v Failure: %v\n",
  60. // startedCommands[evt.RequestID],
  61. // evt.Failure,
  62. //)
  63. var commands bson.Raw
  64. v, ok := startedCommands.Load(evt.RequestID)
  65. if ok {
  66. commands = v.(bson.Raw)
  67. }
  68. utils.MongoLog.Info("\n【MongoDB】[%.3fms] [%v] \n %v \n", float64(evt.Duration)/1e6, commands, evt.Failure)
  69. },
  70. }
  71. // 创建options
  72. clientOpts.SetMonitor(cmdMonitor)
  73. clientOpts.SetMinPoolSize(MinPoolSize)
  74. clientOpts.SetMaxPoolSize(MaxPoolSize)
  75. clientOpts.SetConnectTimeout(10 * time.Second)
  76. mgoClient, err := mongo.Connect(context.TODO(), clientOpts)
  77. if err != nil {
  78. panic(err)
  79. }
  80. if err = mgoClient.Ping(ctx, readpref.Primary()); err != nil {
  81. panic(err)
  82. }
  83. fmt.Println("Connected to MongoDB!")
  84. return mgoClient
  85. }