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 ¶
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 ¶
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 ¶
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.