Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var ( ErrAlreadyLocked = errors.New("already locked") ErrUnlocked = errors.New("explicitly unlocked") )
Functions ¶
This section is empty.
Types ¶
type Locker ¶
type Locker struct {
// contains filtered or unexported fields
}
func (*Locker) Lock ¶
func (m *Locker) Lock(ctx context.Context, key string, options ...Option) (*Lock, context.Context, error)
Example ¶
package main
import (
"context"
"time"
"github.com/khasanovbi/redislock"
"github.com/redis/go-redis/v9"
)
func doJobUnderLockWithContext(ctx context.Context) { //nolint:revive
}
func main() {
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
})
locker, err := redislock.New(rdb, redislock.WithRefreshPeriod(time.Minute), redislock.WithTTL(10*time.Minute))
if err != nil {
panic(err)
}
lock, ctx, err := locker.Lock(context.Background(), "my_lock_key")
if err != nil {
panic(err)
}
defer func() {
_ = lock.Unlock()
}()
doJobUnderLockWithContext(ctx)
}
Output:
Click to show internal directories.
Click to hide internal directories.