Skip to content
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

fix!: Unlock doesn't release connection #10

Merged
merged 1 commit into from
Aug 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
"log"
"os"

pglock "github.com/allisson/go-pglock/v2"
"github.com/allisson/go-pglock/v3"
_ "github.com/lib/pq"
)

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/allisson/go-pglock/v2
module github.com/allisson/go-pglock/v3

go 1.15

Expand Down
12 changes: 7 additions & 5 deletions lock.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ type Locker interface {
Lock(ctx context.Context) (bool, error)
WaitAndLock(ctx context.Context) error
Unlock(ctx context.Context) error
Close() error
}

// Lock implements the Locker interface.
Expand Down Expand Up @@ -37,14 +38,15 @@ func (l *Lock) WaitAndLock(ctx context.Context) error {
return err
}

// Unlock releases the lock and DB connection.
// Unlock releases the lock.
func (l *Lock) Unlock(ctx context.Context) error {
sqlQuery := "SELECT pg_advisory_unlock($1)"
_, err := l.conn.ExecContext(ctx, sqlQuery, l.id)
if err != nil {
return err
}
// Returns the connection to the connection pool
return err
}

// Close closes the DB connection, consequently releasing all locks.
func (l *Lock) Close() error {
return l.conn.Close()
}

Expand Down
8 changes: 8 additions & 0 deletions lock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ func TestNewLock(t *testing.T) {
id := int64(10)
lock, err := NewLock(context.Background(), id, db)
assert.Nil(t, err)
defer assert.Nil(t, lock.Close())
assert.Equal(t, id, lock.id)
assert.NotNil(t, lock.conn)
}
Expand All @@ -63,8 +64,10 @@ func TestLockUnlock(t *testing.T) {
id := int64(1)
lock1, err := NewLock(ctx, id, db1)
assert.Nil(t, err)
defer lock1.Close()
lock2, err := NewLock(ctx, id, db2)
assert.Nil(t, err)
defer lock2.Close()

ok, err := lock1.Lock(ctx)
assert.True(t, ok)
Expand All @@ -83,6 +86,9 @@ func TestLockUnlock(t *testing.T) {

err = lock2.Unlock(ctx)
assert.Nil(t, err)

err = lock2.Unlock(ctx)
assert.Nil(t, err)
}

func TestWaitAndLock(t *testing.T) {
Expand All @@ -98,8 +104,10 @@ func TestWaitAndLock(t *testing.T) {
id := int64(1)
lock1, err := NewLock(ctx, id, db1)
assert.Nil(t, err)
defer lock1.Close()
lock2, err := NewLock(ctx, id, db2)
assert.Nil(t, err)
defer lock2.Close()

start := time.Now()
wg.Add(1)
Expand Down