Skip to content

Commit a2df1d2

Browse files
authored
Merge pull request #2 from kiran94/refactor/apply-interface
refactor(redlock): apply interface
2 parents f2a0902 + 2047380 commit a2df1d2

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

redlock/redlock.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,15 @@ type RedLock struct {
5353
client []*RedClient
5454
}
5555

56+
type RedLocker interface {
57+
SetLock(ctx context.Context, key string, value string, ttl int) (float64, error)
58+
UnSetLock(ctx context.Context, key string, value string) (float64, error)
59+
GetLockTtl(ctx context.Context, key string, value string) (int64, error)
60+
IsLocked(ctx context.Context, key string) bool
61+
}
62+
5663
// NewRedisLock returns a RedisLock.
57-
func NewRedisLock(ctx context.Context, options ...*red.Options) *RedLock {
64+
func NewRedisLock(ctx context.Context, options ...*red.Options) RedLocker {
5865
var clients []*RedClient
5966
for _, opt := range options {
6067
client := red.NewClient(opt)

redlock/redlock_test.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ package redlock
33
import (
44
"context"
55
"fmt"
6-
red "github.com/go-redis/redis/v8"
76
"testing"
7+
8+
red "github.com/go-redis/redis/v8"
89
)
910

1011
func CommonClient() []*RedClient {
@@ -48,7 +49,11 @@ func TestNewRedisLock(t *testing.T) {
4849
for _, tt := range tests {
4950
t.Run(tt.name, func(t *testing.T) {
5051
got := NewRedisLock(tt.args.ctx, tt.args.options...)
51-
for _, client := range got.client {
52+
gotOut, ok := got.(*RedLock)
53+
if !ok {
54+
t.Fatalf("new redis lock was not a redlock instance")
55+
}
56+
for _, client := range gotOut.client {
5257
err := client.cli.Ping(ctx).Err()
5358
if err != nil {
5459
t.Fatalf("redis connect error")

0 commit comments

Comments
 (0)