6
6
"strings"
7
7
"time"
8
8
9
+ "go.mongodb.org/mongo-driver/bson"
9
10
"go.mongodb.org/mongo-driver/mongo"
10
11
"go.mongodb.org/mongo-driver/mongo/options"
11
12
"go.mongodb.org/mongo-driver/mongo/readconcern"
@@ -83,7 +84,10 @@ func ServerSelectionTimeout(d time.Duration) MongoOption {
83
84
}
84
85
}
85
86
86
- func MongoConnect (ctx context.Context , uri string , mongoOptions ... MongoOption ) (* mongo.Client , error ) {
87
+ func MongoConnectWithOpts (ctx context.Context ,
88
+ uri string ,
89
+ mongoOptions ... MongoOption ,
90
+ ) (* mongo.Client , * options.ClientOptions , error ) {
87
91
if ! strings .HasPrefix (uri , "mongodb://" ) {
88
92
uri = "mongodb://" + uri
89
93
}
@@ -103,37 +107,50 @@ func MongoConnect(ctx context.Context, uri string, mongoOptions ...MongoOption)
103
107
for _ , opt := range mongoOptions {
104
108
if opt != nil {
105
109
if err := opt (mopts ); err != nil {
106
- return nil , errors .Wrap (err , "invalid mongo option" )
110
+ return nil , nil , errors .Wrap (err , "invalid mongo option" )
107
111
}
108
112
}
109
113
}
110
114
111
115
conn , err := mongo .Connect (ctx , mopts )
112
116
if err != nil {
113
- return nil , errors .Wrap (err , "connect" )
117
+ return nil , nil , errors .Wrap (err , "connect" )
114
118
}
115
119
116
120
err = conn .Ping (ctx , nil )
117
121
if err != nil {
118
- return nil , errors .Wrap (err , "ping" )
122
+ return nil , nil , errors .Wrap (err , "ping" )
119
123
}
120
124
121
- return conn , nil
125
+ return conn , mopts , nil
126
+ }
127
+
128
+ func MongoConnect (
129
+ ctx context.Context ,
130
+ uri string ,
131
+ mongoOptions ... MongoOption ,
132
+ ) (* mongo.Client , error ) {
133
+ client , _ , err := MongoConnectWithOpts (ctx , uri , mongoOptions ... )
134
+ return client , err
122
135
}
123
136
124
137
type clientImpl struct {
125
- client * mongo.Client
138
+ client * mongo.Client
139
+ options * options.ClientOptions
126
140
}
127
141
128
142
func UnsafeClient (m * mongo.Client ) Client {
129
- return & clientImpl {m }
143
+ return & clientImpl {
144
+ client : m ,
145
+ options : options .Client (),
146
+ }
130
147
}
131
148
132
149
// Connect resolves MongoDB connection to Primary member and wraps it within Client object.
133
150
// In case of replica set it returns connection to Primary member,
134
151
// while in case of sharded cluster it returns connection to Config RS Primary member.
135
152
func Connect (ctx context.Context , uri , appName string ) (Client , error ) {
136
- client , err := MongoConnect (ctx , uri , AppName (appName ))
153
+ client , opts , err := MongoConnectWithOpts (ctx , uri , AppName (appName ))
137
154
if err != nil {
138
155
return nil , errors .Wrap (err , "create mongo connection" )
139
156
}
@@ -143,7 +160,10 @@ func Connect(ctx context.Context, uri, appName string) (Client, error) {
143
160
return nil , errors .Wrap (err , "get NodeInfo" )
144
161
}
145
162
if inf .isMongos () {
146
- return & clientImpl {client : client }, nil
163
+ return & clientImpl {
164
+ client : client ,
165
+ options : opts ,
166
+ }, nil
147
167
}
148
168
149
169
inf .Opts , err = getMongodOpts (ctx , client , nil )
@@ -152,7 +172,10 @@ func Connect(ctx context.Context, uri, appName string) (Client, error) {
152
172
}
153
173
154
174
if inf .isClusterLeader () {
155
- return & clientImpl {client : client }, nil
175
+ return & clientImpl {
176
+ client : client ,
177
+ options : opts ,
178
+ }, nil
156
179
}
157
180
158
181
csvr , err := getConfigsvrURI (ctx , client )
@@ -183,7 +206,10 @@ func Connect(ctx context.Context, uri, appName string) (Client, error) {
183
206
return nil , errors .Wrapf (err , "create mongo connection to configsvr with connection string '%s'" , curi )
184
207
}
185
208
186
- return & clientImpl {client : client }, nil
209
+ return & clientImpl {
210
+ client : client ,
211
+ options : opts ,
212
+ }, nil
187
213
}
188
214
189
215
func (l * clientImpl ) HasValidConnection (ctx context.Context ) error {
@@ -212,11 +238,16 @@ func (l *clientImpl) MongoClient() *mongo.Client {
212
238
return l .client
213
239
}
214
240
241
+ func (l * clientImpl ) MongoOptions () * options.ClientOptions {
242
+ return l .options
243
+ }
244
+
215
245
func (l * clientImpl ) ConfigDatabase () * mongo.Database {
216
246
return l .client .Database ("config" )
217
247
}
218
248
219
- func (l * clientImpl ) AdminCommand (ctx context.Context , cmd any , opts ... * options.RunCmdOptions ) * mongo.SingleResult {
249
+ func (l * clientImpl ) AdminCommand (ctx context.Context , cmd bson.D , opts ... * options.RunCmdOptions ) * mongo.SingleResult {
250
+ cmd = l .applyOptonsFromConnString (cmd )
220
251
return l .client .Database (defs .DB ).RunCommand (ctx , cmd , opts ... )
221
252
}
222
253
@@ -260,15 +291,38 @@ func (l *clientImpl) AgentsStatusCollection() *mongo.Collection {
260
291
return l .client .Database (defs .DB ).Collection (defs .AgentsStatusCollection )
261
292
}
262
293
294
+ func (l * clientImpl ) applyOptonsFromConnString (cmd bson.D ) bson.D {
295
+ if len (cmd ) == 0 {
296
+ return cmd
297
+ }
298
+
299
+ cmdName := cmd [0 ].Key
300
+ switch cmdName {
301
+ case "create" :
302
+ if l .options .WriteConcern != nil {
303
+ cmd = append (cmd , bson.E {"writeConcern" , l .options .WriteConcern })
304
+ }
305
+ default :
306
+ // do nothing for all other commands:
307
+ // flushRouterConfig
308
+ // _configsvrBalancerStart
309
+ // _configsvrBalancerStop
310
+ // _configsvrBalancerStatus
311
+ }
312
+
313
+ return cmd
314
+ }
315
+
263
316
var ErrInvalidConnection = errors .New ("invalid mongo connection" )
264
317
265
318
type Client interface {
266
319
Disconnect (ctx context.Context ) error
267
320
268
321
MongoClient () * mongo.Client
322
+ MongoOptions () * options.ClientOptions
269
323
270
324
ConfigDatabase () * mongo.Database
271
- AdminCommand (ctx context.Context , cmd any , opts ... * options.RunCmdOptions ) * mongo.SingleResult
325
+ AdminCommand (ctx context.Context , cmd bson. D , opts ... * options.RunCmdOptions ) * mongo.SingleResult
272
326
273
327
LogCollection () * mongo.Collection
274
328
ConfigCollection () * mongo.Collection
0 commit comments