/
session.go
260 lines (220 loc) · 8.19 KB
/
session.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
// 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 (
"errors"
"context"
"github.com/mongodb/mongo-go-driver/core/command"
"github.com/mongodb/mongo-go-driver/core/description"
"github.com/mongodb/mongo-go-driver/core/dispatch"
"github.com/mongodb/mongo-go-driver/core/session"
"github.com/mongodb/mongo-go-driver/core/topology"
"github.com/mongodb/mongo-go-driver/mongo/aggregateopt"
"github.com/mongodb/mongo-go-driver/mongo/changestreamopt"
"github.com/mongodb/mongo-go-driver/mongo/countopt"
"github.com/mongodb/mongo-go-driver/mongo/dbopt"
"github.com/mongodb/mongo-go-driver/mongo/deleteopt"
"github.com/mongodb/mongo-go-driver/mongo/distinctopt"
"github.com/mongodb/mongo-go-driver/mongo/dropcollopt"
"github.com/mongodb/mongo-go-driver/mongo/findopt"
"github.com/mongodb/mongo-go-driver/mongo/indexopt"
"github.com/mongodb/mongo-go-driver/mongo/insertopt"
"github.com/mongodb/mongo-go-driver/mongo/listcollectionopt"
"github.com/mongodb/mongo-go-driver/mongo/listdbopt"
"github.com/mongodb/mongo-go-driver/mongo/replaceopt"
"github.com/mongodb/mongo-go-driver/mongo/runcmdopt"
"github.com/mongodb/mongo-go-driver/mongo/transactionopt"
"github.com/mongodb/mongo-go-driver/mongo/updateopt"
)
// ErrWrongClient is returned when a user attempts to pass in a session created by a different client than
// the method call is using.
var ErrWrongClient = errors.New("session was not created by this client")
// Session represents a set of sequential operations executed by an application that are related in some way.
type Session struct {
aggregateopt.AggregateSessionOpt
changestreamopt.ChangeStreamSessionOpt
countopt.CountSessionOpt
deleteopt.DeleteSessionOpt
distinctopt.DistinctSessionOpt
dbopt.DropDBSessionOpt
findopt.FindSessionOpt
dropcollopt.DropCollSessionOpt
listcollectionopt.ListCollectionsSessionOpt
updateopt.UpdateSessionOpt
replaceopt.ReplaceSessionOpt
indexopt.IndexSessionOpt
insertopt.InsertSessionOpt
runcmdopt.RunCmdSessionOpt
listdbopt.ListDatabasesSessionOpt
*session.Client
topo *topology.Topology
didCommitAfterStart bool // true if commit was called after start with no other operations
}
var (
_ aggregateopt.Aggregate = (*Session)(nil)
_ countopt.Count = (*Session)(nil)
_ changestreamopt.ChangeStream = (*Session)(nil)
_ deleteopt.Delete = (*Session)(nil)
_ distinctopt.Distinct = (*Session)(nil)
_ dbopt.DropDB = (*Session)(nil)
_ findopt.Find = (*Session)(nil)
_ findopt.One = (*Session)(nil)
_ findopt.UpdateOne = (*Session)(nil)
_ findopt.ReplaceOne = (*Session)(nil)
_ findopt.DeleteOne = (*Session)(nil)
_ indexopt.Create = (*Session)(nil)
_ indexopt.Drop = (*Session)(nil)
_ indexopt.List = (*Session)(nil)
_ dropcollopt.DropColl = (*Session)(nil)
_ listcollectionopt.ListCollections = (*Session)(nil)
_ insertopt.Many = (*Session)(nil)
_ insertopt.One = (*Session)(nil)
_ updateopt.Update = (*Session)(nil)
_ replaceopt.Replace = (*Session)(nil)
_ runcmdopt.Option = (*Session)(nil)
_ listdbopt.ListDatabases = (*Session)(nil)
)
// EndSession ends the session.
func (s *Session) EndSession(ctx context.Context) {
if s.TransactionInProgress() {
// ignore all errors aborting during an end session
_ = s.AbortTransaction(ctx)
}
s.Client.EndSession()
}
// StartTransaction starts a transaction for this session.
func (s *Session) StartTransaction(opts ...transactionopt.Transaction) error {
err := s.CheckStartTransaction()
if err != nil {
return err
}
s.didCommitAfterStart = false
tranOpts, err := transactionopt.BundleTransaction(opts...).Unbundle(true)
if err != nil {
return err
}
return s.Client.StartTransaction(tranOpts...)
}
// CommitTransaction commits the sesson's transaction.
func (s *Session) CommitTransaction(ctx context.Context) error {
err := s.CheckCommitTransaction()
if err != nil {
return err
}
// Do not run the commit command if the transaction is in started state
if s.TransactionStarting() || s.didCommitAfterStart {
s.didCommitAfterStart = true
return s.Client.CommitTransaction()
}
if s.Client.TransactionCommitted() {
s.RetryingCommit = true
}
cmd := command.CommitTransaction{
Session: s.Client,
}
// Hack to ensure that session stays in committed state
if s.TransactionCommitted() {
s.Committing = true
defer func() {
s.Committing = false
}()
}
_, err = dispatch.CommitTransaction(ctx, cmd, s.topo, description.WriteSelector())
if err == nil {
return s.Client.CommitTransaction()
}
return err
}
// AbortTransaction aborts the session's transaction, returning any errors and error codes
func (s *Session) AbortTransaction(ctx context.Context) error {
err := s.CheckAbortTransaction()
if err != nil {
return err
}
cmd := command.AbortTransaction{
Session: s.Client,
}
s.Aborting = true
_, err = dispatch.AbortTransaction(ctx, cmd, s.topo, description.WriteSelector())
_ = s.Client.AbortTransaction()
return err
}
// ConvertAggregateSession implements the AggregateSession interface.
func (s *Session) ConvertAggregateSession() *session.Client {
return s.Client
}
// ConvertChangeStreamSession implements the ChangeStreamSession interface.
func (s *Session) ConvertChangeStreamSession() *session.Client {
return s.Client
}
// ConvertCountSession implements the CountSession interface.
func (s *Session) ConvertCountSession() *session.Client {
return s.Client
}
// ConvertDeleteSession implements the DeleteSession interface.
func (s *Session) ConvertDeleteSession() *session.Client {
return s.Client
}
// ConvertDropDBSession implements the DropDBSession interface.
func (s *Session) ConvertDropDBSession() *session.Client {
return s.Client
}
// ConvertDropCollSession implements the DropCollSession interface
func (s *Session) ConvertDropCollSession() *session.Client {
return s.Client
}
// ConvertDistinctSession implements the DistinctSession interface.
func (s *Session) ConvertDistinctSession() *session.Client {
return s.Client
}
// ConvertFindSession implements the FindSession interface.
func (s *Session) ConvertFindSession() *session.Client {
return s.Client
}
// ConvertFindOneSession implements the FindOneSession interface.
func (s *Session) ConvertFindOneSession() *session.Client {
return s.Client
}
// ConvertListCollectionsSession implements the ListCollectionsSession interface.
func (s *Session) ConvertListCollectionsSession() *session.Client {
return s.Client
}
// ConvertUpdateOneSession implements the UpdateOneSession interface.
func (s *Session) ConvertUpdateOneSession() *session.Client {
return s.Client
}
// ConvertReplaceOneSession implements the ReplaceOneSession interface.
func (s *Session) ConvertReplaceOneSession() *session.Client {
return s.Client
}
// ConvertDeleteOneSession implements the DeleteOneSession interface.
func (s *Session) ConvertDeleteOneSession() *session.Client {
return s.Client
}
// ConvertIndexSession implements the IndexSession interface.
func (s *Session) ConvertIndexSession() *session.Client {
return s.Client
}
// ConvertInsertSession implements the InsertManySession and InsertOneSession interfaces.
func (s *Session) ConvertInsertSession() *session.Client {
return s.Client
}
// ConvertUpdateSession implements the UpdateSession interface.
func (s *Session) ConvertUpdateSession() *session.Client {
return s.Client
}
// ConvertReplaceSession implements the ReplaceSession interface.
func (s *Session) ConvertReplaceSession() *session.Client {
return s.Client
}
// ConvertRunCmdSession implements the RunCmdSession interface.
func (s *Session) ConvertRunCmdSession() *session.Client {
return s.Client
}
// ConvertListDatabasesSession implements the ListDatabasesSession interface
func (s *Session) ConvertListDatabasesSession() *session.Client {
return s.Client
}