forked from mongodb/mongo-go-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
360 lines (301 loc) · 9.8 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
// Copyright (C) MongoDB, Inc. 2017-present.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
package mongo
import (
"context"
"time"
"github.com/mongodb/mongo-go-driver/core/command"
"github.com/mongodb/mongo-go-driver/core/connstring"
"github.com/mongodb/mongo-go-driver/core/description"
"github.com/mongodb/mongo-go-driver/core/dispatch"
"github.com/mongodb/mongo-go-driver/core/readconcern"
"github.com/mongodb/mongo-go-driver/core/readpref"
"github.com/mongodb/mongo-go-driver/core/session"
"github.com/mongodb/mongo-go-driver/core/tag"
"github.com/mongodb/mongo-go-driver/core/topology"
"github.com/mongodb/mongo-go-driver/core/uuid"
"github.com/mongodb/mongo-go-driver/core/writeconcern"
"github.com/mongodb/mongo-go-driver/mongo/clientopt"
"github.com/mongodb/mongo-go-driver/mongo/dbopt"
"github.com/mongodb/mongo-go-driver/mongo/listdbopt"
"github.com/mongodb/mongo-go-driver/mongo/sessionopt"
)
const defaultLocalThreshold = 15 * time.Millisecond
// Client performs operations on a given topology.
type Client struct {
id uuid.UUID
topologyOptions []topology.Option
topology *topology.Topology
connString connstring.ConnString
localThreshold time.Duration
retryWrites bool
clock *session.ClusterClock
readPreference *readpref.ReadPref
readConcern *readconcern.ReadConcern
writeConcern *writeconcern.WriteConcern
}
// Connect creates a new Client and then initializes it using the Connect method.
func Connect(ctx context.Context, uri string, opts ...clientopt.Option) (*Client, error) {
c, err := NewClientWithOptions(uri, opts...)
if err != nil {
return nil, err
}
err = c.Connect(ctx)
if err != nil {
return nil, err
}
return c, nil
}
// NewClient creates a new client to connect to a cluster specified by the uri.
func NewClient(uri string) (*Client, error) {
cs, err := connstring.Parse(uri)
if err != nil {
return nil, err
}
return newClient(cs, nil)
}
// NewClientWithOptions creates a new client to connect to to a cluster specified by the connection
// string and the options manually passed in. If the same option is configured in both the
// connection string and the manual options, the manual option will be ignored.
func NewClientWithOptions(uri string, opts ...clientopt.Option) (*Client, error) {
cs, err := connstring.Parse(uri)
if err != nil {
return nil, err
}
return newClient(cs, opts...)
}
// NewClientFromConnString creates a new client to connect to a cluster, with configuration
// specified by the connection string.
func NewClientFromConnString(cs connstring.ConnString) (*Client, error) {
return newClient(cs, nil)
}
// Connect initializes the Client by starting background monitoring goroutines.
// This method must be called before a Client can be used.
func (c *Client) Connect(ctx context.Context) error {
err := c.topology.Connect(ctx)
if err != nil {
return err
}
return nil
}
// Disconnect closes sockets to the topology referenced by this Client. It will
// shut down any monitoring goroutines, close the idle connection pool, and will
// wait until all the in use connections have been returned to the connection
// pool and closed before returning. If the context expires via cancellation,
// deadline, or timeout before the in use connections have returned, the in use
// connections will be closed, resulting in the failure of any in flight read
// or write operations. If this method returns with no errors, all connections
// associated with this Client have been closed.
func (c *Client) Disconnect(ctx context.Context) error {
c.endSessions(ctx)
return c.topology.Disconnect(ctx)
}
// StartSession starts a new session.
func (c *Client) StartSession(opts ...sessionopt.Session) (*Session, error) {
if c.topology.SessionPool == nil {
return nil, topology.ErrTopologyClosed
}
// By default the session inherits the default read/write concerns of the client
defaultOpts := []sessionopt.Session{
sessionopt.DefaultReadConcern(c.readConcern),
sessionopt.DefaultReadPreference(c.readPreference),
sessionopt.DefaultWriteConcern(c.writeConcern),
}
// If the user provided the default read/write concerns explicitly, this will overwrite with them.
sessionOpts, err := sessionopt.BundleSession(append(defaultOpts, opts...)...).Unbundle(true)
if err != nil {
return nil, err
}
sess, err := session.NewClientSession(c.topology.SessionPool, c.id, session.Explicit, sessionOpts...)
if err != nil {
return nil, err
}
sess.RetryWrite = c.retryWrites
return &Session{
Client: sess,
topo: c.topology,
}, nil
}
func (c *Client) endSessions(ctx context.Context) {
cmd := command.EndSessions{
Clock: c.clock,
SessionIDs: c.topology.SessionPool.IDSlice(),
}
_, _ = dispatch.EndSessions(ctx, cmd, c.topology, description.ReadPrefSelector(readpref.PrimaryPreferred()))
}
func newClient(cs connstring.ConnString, opts ...clientopt.Option) (*Client, error) {
clientOpt, err := clientopt.BundleClient(opts...).Unbundle(cs)
if err != nil {
return nil, err
}
client := &Client{
topologyOptions: clientOpt.TopologyOptions,
connString: clientOpt.ConnString,
localThreshold: defaultLocalThreshold,
}
uuid, err := uuid.New()
if err != nil {
return nil, err
}
client.id = uuid
topts := append(
client.topologyOptions,
topology.WithConnString(func(connstring.ConnString) connstring.ConnString { return client.connString }),
topology.WithServerOptions(func(opts ...topology.ServerOption) []topology.ServerOption {
return append(opts, topology.WithClock(func(clock *session.ClusterClock) *session.ClusterClock {
return client.clock
}))
}),
)
topo, err := topology.New(topts...)
if err != nil {
return nil, err
}
client.topology = topo
client.clock = &session.ClusterClock{}
if client.readConcern == nil {
client.readConcern = readConcernFromConnString(&client.connString)
if client.readConcern == nil {
// no read concern in conn string
client.readConcern = readconcern.New()
}
}
if client.writeConcern == nil {
client.writeConcern = writeConcernFromConnString(&client.connString)
}
if client.readPreference == nil {
rp, err := readPreferenceFromConnString(&client.connString)
if err != nil {
return nil, err
}
if rp != nil {
client.readPreference = rp
} else {
client.readPreference = readpref.Primary()
}
}
return client, nil
}
func readConcernFromConnString(cs *connstring.ConnString) *readconcern.ReadConcern {
if len(cs.ReadConcernLevel) == 0 {
return nil
}
rc := &readconcern.ReadConcern{}
readconcern.Level(cs.ReadConcernLevel)(rc)
return rc
}
func writeConcernFromConnString(cs *connstring.ConnString) *writeconcern.WriteConcern {
var wc *writeconcern.WriteConcern
if len(cs.WString) > 0 {
if wc == nil {
wc = writeconcern.New()
}
writeconcern.WTagSet(cs.WString)(wc)
} else if cs.WNumberSet {
if wc == nil {
wc = writeconcern.New()
}
writeconcern.W(cs.WNumber)(wc)
}
if cs.JSet {
if wc == nil {
wc = writeconcern.New()
}
writeconcern.J(cs.J)(wc)
}
if cs.WTimeoutSet {
if wc == nil {
wc = writeconcern.New()
}
writeconcern.WTimeout(cs.WTimeout)(wc)
}
return wc
}
func readPreferenceFromConnString(cs *connstring.ConnString) (*readpref.ReadPref, error) {
var rp *readpref.ReadPref
var err error
options := make([]readpref.Option, 0, 1)
tagSets := tag.NewTagSetsFromMaps(cs.ReadPreferenceTagSets)
if len(tagSets) > 0 {
options = append(options, readpref.WithTagSets(tagSets...))
}
if cs.MaxStaleness != 0 {
options = append(options, readpref.WithMaxStaleness(cs.MaxStaleness))
}
if len(cs.ReadPreference) > 0 {
if rp == nil {
mode, _ := readpref.ModeFromString(cs.ReadPreference)
rp, err = readpref.New(mode, options...)
if err != nil {
return nil, err
}
}
}
return rp, nil
}
// ValidSession returns an error if the session doesn't belong to the client
func (c *Client) ValidSession(sess *session.Client) error {
if sess != nil && !uuid.Equal(sess.ClientID, c.id) {
return ErrWrongClient
}
return nil
}
// Database returns a handle for a given database.
func (c *Client) Database(name string, opts ...dbopt.Option) *Database {
return newDatabase(c, name, opts...)
}
// ConnectionString returns the connection string of the cluster the client is connected to.
func (c *Client) ConnectionString() string {
return c.connString.Original
}
// ListDatabases returns a ListDatabasesResult.
func (c *Client) ListDatabases(ctx context.Context, filter interface{}, opts ...listdbopt.ListDatabases) (ListDatabasesResult, error) {
if ctx == nil {
ctx = context.Background()
}
listDbOpts, sess, err := listdbopt.BundleListDatabases(opts...).Unbundle(true)
if err != nil {
return ListDatabasesResult{}, err
}
err = c.ValidSession(sess)
if err != nil {
return ListDatabasesResult{}, err
}
f, err := TransformDocument(filter)
if err != nil {
return ListDatabasesResult{}, err
}
cmd := command.ListDatabases{
Filter: f,
Opts: listDbOpts,
Session: sess,
Clock: c.clock,
}
res, err := dispatch.ListDatabases(
ctx, cmd,
c.topology,
description.ReadPrefSelector(readpref.Primary()),
c.id,
c.topology.SessionPool,
)
if err != nil {
return ListDatabasesResult{}, err
}
return (ListDatabasesResult{}).fromResult(res), nil
}
// ListDatabaseNames returns a slice containing the names of all of the databases on the server.
func (c *Client) ListDatabaseNames(ctx context.Context, filter interface{}, opts ...listdbopt.ListDatabases) ([]string, error) {
opts = append(opts, listdbopt.NameOnly(true))
res, err := c.ListDatabases(ctx, filter, opts...)
if err != nil {
return nil, err
}
names := make([]string, 0)
for _, spec := range res.Databases {
names = append(names, spec.Name)
}
return names, nil
}