Skip to content

Commit

Permalink
feat: allow sharing the underlying Client of a rueidislock.Locker
Browse files Browse the repository at this point in the history
  • Loading branch information
rueian committed Nov 25, 2023
1 parent cfe1cc0 commit 65f7055
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
6 changes: 6 additions & 0 deletions rueidislock/lock.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ type Locker interface {
WithContext(ctx context.Context, name string) (context.Context, context.CancelFunc, error)
// TryWithContext tries to acquire a distributed redis lock by name without waiting. It may return ErrNotLocked.
TryWithContext(ctx context.Context, name string) (context.Context, context.CancelFunc, error)
// Client exports the underlying rueidis.Client
Client() rueidis.Client
// Close closes the underlying rueidis.Client
Close()
}
Expand Down Expand Up @@ -390,6 +392,10 @@ func (m *locker) WithContext(ctx context.Context, name string) (context.Context,
}
}

func (m *locker) Client() rueidis.Client {
return m.client
}

func (m *locker) Close() {
m.mu.Lock()
for _, g := range m.gates {
Expand Down
9 changes: 7 additions & 2 deletions rueidislock/lock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,21 @@ func TestNewLocker(t *testing.T) {
}

func TestNewLocker_WithClientBuilder(t *testing.T) {
var client rueidis.Client
l, err := NewLocker(LockerOption{
ClientOption: rueidis.ClientOption{InitAddress: address},
ClientBuilder: func(option rueidis.ClientOption) (rueidis.Client, error) {
return rueidis.NewClient(option)
ClientBuilder: func(option rueidis.ClientOption) (_ rueidis.Client, err error) {
client, err = rueidis.NewClient(option)
return client, err
},
})
if err != nil {
t.Fatal(err)
}
defer l.Close()
if l.Client() != client {
t.Fatal("client mismatched")
}
}

func TestLocker_WithContext_MultipleLocker(t *testing.T) {
Expand Down

0 comments on commit 65f7055

Please sign in to comment.