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

Function ExampleClient_Obtain_customDeadline

example_test.go:134–157  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

132}
133
134func ExampleClient_Obtain_customDeadline() {
135 client := redis.NewClient(&redis.Options{Network: "tcp", Addr: "127.0.0.1:6379"})
136 defer client.Close()
137
138 locker := redislock.New(client)
139
140 // Retry every 500ms, for up-to a minute
141 backoff := redislock.LinearBackoff(500 * time.Millisecond)
142 ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(time.Minute))
143 defer cancel()
144
145 // Obtain lock with retry + custom deadline
146 lock, err := locker.Obtain(ctx, "my-key", time.Second, &redislock.Options{
147 RetryStrategy: backoff,
148 })
149 if err == redislock.ErrNotObtained {
150 fmt.Println("Could not obtain lock!")
151 } else if err != nil {
152 log.Fatalln(err)
153 }
154 defer lock.Release(context.Background())
155
156 fmt.Println("I have a lock!")
157}

Callers

nothing calls this directly

Calls 4

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

Tested by

no test coverage detected