-
Notifications
You must be signed in to change notification settings - Fork 56
storage: impl mongodb storage #757
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
hyprh
wants to merge
2
commits into
trpc-group:main
Choose a base branch
from
hyprh:mongodb
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+719
−0
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| module trpc.group/trpc-go/trpc-agent-go/storage/mongodb | ||
|
|
||
| go 1.21 | ||
|
|
||
| require ( | ||
| github.com/stretchr/testify v1.11.1 | ||
| go.mongodb.org/mongo-driver v1.17.1 | ||
| ) | ||
|
|
||
| require ( | ||
| github.com/davecgh/go-spew v1.1.1 // indirect | ||
| github.com/golang/snappy v0.0.4 // indirect | ||
| github.com/klauspost/compress v1.13.6 // indirect | ||
| github.com/montanaflynn/stats v0.7.1 // indirect | ||
| github.com/pmezard/go-difflib v1.0.0 // indirect | ||
| github.com/xdg-go/pbkdf2 v1.0.0 // indirect | ||
| github.com/xdg-go/scram v1.1.2 // indirect | ||
| github.com/xdg-go/stringprep v1.0.4 // indirect | ||
| github.com/youmark/pkcs8 v0.0.0-20240726163527-a2c0da244d78 // indirect | ||
| golang.org/x/crypto v0.26.0 // indirect | ||
| golang.org/x/sync v0.8.0 // indirect | ||
| golang.org/x/text v0.17.0 // indirect | ||
| gopkg.in/yaml.v3 v3.0.1 // indirect | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= | ||
| github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
| github.com/golang/snappy v0.0.4 h1:yAGX7huGHXlcLOEtBnF4w7FQwA26wojNCwOYAEhLjQM= | ||
| github.com/golang/snappy v0.0.4/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= | ||
| github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= | ||
| github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= | ||
| github.com/klauspost/compress v1.13.6 h1:P76CopJELS0TiO2mebmnzgWaajssP/EszplttgQxcgc= | ||
| github.com/klauspost/compress v1.13.6/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk= | ||
| github.com/montanaflynn/stats v0.7.1 h1:etflOAAHORrCC44V+aR6Ftzort912ZU+YLiSTuV8eaE= | ||
| github.com/montanaflynn/stats v0.7.1/go.mod h1:etXPPgVO6n31NxCd9KQUMvCM+ve0ruNzt6R8Bnaayow= | ||
| github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= | ||
| github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= | ||
| github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U= | ||
| github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U= | ||
| github.com/xdg-go/pbkdf2 v1.0.0 h1:Su7DPu48wXMwC3bs7MCNG+z4FhcyEuz5dlvchbq0B0c= | ||
| github.com/xdg-go/pbkdf2 v1.0.0/go.mod h1:jrpuAogTd400dnrH08LKmI/xc1MbPOebTwRqcT5RDeI= | ||
| github.com/xdg-go/scram v1.1.2 h1:FHX5I5B4i4hKRVRBCFRxq1iQRej7WO3hhBuJf+UUySY= | ||
| github.com/xdg-go/scram v1.1.2/go.mod h1:RT/sEzTbU5y00aCK8UOx6R7YryM0iF1N2MOmC3kKLN4= | ||
| github.com/xdg-go/stringprep v1.0.4 h1:XLI/Ng3O1Atzq0oBs3TWm+5ZVgkq2aqdlvP9JtoZ6c8= | ||
| github.com/xdg-go/stringprep v1.0.4/go.mod h1:mPGuuIYwz7CmR2bT9j4GbQqutWS1zV24gijq1dTyGkM= | ||
| github.com/youmark/pkcs8 v0.0.0-20240726163527-a2c0da244d78 h1:ilQV1hzziu+LLM3zUTJ0trRztfwgjqKnBWNtSRkbmwM= | ||
| github.com/youmark/pkcs8 v0.0.0-20240726163527-a2c0da244d78/go.mod h1:aL8wCCfTfSfmXjznFBSZNN13rSJjlIOI1fUNAtF7rmI= | ||
| github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= | ||
| go.mongodb.org/mongo-driver v1.17.1 h1:Wic5cJIwJgSpBhe3lx3+/RybR5PiYRMpVFgO7cOHyIM= | ||
| go.mongodb.org/mongo-driver v1.17.1/go.mod h1:wwWm/+BuOddhcq3n68LKRmgk2wXzmF6s0SFOa0GINL4= | ||
| golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= | ||
| golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= | ||
| golang.org/x/crypto v0.26.0 h1:RrRspgV4mU+YwB4FYnuBoKsUapNIL5cohGAmSH3azsw= | ||
| golang.org/x/crypto v0.26.0/go.mod h1:GY7jblb9wI+FOo5y8/S2oY4zWP07AkOJ4+jxCqdqn54= | ||
| golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= | ||
| golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= | ||
| golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= | ||
| golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= | ||
| golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= | ||
| golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= | ||
| golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ= | ||
| golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= | ||
| golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= | ||
| golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= | ||
| golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= | ||
| golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= | ||
| golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= | ||
| golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= | ||
| golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= | ||
| golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= | ||
| golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= | ||
| golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= | ||
| golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ= | ||
| golang.org/x/text v0.17.0 h1:XtiM5bkSOt+ewxlOE/aE/AKEHibwj/6gvWMl9Rsh0Qc= | ||
| golang.org/x/text v0.17.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY= | ||
| golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= | ||
| golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= | ||
| golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= | ||
| golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= | ||
| gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= | ||
| gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= | ||
| gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= | ||
| gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,253 @@ | ||
| // | ||
| // Tencent is pleased to support the open source community by making trpc-agent-go available. | ||
| // | ||
| // Copyright (C) 2025 Tencent. All rights reserved. | ||
| // | ||
| // trpc-agent-go is licensed under the Apache License Version 2.0. | ||
| // | ||
| // | ||
|
|
||
| // Package mongodb provides the MongoDB instance info management. | ||
| package mongodb | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "fmt" | ||
|
|
||
| "go.mongodb.org/mongo-driver/mongo" | ||
| "go.mongodb.org/mongo-driver/mongo/options" | ||
| ) | ||
|
|
||
| func init() { | ||
| mongodbRegistry = make(map[string][]ClientBuilderOpt) | ||
| } | ||
|
|
||
| var mongodbRegistry map[string][]ClientBuilderOpt | ||
|
|
||
| type clientBuilder func(ctx context.Context, builderOpts ...ClientBuilderOpt) (Client, error) | ||
|
|
||
| var globalBuilder clientBuilder = defaultClientBuilder | ||
|
|
||
| // SetClientBuilder sets the mongodb client builder. | ||
| func SetClientBuilder(builder clientBuilder) { | ||
| globalBuilder = builder | ||
| } | ||
|
|
||
| // GetClientBuilder gets the mongodb client builder. | ||
| func GetClientBuilder() clientBuilder { | ||
| return globalBuilder | ||
| } | ||
|
|
||
| // defaultClientBuilder is the default mongodb client builder. | ||
| // It creates a native MongoDB client using the official Go driver. | ||
| func defaultClientBuilder(ctx context.Context, builderOpts ...ClientBuilderOpt) (Client, error) { | ||
| o := &ClientBuilderOpts{} | ||
| for _, opt := range builderOpts { | ||
| opt(o) | ||
| } | ||
|
|
||
| if o.URI == "" { | ||
| return nil, errors.New("mongodb: URI is empty") | ||
| } | ||
|
|
||
| // Create MongoDB client options | ||
| clientOpts := options.Client().ApplyURI(o.URI) | ||
|
|
||
| // Connect to MongoDB | ||
| client, err := mongo.Connect(ctx, clientOpts) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("mongodb: connect failed: %w", err) | ||
| } | ||
|
|
||
| // Verify connection | ||
| if err := client.Ping(ctx, nil); err != nil { | ||
| client.Disconnect(ctx) | ||
| return nil, fmt.Errorf("mongodb: ping failed: %w", err) | ||
| } | ||
|
|
||
| return &nativeClient{client: client}, nil | ||
| } | ||
|
|
||
| // ClientBuilderOpt is the option for the mongodb client. | ||
| type ClientBuilderOpt func(*ClientBuilderOpts) | ||
|
|
||
| // ClientBuilderOpts is the options for the mongodb client. | ||
| type ClientBuilderOpts struct { | ||
| // URI is the mongodb connection string. | ||
| // Format: "mongodb://username:password@host:port/database?options" | ||
| URI string | ||
|
|
||
| // ExtraOptions is the extra options for the mongodb client. | ||
| // This is mainly used for customized mongodb client builders. | ||
| ExtraOptions []any | ||
| } | ||
|
|
||
| // WithClientBuilderDSN sets the mongodb connection URI for clientBuilder. | ||
| func WithClientBuilderDSN(uri string) ClientBuilderOpt { | ||
| return func(opts *ClientBuilderOpts) { | ||
| opts.URI = uri | ||
| } | ||
| } | ||
|
|
||
| // WithExtraOptions sets the mongodb client extra options for clientBuilder. | ||
| // This option is mainly used for customized mongodb client builders. | ||
| func WithExtraOptions(extraOptions ...any) ClientBuilderOpt { | ||
| return func(opts *ClientBuilderOpts) { | ||
| opts.ExtraOptions = append(opts.ExtraOptions, extraOptions...) | ||
| } | ||
| } | ||
|
|
||
| // RegisterMongoDBInstance registers a mongodb instance with the given options. | ||
| func RegisterMongoDBInstance(name string, opts ...ClientBuilderOpt) { | ||
| mongodbRegistry[name] = append(mongodbRegistry[name], opts...) | ||
| } | ||
|
|
||
| // GetMongoDBInstance gets the mongodb instance options by name. | ||
| func GetMongoDBInstance(name string) ([]ClientBuilderOpt, bool) { | ||
| instance, ok := mongodbRegistry[name] | ||
| return instance, ok | ||
| } | ||
|
|
||
| // Client defines the interface for MongoDB operations. | ||
| // This is a subset of the internal mongodb.Client interface, | ||
| // containing only the methods needed by the session layer. | ||
| type Client interface { | ||
| // InsertOne executes an insert command to insert a single document into the collection. | ||
| InsertOne(ctx context.Context, database string, coll string, document interface{}, | ||
| opts ...*options.InsertOneOptions) (*mongo.InsertOneResult, error) | ||
|
|
||
| // UpdateOne executes an update command to update at most one document in the collection. | ||
| UpdateOne(ctx context.Context, database string, coll string, filter interface{}, update interface{}, | ||
| opts ...*options.UpdateOptions) (*mongo.UpdateResult, error) | ||
|
|
||
| // DeleteOne executes a delete command to delete at most one document from the collection. | ||
| DeleteOne(ctx context.Context, database string, coll string, filter interface{}, | ||
| opts ...*options.DeleteOptions) (*mongo.DeleteResult, error) | ||
|
|
||
| // DeleteMany executes a delete command to delete documents from the collection. | ||
| DeleteMany(ctx context.Context, database string, coll string, filter interface{}, | ||
| opts ...*options.DeleteOptions) (*mongo.DeleteResult, error) | ||
|
|
||
| // FindOne executes a find command and returns a SingleResult for one document in the collection. | ||
| FindOne(ctx context.Context, database string, coll string, filter interface{}, | ||
| opts ...*options.FindOneOptions) *mongo.SingleResult | ||
|
|
||
| // Find executes a find command and returns a Cursor over the matching documents in the collection. | ||
| Find(ctx context.Context, database string, coll string, filter interface{}, | ||
| opts ...*options.FindOptions) (*mongo.Cursor, error) | ||
|
|
||
| // CountDocuments returns the number of documents in the collection. | ||
| CountDocuments(ctx context.Context, database string, coll string, filter interface{}, | ||
| opts ...*options.CountOptions) (int64, error) | ||
|
|
||
| // Transaction executes a transaction. | ||
| // The sf parameter is a function that receives a mongo.SessionContext for transaction operations. | ||
| Transaction(ctx context.Context, sf func(sc mongo.SessionContext) error, tOpts []*options.TransactionOptions, | ||
| opts ...*options.SessionOptions) error | ||
|
|
||
| // Disconnect closes the mongo client. | ||
| Disconnect(ctx context.Context) error | ||
| } | ||
|
|
||
| // ErrNoClientBuilder is returned when no client builder is set. | ||
| var ErrNoClientBuilder = errors.New("mongodb: no client builder set, please call SetClientBuilder first") | ||
|
|
||
| // NewClient creates a new mongodb client using the global builder. | ||
| func NewClient(ctx context.Context, opts ...ClientBuilderOpt) (Client, error) { | ||
| if globalBuilder == nil { | ||
| return nil, ErrNoClientBuilder | ||
| } | ||
| return globalBuilder(ctx, opts...) | ||
| } | ||
|
|
||
| // NewClientFromInstance creates a new mongodb client from a registered instance. | ||
| func NewClientFromInstance(ctx context.Context, instanceName string, extraOpts ...ClientBuilderOpt) (Client, error) { | ||
| if globalBuilder == nil { | ||
| return nil, ErrNoClientBuilder | ||
| } | ||
|
|
||
| builderOpts, ok := GetMongoDBInstance(instanceName) | ||
| if !ok { | ||
| return nil, errors.New("mongodb: instance not found: " + instanceName) | ||
| } | ||
|
|
||
| // Append extra options if provided | ||
| allOpts := make([]ClientBuilderOpt, 0, len(builderOpts)+len(extraOpts)) | ||
| allOpts = append(allOpts, builderOpts...) | ||
| allOpts = append(allOpts, extraOpts...) | ||
|
|
||
| return globalBuilder(ctx, allOpts...) | ||
| } | ||
|
|
||
| // nativeClient wraps *mongo.Client to implement the Client interface. | ||
| type nativeClient struct { | ||
| client *mongo.Client | ||
| } | ||
|
|
||
| // InsertOne implements Client.InsertOne. | ||
| func (c *nativeClient) InsertOne(ctx context.Context, database string, coll string, document interface{}, | ||
| opts ...*options.InsertOneOptions) (*mongo.InsertOneResult, error) { | ||
| return c.client.Database(database).Collection(coll).InsertOne(ctx, document, opts...) | ||
| } | ||
|
|
||
| // UpdateOne implements Client.UpdateOne. | ||
| func (c *nativeClient) UpdateOne(ctx context.Context, database string, coll string, filter interface{}, | ||
| update interface{}, opts ...*options.UpdateOptions) (*mongo.UpdateResult, error) { | ||
| return c.client.Database(database).Collection(coll).UpdateOne(ctx, filter, update, opts...) | ||
| } | ||
|
|
||
| // DeleteOne implements Client.DeleteOne. | ||
| func (c *nativeClient) DeleteOne(ctx context.Context, database string, coll string, filter interface{}, | ||
| opts ...*options.DeleteOptions) (*mongo.DeleteResult, error) { | ||
| return c.client.Database(database).Collection(coll).DeleteOne(ctx, filter, opts...) | ||
| } | ||
|
|
||
| // DeleteMany implements Client.DeleteMany. | ||
| func (c *nativeClient) DeleteMany(ctx context.Context, database string, coll string, filter interface{}, | ||
| opts ...*options.DeleteOptions) (*mongo.DeleteResult, error) { | ||
| return c.client.Database(database).Collection(coll).DeleteMany(ctx, filter, opts...) | ||
| } | ||
|
|
||
| // FindOne implements Client.FindOne. | ||
| func (c *nativeClient) FindOne(ctx context.Context, database string, coll string, filter interface{}, | ||
| opts ...*options.FindOneOptions) *mongo.SingleResult { | ||
| return c.client.Database(database).Collection(coll).FindOne(ctx, filter, opts...) | ||
| } | ||
|
|
||
| // Find implements Client.Find. | ||
| func (c *nativeClient) Find(ctx context.Context, database string, coll string, filter interface{}, | ||
| opts ...*options.FindOptions) (*mongo.Cursor, error) { | ||
| return c.client.Database(database).Collection(coll).Find(ctx, filter, opts...) | ||
| } | ||
|
|
||
| // CountDocuments implements Client.CountDocuments. | ||
| func (c *nativeClient) CountDocuments(ctx context.Context, database string, coll string, filter interface{}, | ||
| opts ...*options.CountOptions) (int64, error) { | ||
| return c.client.Database(database).Collection(coll).CountDocuments(ctx, filter, opts...) | ||
| } | ||
|
|
||
| // Transaction implements Client.Transaction. | ||
| func (c *nativeClient) Transaction(ctx context.Context, sf func(sc mongo.SessionContext) error, | ||
| tOpts []*options.TransactionOptions, opts ...*options.SessionOptions) error { | ||
| session, err := c.client.StartSession(opts...) | ||
| if err != nil { | ||
| return fmt.Errorf("mongodb: start session failed: %w", err) | ||
| } | ||
| defer session.EndSession(ctx) | ||
|
|
||
| var txOpt *options.TransactionOptions | ||
| if len(tOpts) > 0 { | ||
| txOpt = tOpts[0] | ||
| } | ||
|
|
||
| _, err = session.WithTransaction(ctx, func(sc mongo.SessionContext) (interface{}, error) { | ||
| return nil, sf(sc) | ||
| }, txOpt) | ||
| return err | ||
| } | ||
|
|
||
| // Disconnect implements Client.Disconnect. | ||
| func (c *nativeClient) Disconnect(ctx context.Context) error { | ||
| return c.client.Disconnect(ctx) | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个options考虑单独搞一个option.go的文件来放,查看配置时方便点
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done