Skip to content

Commit 39760b5

Browse files
authored
Merge pull request #3 from foomo/feature/purge-gc
feat: add garbage collection
2 parents acee681 + f4cdb5d commit 39760b5

File tree

3 files changed

+32
-19
lines changed

3 files changed

+32
-19
lines changed

README.md

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
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
```
2930
db.locks.createIndex( { "exclusive.lockId": 1 } )
3031
db.locks.createIndex( { "exclusive.expiresAt": 1 } )
@@ -45,37 +46,28 @@ package main
4546
import (
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

5757
func 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

lock.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -610,6 +610,23 @@ func (c *Client) Renew(ctx context.Context, lockId string, ttl uint) ([]LockStat
610610
return statuses, nil
611611
}
612612

613+
// GC removes all lock entries in the collection that have expired
614+
// Specifically, this removes lock entries where exclusive.lockId is nil AND shared.count is 0
615+
func (c *Client) GC(ctx context.Context) error {
616+
// Get all empty locks and remove
617+
selector := bson.M{
618+
"exclusive.lockId": nil,
619+
"shared.count": 0,
620+
}
621+
622+
_, err := c.collection.DeleteMany(ctx, selector)
623+
if err != nil {
624+
return err
625+
}
626+
627+
return nil
628+
}
629+
613630
// ------------------------------------------------------------------------- //
614631

615632
// xUnlock unlocks an exclusive lock on a resource.

purge.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,11 @@ func (p *purger) Purge(ctx context.Context) ([]LockStatus, error) {
4949
allUnlocked = append(allUnlocked, unlocked...)
5050
}
5151

52+
// Garbage collect unused lock records
53+
err = p.client.GC(ctx)
54+
if err != nil {
55+
return allUnlocked, err
56+
}
57+
5258
return allUnlocked, nil
5359
}

0 commit comments

Comments
 (0)