-
Notifications
You must be signed in to change notification settings - Fork 0
/
session.go
76 lines (68 loc) · 3.1 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
/*
Copyright 2020 The Qmgo Authors.
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
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package mgo类
import (
"context"
opts "github.com/888go/qmgo/options"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/x/mongo/driver"
)
// XSession事务 是一个结构体,表示 MongoDB 的逻辑会话 md5:a17367bc3a251e77
type XSession事务 struct {
session mongo.Session
}
// X开始事务 开始一个事务
// 预条件:
// - MongoDB服务器版本大于等于v4.0
// - MongoDB服务器的拓扑结构不是单节点
// 同时需要注意:
// - 确保回调中的所有操作将sessCtx作为上下文参数
// - 如果不再使用session,别忘了调用EndSession
// - 如果回调中的操作耗时超过(包括等于)120秒,这些操作将不会生效
// - 如果回调中的操作返回qmgo.ErrTransactionRetry错误,
// 整个事务将会重试,因此这个事务必须是幂等的
// - 如果回调中的操作返回qmgo.ErrTransactionNotSupported错误,
// - 如果ctx参数中已经附加了一个Session,它将被此session替换。
//
// md5:7a854b4c45212490
func (s *XSession事务) X开始事务(上下文 context.Context, 回调函数 func(事务上下文 context.Context) (interface{}, error), 可选选项 ...*opts.TransactionOptions) (interface{}, error) {
transactionOpts := options.Transaction()
if len(可选选项) > 0 && 可选选项[0].TransactionOptions != nil {
transactionOpts = 可选选项[0].TransactionOptions
}
result, err := s.session.WithTransaction(上下文, wrapperCustomCb(回调函数), transactionOpts)
if err != nil {
return nil, err
}
return result, nil
}
// X结束Session 会终止任何现有的事务并关闭会话。 md5:2ee8849531868b7e
func (s *XSession事务) X结束Session(上下文 context.Context) {
s.session.EndSession(上下文)
}
// X中止事务 会取消此会话中的活动事务。如果此会话没有活动事务,或者事务已经提交或中止,此方法将返回错误。
// md5:ca9bc056086304f0
func (s *XSession事务) X中止事务(上下文 context.Context) error {
return s.session.AbortTransaction(上下文)
}
// wrapperCustomF 将调用者的回调函数包装成mongo驱动所需的函数 md5:8df643188861ec8b
func wrapperCustomCb(cb func(ctx context.Context) (interface{}, error)) func(sessCtx mongo.SessionContext) (interface{}, error) {
return func(sessCtx mongo.SessionContext) (interface{}, error) {
result, err := cb(sessCtx)
if err == X错误_事务_重试 {
return nil, mongo.CommandError{Labels: []string{driver.TransientTransactionError}}
}
return result, err
}
}