redislock

package module
v2.2.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 6, 2025 License: MIT Imports: 8 Imported by: 5

README

redislock

CI State Go Report Card Go Doc

install

go get github.com/go-co-op/gocron-redis-lock/v2

usage

Here is an example usage that would be deployed in multiple instances

package main

import (
	"fmt"
	"time"

	"github.com/go-co-op/gocron/v2"
	"github.com/redis/go-redis/v9"

	redislock "github.com/go-co-op/gocron-redis-lock/v2"
)

func main() {
	redisOptions := &redis.Options{
		Addr: "localhost:6379",
	}
	redisClient := redis.NewClient(redisOptions)
	locker, err := redislock.NewRedisLocker(redisClient, redislock.WithTries(1))
	if err != nil {
		// handle the error
	}

	s, err := gocron.NewScheduler(gocron.WithDistributedLocker(locker))
	if err != nil {
		// handle the error
	}
	_, err = s.NewJob(gocron.DurationJob(500*time.Millisecond), gocron.NewTask(func() {
		// task to do
	}, 1))
	if err != nil {
		// handle the error
	}
	
	s.Start()
}

The redis UniversalClient can also be used

package main

import (
	"fmt"
	"time"

	"github.com/redis/go-redis/v9"

	redislock "github.com/go-co-op/gocron-redis-lock/v2"
)

func main() {
	redisOptions := &redis.UniversalOptions{
		Addrs: []string{"localhost:6379"},
	}
	redisClient := redis.NewUniversalClient(redisOptions)
	locker, err := redislock.NewRedisLocker(redisClient, redislock.WithTries(1))
	if err != nil {
		// handle the error
	}
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	WithExpiry         = redsync.WithExpiry
	WithDriftFactor    = redsync.WithDriftFactor
	WithGenValueFunc   = redsync.WithGenValueFunc
	WithRetryDelay     = redsync.WithRetryDelay
	WithRetryDelayFunc = redsync.WithRetryDelayFunc
	WithTimeoutFactor  = redsync.WithTimeoutFactor
	WithTries          = redsync.WithTries
	WithValue          = redsync.WithValue

	ErrFailedToConnectToRedis = errors.New("gocron: failed to connect to redis")
	ErrFailedToObtainLock     = errors.New("gocron: failed to obtain lock")
	ErrFailedToReleaseLock    = errors.New("gocron: failed to release lock")
)

alias options

Functions

func NewRedisLocker

func NewRedisLocker(r redis.UniversalClient, options ...redsync.Option) (gocron.Locker, error)

NewRedisLocker provides an implementation of the Locker interface using redis for storage.

func NewRedisLockerAlways

func NewRedisLockerAlways(r redis.UniversalClient, options ...redsync.Option) (gocron.Locker, error)

NewRedisLockerAlways provides an implementation of the Locker interface using redis for storage, even if the connection fails.

func NewRedisLockerWithOptions added in v2.1.0

func NewRedisLockerWithOptions(r redis.UniversalClient, options ...LockerOption) (gocron.Locker, error)

NewRedisLockerWithOptions provides an implementation of the Locker interface using redis for storage, with options to configure the locker.

Types

type LockerOption added in v2.1.0

type LockerOption func(*redisLocker)

LockerOption defines a function type that can be implemented to add options to the Locker

func WithAutoExtendDuration added in v2.1.0

func WithAutoExtendDuration(duration time.Duration) LockerOption

WithAutoExtendDuration sets the duration for auto extending the lock.

func WithKeyPrefix added in v2.2.0

func WithKeyPrefix(prefix string) LockerOption

WithKeyPrefix is a LockerOption for adding a prefix to the lock key

func WithRedsyncOptions added in v2.1.0

func WithRedsyncOptions(options ...redsync.Option) LockerOption

WithRedsyncOptions sets the redsync options.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL