From 1c403c2b8f68c0f05023ad60f229799ed870163a Mon Sep 17 00:00:00 2001 From: Bostjan Marusic Date: Mon, 2 Oct 2023 16:08:50 +0200 Subject: [PATCH 1/3] implemented simple gc of released loc entries in mongo collection --- lock.go | 15 +++++++++++++++ purge.go | 5 +++++ 2 files changed, 20 insertions(+) diff --git a/lock.go b/lock.go index 1eb4268..4511155 100644 --- a/lock.go +++ b/lock.go @@ -684,6 +684,21 @@ func (c *Client) sUnlock(ctx context.Context, resourceName, lockId string) error return nil } +// Gc removes all lock entries in the collection that have expired +// Specifically this removes lock entries where exclusive.lockId is nil AND shared.count is 0 +func (c *Client) Gc(ctx context.Context) error { + // Get all empty locks and remove + selector := bson.A{ + bson.D{{Key: "exclusive.lockId", Value: nil}}, + bson.D{{Key: "shared.count", Value: 0}}, + } + _, err := c.collection.DeleteMany(ctx, selector) + if err != nil { + return err + } + return nil +} + // lockFromDetails creates a lock struct from a lockId and a LockDetails struct. func lockFromDetails(lockId string, ld LockDetails) lock { now := time.Now() diff --git a/purge.go b/purge.go index b7ed54b..c605ecc 100644 --- a/purge.go +++ b/purge.go @@ -45,6 +45,11 @@ func (p *purger) Purge(ctx context.Context) ([]LockStatus, error) { } allUnlocked = append(allUnlocked, unlocked...) } + // Garbage collect unused lock records + err = p.client.Gc(ctx) + if err != nil { + return allUnlocked, err + } return allUnlocked, nil } From c9f6092fab3607b9fbd8254888463bf35d0026bf Mon Sep 17 00:00:00 2001 From: Bostjan Marusic Date: Mon, 2 Oct 2023 16:58:45 +0200 Subject: [PATCH 2/3] fixed/simplified selector --- lock.go | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/lock.go b/lock.go index 4511155..b80486f 100644 --- a/lock.go +++ b/lock.go @@ -688,10 +688,9 @@ func (c *Client) sUnlock(ctx context.Context, resourceName, lockId string) error // Specifically this removes lock entries where exclusive.lockId is nil AND shared.count is 0 func (c *Client) Gc(ctx context.Context) error { // Get all empty locks and remove - selector := bson.A{ - bson.D{{Key: "exclusive.lockId", Value: nil}}, - bson.D{{Key: "shared.count", Value: 0}}, - } + selector := bson.M{"exclusive.lockId": nil, + "shared.count": 0} + _, err := c.collection.DeleteMany(ctx, selector) if err != nil { return err From ad5eefdde8ae3df87e091365f3352c5b57cb5a1b Mon Sep 17 00:00:00 2001 From: Bostjan Marusic Date: Mon, 2 Oct 2023 16:59:35 +0200 Subject: [PATCH 3/3] fixed/simplified selector --- lock.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lock.go b/lock.go index b80486f..196fc51 100644 --- a/lock.go +++ b/lock.go @@ -688,8 +688,10 @@ func (c *Client) sUnlock(ctx context.Context, resourceName, lockId string) error // Specifically this removes lock entries where exclusive.lockId is nil AND shared.count is 0 func (c *Client) Gc(ctx context.Context) error { // Get all empty locks and remove - selector := bson.M{"exclusive.lockId": nil, - "shared.count": 0} + selector := bson.M{ + "exclusive.lockId": nil, + "shared.count": 0, + } _, err := c.collection.DeleteMany(ctx, selector) if err != nil {