/
conn.go
169 lines (126 loc) · 4.26 KB
/
conn.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
package mongo
import (
"context"
"strings"
"github.com/americanas-go/log"
"go.mongodb.org/mongo-driver/event"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/x/mongo/driver/connstring"
)
type Conn struct {
ClientOptions *options.ClientOptions
Client *mongo.Client
Database *mongo.Database
}
type ClientOptionsPlugin func(context.Context, *options.ClientOptions) error
type ClientPlugin func(context.Context, *mongo.Client) error
type Plugin func(context.Context) (ClientOptionsPlugin, ClientPlugin)
func NewConn(ctx context.Context, plugins ...Plugin) (*Conn, error) {
logger := log.FromContext(ctx)
o, err := NewOptions()
if err != nil {
logger.Fatalf(err.Error())
}
return NewConnWithOptions(ctx, o, plugins...)
}
func NewConnWithOptions(ctx context.Context, o *Options, plugins ...Plugin) (conn *Conn, err error) {
logger := log.FromContext(ctx)
var clientOptionsPlugins []ClientOptionsPlugin
var clientPlugins []ClientPlugin
for _, plugin := range plugins {
clientOptionsPlugin, clientPlugin := plugin(ctx)
if clientOptionsPlugin != nil {
clientOptionsPlugins = append(clientOptionsPlugins, clientOptionsPlugin)
}
if clientPlugin != nil {
clientPlugins = append(clientPlugins, clientPlugin)
}
}
co := clientOptions(ctx, o)
for _, clientOptionsPlugin := range clientOptionsPlugins {
if err := clientOptionsPlugin(ctx, co); err != nil {
logger.Fatalf(err.Error())
}
}
var client *mongo.Client
var database *mongo.Database
client, database, err = newClient(ctx, co)
if err != nil {
return nil, err
}
for _, clientPlugin := range clientPlugins {
if err := clientPlugin(ctx, client); err != nil {
logger.Fatalf(err.Error())
}
}
conn = &Conn{
ClientOptions: co,
Client: client,
Database: database,
}
return conn, err
}
func newClient(ctx context.Context, co *options.ClientOptions) (client *mongo.Client, database *mongo.Database, err error) {
logger := log.FromContext(ctx)
client, err = mongo.Connect(ctx, co)
if err != nil {
return nil, nil, err
}
// Check the connection
err = client.Ping(ctx, nil)
if err != nil {
return nil, nil, err
}
var connFields connstring.ConnString
connFields, err = connstring.Parse(co.GetURI())
if err != nil {
return nil, nil, err
}
database = client.Database(connFields.Database)
logger.Infof("Connected to MongoDB server: %v", strings.Join(connFields.Hosts, ","))
return client, database, err
}
func clientOptions(ctx context.Context, o *Options) *options.ClientOptions {
logger := log.FromContext(ctx)
clientOptions := options.Client().ApplyURI(o.Uri)
clientOptions.SetMonitor(&event.CommandMonitor{
Started: func(ctx context.Context, startedEvent *event.CommandStartedEvent) {
logger.Debugf("mongodb cmd - %v %s %s %v", startedEvent.ConnectionID, startedEvent.CommandName, startedEvent.DatabaseName, startedEvent.RequestID)
},
Succeeded: func(ctx context.Context, succeededEvent *event.CommandSucceededEvent) {
logger.Debugf("mongodb cmd - %v %s %vus %v", succeededEvent.ConnectionID, succeededEvent.CommandName, succeededEvent.DurationNanos, succeededEvent.RequestID)
},
Failed: func(ctx context.Context, failedEvent *event.CommandFailedEvent) {
logger.Errorf("mongodb cmd - %v %s %s %v", failedEvent.ConnectionID, failedEvent.CommandName, failedEvent.Failure, failedEvent.RequestID)
},
})
clientOptions.SetPoolMonitor(&event.PoolMonitor{
Event: func(poolEvent *event.PoolEvent) {
logger.Debugf("mongodb conn pool - %v %s %s %s", poolEvent.ConnectionID, poolEvent.Type, poolEvent.Reason, poolEvent.Address)
},
})
if o.Auth != nil {
setAuthOptions(o, clientOptions)
}
return clientOptions
}
func setAuthOptions(o *Options, clientOptions *options.ClientOptions) {
if o.Auth.Password == "" && o.Auth.Username == "" {
return
}
if clientOptions.Auth == nil {
clientOptions.Auth = &options.Credential{}
}
if o.Auth.Password != "" {
clientOptions.Auth.Password = o.Auth.Password
clientOptions.Auth.PasswordSet = true
}
if o.Auth.Username != "" {
clientOptions.Auth.Username = o.Auth.Username
}
if clientOptions.Auth.AuthSource == "" {
connFields, _ := connstring.Parse(clientOptions.GetURI())
clientOptions.Auth.AuthSource = connFields.Database
}
}