-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #187 from guoming0000/main
add redlock
- Loading branch information
Showing
4 changed files
with
80 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package redlock | ||
|
||
import ( | ||
"github.com/go-redsync/redsync/v4" | ||
"github.com/go-redsync/redsync/v4/redis/goredis/v9" | ||
"github.com/redis/go-redis/v9" | ||
) | ||
|
||
type Redlock struct { | ||
redsync *redsync.Redsync | ||
} | ||
|
||
// New 简单封装,仅支持单个redlock | ||
func New(c *redis.Client) *Redlock { | ||
pool := goredis.NewPool(c) | ||
rs := redsync.New(pool) | ||
return &Redlock{ | ||
redsync: rs, | ||
} | ||
} | ||
|
||
func (r *Redlock) NewMutex(name string, options ...redsync.Option) *redsync.Mutex { | ||
return r.redsync.NewMutex(name, options...) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package redlock | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"testing" | ||
"time" | ||
|
||
"github.com/go-redsync/redsync/v4" | ||
"github.com/redis/go-redis/v9" | ||
) | ||
|
||
const ( | ||
EsLockKey = "redlock:es:%s" // redlock:es:{} | ||
EsLockExpireTime = time.Second * 60 // 统一默认过期时间 | ||
) | ||
|
||
func TestRedlock(t *testing.T) { | ||
var c *redis.Client | ||
lock := New(c) | ||
key := fmt.Sprintf(EsLockKey, "id") | ||
esLock := lock.NewMutex(key, redsync.WithExpiry(EsLockExpireTime)) | ||
|
||
// lock with Lock | ||
err := esLock.Lock() | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
// do sth | ||
_, _ = esLock.Unlock() | ||
|
||
// lock with LockContext | ||
ctx := context.Background() | ||
if err = esLock.LockContext(ctx); err != nil { | ||
t.Error(err) | ||
} | ||
if _, err = esLock.UnlockContext(ctx); err != nil { | ||
t.Error(err) | ||
} | ||
} |