-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
39 lines (32 loc) · 820 Bytes
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package main
import (
"context"
"log"
"os"
"time"
"github.com/flowerinthenight/dlock"
"github.com/gomodule/redigo/redis"
)
func main() {
host := os.Getenv("REDIS_HOST")
pass := os.Getenv("REDIS_PASSWORD")
if host == "" {
return
}
// Test with a single Redis instance with password.
pool := dlock.NewRedisPool(host, dlock.WithPassword(pass))
// Use 1 Redis pool.
l := dlock.NewRedisLock("testredislock", dlock.WithPools([]*redis.Pool{pool}))
// Use the usual lock/unlock.
l.Lock(context.TODO())
log.Println("locked")
time.Sleep(time.Second * 5)
l.Unlock()
log.Println("unlocked")
// Use context expiration.
// ctx, _ := context.WithTimeout(context.Background(), time.Second*10)
// log.Println("lock with context, expire 10s")
// l.Lock(ctx)
// <-ctx.Done()
// log.Println("unlocked")
}