Help us shape the future of Go! Take the 2026 Go Developer Survey

tasklock

package
v1.21.2 Latest Latest
Warning

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

Go to latest
Published: Oct 27, 2026 License: GPL-3.0 Imports: 6 Imported by: 0

Documentation

Overview

Package tasklock keeps two runs of one periodic task from overlapping, across replicas, with a Redis lock that only its owner may release or extend.

Index

Constants

This section is empty.

Variables

View Source
var ErrLost = errors.New("task lock is no longer held by this run")

ErrLost reports a heartbeat that found the lock no longer held by this run: it expired, and possibly another run took it over.

Functions

This section is empty.

Types

type Lock

type Lock struct {
	// contains filtered or unexported fields
}

Lock is a held task lock.

func Acquire

func Acquire(ctx context.Context, client *redis.Client, key string, ttl time.Duration) (lock *Lock, ok bool, err error)

Acquire takes the lock at key for ttl. ok is false, with a nil error, when another run holds it.

func (*Lock) Extend

func (l *Lock) Extend(ctx context.Context, ttl time.Duration) (bool, error)

Extend gives the lock ttl more from now if this run still owns it and reports whether it did; a lock that expired, or that another run took over, is left alone.

func (*Lock) KeepAlive

func (l *Lock) KeepAlive(ctx context.Context, ttl time.Duration, report func(error)) (stop func())

KeepAlive extends the lock by ttl every third of ttl until stop is called or ctx ends, so a run that outlives its TTL keeps the lock a replica would otherwise take over. report, if not nil, is told of a failed extension: ErrLost when the lock is gone, after which the heartbeat stops, or the Redis error, after which it keeps trying. Call stop before Release; it waits for the heartbeat to end.

func (*Lock) Release

func (l *Lock) Release(ctx context.Context) (bool, error)

Release frees the lock if this run still owns it and reports whether it did; an expired lock taken over by another run is left alone.

Jump to

Keyboard shortcuts

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