MCPcopy
hub / github.com/bsm/redislock / ExampleClient_Obtain_retry

Function ExampleClient_Obtain_retry

example_test.go:67–90  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

65}
66
67func ExampleClient_Obtain_retry() {
68 client := redis.NewClient(&redis.Options{Network: "tcp", Addr: "127.0.0.1:6379"})
69 defer client.Close()
70
71 locker := redislock.New(client)
72
73 ctx := context.Background()
74
75 // Retry every 100ms, for up-to 3x
76 backoff := redislock.LimitRetry(redislock.LinearBackoff(100*time.Millisecond), 3)
77
78 // Obtain lock with retry
79 lock, err := locker.Obtain(ctx, "my-key", time.Second, &redislock.Options{
80 RetryStrategy: backoff,
81 })
82 if err == redislock.ErrNotObtained {
83 fmt.Println("Could not obtain lock!")
84 } else if err != nil {
85 log.Fatalln(err)
86 }
87 defer lock.Release(ctx)
88
89 fmt.Println("I have a lock!")
90}
91
92func ExampleLock_Refresh_watchdog() {
93 client := redis.NewClient(&redis.Options{Network: "tcp", Addr: "127.0.0.1:6379"})

Callers

nothing calls this directly

Calls 5

NewFunction · 0.92
LimitRetryFunction · 0.92
LinearBackoffFunction · 0.92
ObtainMethod · 0.80
ReleaseMethod · 0.80

Tested by

no test coverage detected