Created
September 7, 2020 20:50
-
-
Save seamusv/136a331d41c222a5fd69dec88ff21a16 to your computer and use it in GitHub Desktop.
Test Distributed Lock
This file contains hidden or 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
package main | |
import ( | |
"fmt" | |
goredislib "github.com/go-redis/redis" | |
"github.com/go-redsync/redsync/v3" | |
"github.com/go-redsync/redsync/v3/redis" | |
"github.com/go-redsync/redsync/v3/redis/goredis" | |
"sync" | |
"time" | |
) | |
func main() { | |
wg := sync.WaitGroup{} | |
samples := 50 | |
var failed = 0 | |
var num = 0 | |
sampleChannel := make(chan string) | |
for i := 0; i < samples; i++ { | |
wg.Add(1) | |
go func() { | |
rdb := goredislib.NewClient(&goredislib.Options{ | |
Addr: "localhost:56379", | |
Password: "", | |
DB: 0, | |
}) | |
defer rdb.Close() | |
pool := goredis.NewGoredisPool(rdb) | |
rs := redsync.New([]redis.Pool{pool}) | |
mutex := rs.NewMutex("test-redsync", redsync.SetTries(1)) | |
<-sampleChannel | |
if err := mutex.Lock(); err == nil { | |
time.Sleep(time.Second * 1) | |
num++ | |
mutex.Unlock() | |
} else { | |
failed++ | |
} | |
wg.Done() | |
}() | |
} | |
close(sampleChannel) | |
wg.Wait() | |
fmt.Printf("Failed: %d\nSucceed: %d\nSamples: %d\n\n", failed, num, samples) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment