11# Distributed Locks in MongoDB
22
3+ [ ![ Build Status] ( https://github.com/foomo/mongo-lock/actions/workflows/test.yml/badge.svg?branch=main )] ( https://github.com/foomo/mongo-lock/actions/workflows/test.yml )
34[ ![ Go Report Card] ( https://goreportcard.com/badge/github.com/foomo/mongo-lock )] ( https://goreportcard.com/report/github.com/foomo/mongo-lock )
45[ ![ GoDoc] ( https://godoc.org/github.com/foomo/mongo-lock?status.svg )] ( https://godoc.org/github.com/foomo/mongo-lock )
56
@@ -24,7 +25,7 @@ db.locks.createIndex( { resource: 1 }, { unique: true } )
2425```
2526
2627#### Recommended Indexes
27- The following indexes are recommend to help the performance of certain queries:
28+ The following indexes are recommended to help the performance of certain queries:
2829```
2930db.locks.createIndex( { "exclusive.lockId": 1 } )
3031db.locks.createIndex( { "exclusive.expiresAt": 1 } )
@@ -45,37 +46,28 @@ package main
4546import (
4647 " context"
4748 " log"
48- " time"
4949
50- " go.mongodb.org/mongo-driver/mongo"
51- " go.mongodb.org/mongo-driver/mongo/options"
52- " go.mongodb.org/mongo-driver/mongo/writeconcern"
50+ " go.mongodb.org/mongo-driver/v2/ mongo"
51+ " go.mongodb.org/mongo-driver/v2/ mongo/options"
52+ " go.mongodb.org/mongo-driver/v2/ mongo/writeconcern"
5353
5454 " github.com/foomo/mongo-lock"
5555)
5656
5757func main () {
58- // Create a Mongo session and set the write mode to "majority".
58+ // Create a Mongo client and set the write concern to "majority".
5959 mongoUrl := " youMustProvideThis"
6060 database := " dbName"
6161 collection := " collectionName"
6262
63- ctx , cancel := context.WithTimeout (context.Background (), time.Second *30 )
64- defer cancel ()
65-
66- m , err := mongo.Connect (ctx, options.Client ().
63+ m , err := mongo.Connect (options.Client ().
6764 ApplyURI (mongoUrl).
68- SetWriteConcern (writeconcern.New (writeconcern.WMajority ())))
69-
65+ SetWriteConcern (writeconcern.Majority ()))
7066 if err != nil {
7167 log.Fatal (err)
7268 }
7369
74- defer func () {
75- if err = m.Disconnect (ctx); err != nil {
76- panic (err)
77- }
78- }()
70+ ctx := context.Background ()
7971
8072 // Configure the client for the database and collection the lock will go into.
8173 col := m.Database (database).Collection (collection)
@@ -106,8 +98,6 @@ func main() {
10698 log.Fatal (err)
10799 }
108100}
109-
110-
111101```
112102
113103## How It Works
0 commit comments