forked from coze-cloud/clerk
/
connection.go
68 lines (55 loc) 路 1.56 KB
/
connection.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
package mongodb
import (
"context"
"time"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/mongo/readconcern"
"go.mongodb.org/mongo-driver/mongo/readpref"
"go.mongodb.org/mongo-driver/mongo/writeconcern"
)
type MongodbConnection struct {
ctx context.Context
client *mongo.Client
}
func NewMongoConnection(ctx context.Context, url string) (*MongodbConnection, error) {
var err error
client, err := mongo.Connect(ctx, options.Client().ApplyURI(url))
if err != nil {
return nil, err
}
timeoutCtx, timeoutCancel := context.WithTimeout(ctx, 5*time.Second)
defer timeoutCancel()
if err = client.Ping(timeoutCtx, readpref.Primary()); err != nil {
return nil, err
}
return &MongodbConnection{
ctx: ctx,
client: client,
}, nil
}
type MongodbTransactionFunc func(ctx context.Context) error
func (c *MongodbConnection) WithTransaction(ctx context.Context, fn MongodbTransactionFunc) error {
session, err := c.client.StartSession()
if err != nil {
return err
}
defer session.EndSession(context.Background())
wc := writeconcern.New(writeconcern.WMajority())
rc := readconcern.Snapshot()
txnOpts := options.
Transaction().
SetWriteConcern(wc).
SetReadConcern(rc)
sessionCallback := func(sessionContext mongo.SessionContext) (any, error) {
return nil, fn(sessionContext)
}
_, err = session.WithTransaction(ctx, sessionCallback, txnOpts)
return err
}
func (c *MongodbConnection) Close(handler func(err error)) {
err := c.client.Disconnect(c.ctx)
if handler != nil {
handler(err)
}
}