-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathsession.go
226 lines (182 loc) · 4.52 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
package lungo
import (
"context"
"errors"
"fmt"
"sync"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo/options"
)
type sessionKey struct{}
// ErrSessionEnded is returned if the session has been ended.
var ErrSessionEnded = errors.New("session ended")
// SessionContext provides a mongo compatible session context.
type SessionContext struct {
context.Context
*Session
}
// Session provides a mongo compatible way to handle transactions.
type Session struct {
engine *Engine
txn *Transaction
ended bool
mutex sync.Mutex
}
// ID implements the ISession.ID method.
func (s *Session) ID() bson.Raw {
return nil
}
// AbortTransaction implements the ISession.AbortTransaction method.
func (s *Session) AbortTransaction(context.Context) error {
// acquire lock
s.mutex.Lock()
defer s.mutex.Unlock()
// check if ended
if s.ended {
return ErrSessionEnded
}
// abort and unset transaction
if s.txn != nil {
s.engine.Abort(s.txn)
s.txn = nil
}
return nil
}
// AdvanceClusterTime implements the ISession.AdvanceClusterTime method.
func (s *Session) AdvanceClusterTime(bson.Raw) error {
panic("lungo: not implemented")
}
// AdvanceOperationTime implements the ISession.AdvanceOperationTime method.
func (s *Session) AdvanceOperationTime(*primitive.Timestamp) error {
panic("lungo: not implemented")
}
// Client implements the ISession.Client method.
func (s *Session) Client() IClient {
return &Client{
engine: s.engine,
}
}
// ClusterTime implements the ISession.ClusterTime method.
func (s *Session) ClusterTime() bson.Raw {
panic("lungo: not implemented")
}
// CommitTransaction implements the ISession.CommitTransaction method.
func (s *Session) CommitTransaction(context.Context) error {
// acquire lock
s.mutex.Lock()
defer s.mutex.Unlock()
// check if ended
if s.ended {
return ErrSessionEnded
}
// check transaction
if s.txn == nil {
return fmt.Errorf("missing transaction")
}
// get and unset transaction
txn := s.txn
s.txn = nil
// commit transaction
err := s.engine.Commit(txn)
if err != nil {
return err
}
return nil
}
// EndSession implements the ISession.EndSession method.
func (s *Session) EndSession(context.Context) {
// acquire lock
s.mutex.Lock()
defer s.mutex.Unlock()
// check if ended
if s.ended {
return
}
// abort and unset transaction
if s.txn != nil {
s.engine.Abort(s.txn)
s.txn = nil
}
// set flag
s.ended = true
}
// OperationTime implements the ISession.OperationTime method.
func (s *Session) OperationTime() *primitive.Timestamp {
panic("lungo: not implemented")
}
// StartTransaction implements the ISession.StartTransaction method.
func (s *Session) StartTransaction(opts ...*options.TransactionOptions) error {
// acquire lock
s.mutex.Lock()
defer s.mutex.Unlock()
// check if ended
if s.ended {
return ErrSessionEnded
}
// merge options
opt := options.MergeTransactionOptions(opts...)
// assert supported options
assertOptions(opt, map[string]string{
"ReadConcern": ignored,
"ReadPreference": ignored,
"WriteConcern": ignored,
"MaxCommitTime": ignored,
})
// check transaction
if s.txn != nil {
return fmt.Errorf("existing transaction")
}
// create transaction
txn, err := s.engine.Begin(nil, true)
if err != nil {
return err
}
// set transaction
s.txn = txn
return nil
}
// WithTransaction implements the ISession.WithTransaction method.
func (s *Session) WithTransaction(ctx context.Context, fn func(ISessionContext) (interface{}, error), opts ...*options.TransactionOptions) (interface{}, error) {
// do not take locks as we only use safe functions
// merge options
opt := options.MergeTransactionOptions(opts...)
// assert supported options
assertOptions(opt, map[string]string{
"ReadConcern": ignored,
"ReadPreference": ignored,
"WriteConcern": ignored,
"MaxCommitTime": ignored,
})
// start transaction
err := s.StartTransaction(opt)
if err != nil {
return nil, err
}
// ensure abort
defer func() {
_ = s.AbortTransaction(ctx)
}()
// yield transaction
res, err := fn(&SessionContext{
Context: context.WithValue(ensureContext(ctx), sessionKey{}, s),
Session: s,
})
if err != nil {
return nil, err
}
// commit transaction
err = s.CommitTransaction(ctx)
if err != nil {
return nil, err
}
return res, nil
}
// Transaction will return the active transaction or nil if no transaction has
// been started.
func (s *Session) Transaction() *Transaction {
// acquire lock
s.mutex.Lock()
defer s.mutex.Unlock()
return s.txn
}