redislock

package module
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Dec 18, 2023 License: MIT Imports: 7 Imported by: 1

README

redislock

CI State Go Report Card Go Doc

install

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

usage

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

package main

import (
	"fmt"
	"time"

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

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

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 := gocron.NewScheduler(time.UTC)
	s.WithDistributedLocker(locker)

	_, err = s.Every("1s").Name("unique_name").Do(func() {
		// task to do
		fmt.Println("call 1s")
	})
	if err != nil {
		// handle the error
	}

	s.StartBlocking()
}

The redis UniversalClient can also be used

package main

import (
	"fmt"
	"time"

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

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

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
	}

	s := gocron.NewScheduler(time.UTC)
	s.WithDistributedLocker(locker)

	_, err = s.Every("1s").Name("unique_name").Do(func() {
		// task to do
		fmt.Println("call 1s")
	})
	if err != nil {
		// handle the error
	}

	s.StartBlocking()
}

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
)

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 added in v1.2.0

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 v1.3.0

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

Types

type LockerOption added in v1.3.0

type LockerOption func(*redisLocker)

func WithAutoExtendDuration added in v1.3.0

func WithAutoExtendDuration(duration time.Duration) LockerOption

func WithRedsyncOptions added in v1.3.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