@@ -2,6 +2,7 @@ package mongodbc
22
33import (
44 "context"
5+ "errors"
56 "flag"
67 "log/slog"
78 "time"
@@ -13,10 +14,16 @@ import (
1314)
1415
1516type config struct {
16- url string
17- username string
18- password string
19- timeout time.Duration
17+ url string
18+ username string
19+ password string
20+ authMechanism string
21+ authSource string
22+ maxPoolSize uint64
23+ minPoolSize uint64
24+ timeout time.Duration
25+ connectionTimeout time.Duration
26+ ServerSelectionTimeout time.Duration
2027}
2128
2229type mongoDbComponent struct {
@@ -34,39 +41,93 @@ func NewMongoDbComponent(id string) *mongoDbComponent {
3441 }
3542}
3643
44+ // GetMongoClient returns the mongo client
3745func (m * mongoDbComponent ) GetMongoClient () * mongo.Client {
3846 return m .mongoClient
3947}
4048
49+ // GetMongoClientWithName returns the mongo client with the given name in config
50+ func (m * mongoDbComponent ) GetDatabase () * mongo.Database {
51+ return m .mongoClient .Database (m .config .authSource )
52+ }
53+
54+ // GetCollection returns the mongo client with the given collection name
55+ func (m * mongoDbComponent ) GetCollection (collectionName string ) * mongo.Collection {
56+ return m .mongoClient .Database (m .config .authSource ).Collection (collectionName )
57+ }
58+
59+ // GetDatabaseName returns the mongo client with the given name in config
60+ func (m * mongoDbComponent ) GetDatabaseName () string {
61+ return m .config .authSource
62+ }
63+
64+ // GetDatabaseWithName returns the mongo client with the given name
65+ func (m * mongoDbComponent ) GetDatabaseWithName (dbName string ) * mongo.Database {
66+ return m .mongoClient .Database (dbName )
67+ }
68+
69+ // GetCollection returns the mongo client with the given name
70+ func (m * mongoDbComponent ) GetCollectionWithDatabase (dbName , collectionName string ) * mongo.Collection {
71+ return m .mongoClient .Database (dbName ).Collection (collectionName )
72+ }
73+
4174func (m * mongoDbComponent ) ID () string {
4275 return m .id
4376}
4477
4578func (m * mongoDbComponent ) InitFlags () {
4679 flag .StringVar (& m .url , m .id + "-url" , "mongodb://localhost:27017" , "redis urls. default: mongodb://localhost:27017" )
47- flag .StringVar (& m .username , m .id + "-username" , "" , "redis username. default: ''" )
48- flag .StringVar (& m .password , m .id + "-password" , "" , "redis password. default: ''" )
4980
50- flag .DurationVar (& m .timeout , m .id + "-timeout" , 10 * time .Second , "redis timeout. default: 10s" )
81+ flag .StringVar (& m .username , m .id + "-username" , "" , "mongodb username. default: ''" )
82+ flag .StringVar (& m .password , m .id + "-password" , "" , "mongodb password. default: ''" )
83+ flag .StringVar (& m .authMechanism , m .id + "-auth-mechanism" , "SCRAM-SHA-256" , "AuthMechanism supported values include SCRAM-SHA-256, SCRAM-SHA-1, MONGODB-CR, PLAIN, GSSAPI, MONGODB-X509, and MONGODB-AWS. default: 'SCRAM-SHA-256'" )
84+ flag .StringVar (& m .authSource , m .id + "-auth-source" , "" , "AuthSource. default: ''" )
85+
86+ flag .Uint64Var (& m .minPoolSize , m .id + "-min-pool-size" , 10 , "mongodb min pool size. default: 10" )
87+ flag .Uint64Var (& m .maxPoolSize , m .id + "-max-pool-size" , 100 , "mongodb max pool size. default: 100" )
88+
89+ flag .DurationVar (& m .timeout , m .id + "-timeout" , 10 * time .Second , "mongodb timeout. default: 10s" )
90+ flag .DurationVar (& m .connectionTimeout , m .id + "-connection-timeout" , 30 * time .Second , "mongodb connection timeout. default: 30s" )
91+ flag .DurationVar (& m .ServerSelectionTimeout , m .id + "-server-selection-timeout" , 30 * time .Second , "mongodb server selection timeout. default: 30s" )
92+
5193}
5294
5395func (m * mongoDbComponent ) Activate (ctx sctx.ServiceContext ) error {
5496 // create mongo client
5597 opts := options .Client ()
5698 // set url
5799 opts .ApplyURI (m .url )
100+
101+ // set min pool size
102+ opts .SetMinPoolSize (m .minPoolSize )
103+
104+ // set max pool size
105+ opts .SetMaxPoolSize (m .maxPoolSize )
106+
58107 // set timeout
59108 opts .SetTimeout (m .timeout )
60109
61- // set auth
110+ // set connection timeout
111+ opts .SetConnectTimeout (m .connectionTimeout )
112+
113+ // set ConnectTimeout
114+ opts .SetServerSelectionTimeout (m .ServerSelectionTimeout )
115+
116+ // set auth database
62117 if m .username != "" && m .password != "" {
63118 opts .SetAuth (options.Credential {
64- Username : m .username ,
65- Password : m .password ,
119+ Username : m .username ,
120+ Password : m .password ,
121+ AuthMechanism : m .authMechanism ,
122+ AuthSource : m .authSource ,
66123 })
67124 }
125+ // validate auth source, return err
126+ if opts .Auth .AuthSource == "" {
127+ return errors .New ("auth source is empty" )
128+ }
68129
69- slog .Info ("Connecting to mongo db" )
130+ slog .Info ("Connecting to mongo db ... " )
70131
71132 client , err := mongo .Connect (opts )
72133 if err != nil {
@@ -81,7 +142,7 @@ func (m *mongoDbComponent) Activate(ctx sctx.ServiceContext) error {
81142
82143 m .mongoClient = client
83144
84- slog .Info ("Connect to mongo db success" )
145+ slog .Info ("Connect to mongo db success !!! " )
85146
86147 return nil
87148}
0 commit comments