diff --git a/redlock/redlock.go b/redlock/redlock.go index a702e09..ef01a43 100644 --- a/redlock/redlock.go +++ b/redlock/redlock.go @@ -53,8 +53,15 @@ type RedLock struct { client []*RedClient } +type RedLocker interface { + SetLock(ctx context.Context, key string, value string, ttl int) (float64, error) + UnSetLock(ctx context.Context, key string, value string) (float64, error) + GetLockTtl(ctx context.Context, key string, value string) (int64, error) + IsLocked(ctx context.Context, key string) bool +} + // NewRedisLock returns a RedisLock. -func NewRedisLock(ctx context.Context, options ...*red.Options) *RedLock { +func NewRedisLock(ctx context.Context, options ...*red.Options) RedLocker { var clients []*RedClient for _, opt := range options { client := red.NewClient(opt) diff --git a/redlock/redlock_test.go b/redlock/redlock_test.go index a3cfdb1..b9e1de0 100644 --- a/redlock/redlock_test.go +++ b/redlock/redlock_test.go @@ -3,8 +3,9 @@ package redlock import ( "context" "fmt" - red "github.com/go-redis/redis/v8" "testing" + + red "github.com/go-redis/redis/v8" ) func CommonClient() []*RedClient { @@ -48,7 +49,11 @@ func TestNewRedisLock(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { got := NewRedisLock(tt.args.ctx, tt.args.options...) - for _, client := range got.client { + gotOut, ok := got.(*RedLock) + if !ok { + t.Fatalf("new redis lock was not a redlock instance") + } + for _, client := range gotOut.client { err := client.cli.Ping(ctx).Err() if err != nil { t.Fatalf("redis connect error")