forked from mongodb/mongo-go-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
129 lines (102 loc) · 3.16 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
// 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/mongo/connstring"
"github.com/mongodb/mongo-go-driver/mongo/private/cluster"
"github.com/mongodb/mongo-go-driver/mongo/private/ops"
"github.com/mongodb/mongo-go-driver/mongo/readconcern"
"github.com/mongodb/mongo-go-driver/mongo/readpref"
"github.com/mongodb/mongo-go-driver/mongo/writeconcern"
)
const defaultLocalThreshold = 15 * time.Millisecond
// Client performs operations on a given cluster.
type Client struct {
cluster *cluster.Cluster
connString connstring.ConnString
localThreshold time.Duration
readPreference *readpref.ReadPref
readConcern *readconcern.ReadConcern
writeConcern *writeconcern.WriteConcern
}
// 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 NewClientFromConnString(cs)
}
// NewClientFromConnString creates a new client to connect to a cluster specified by the connection string.
func NewClientFromConnString(cs connstring.ConnString) (*Client, error) {
clst, err := cluster.New(cluster.WithConnString(cs))
if err != nil {
return nil, err
}
// TODO GODRIVER-92: Allow custom localThreshold
client := &Client{
cluster: clst,
connString: cs,
localThreshold: defaultLocalThreshold,
readPreference: readpref.Primary(),
readConcern: readConcernFromConnString(&cs),
writeConcern: writeConcernFromConnString(&cs),
}
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
}
// Database returns a handle for a given database.
func (client *Client) Database(name string) *Database {
return newDatabase(client, name)
}
// ConnectionString returns the connection string of the cluster the client is connected to.
func (client *Client) ConnectionString() string {
return client.connString.Original
}
func (client *Client) selectServer(ctx context.Context, selector cluster.ServerSelector,
readPref *readpref.ReadPref) (*ops.SelectedServer, error) {
s, err := client.cluster.SelectServer(ctx, selector, readPref)
if err != nil {
return nil, err
}
return s, nil
}