netlock

package module
v0.0.0-...-4bfd32b Latest Latest
Warning

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

Go to latest
Published: Sep 9, 2017 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package netlock provides simple mutex-like primitive that uses Redis server to coordinate.

It implements the algorithm described at https://redis.io/commands/set#patterns

Caller tries to set unique key with given TTL (upper estimate of time to hold the mutex), if it succeeds, it can proceed with its work using context to check whether TTL expired and cancel function to both cancel the context and release the lock.

Index

Constants

This section is empty.

Variables

View Source
var ErrLocked = errors.New("lock is already held")

ErrLocked is the error returned by Acquire if key already exists

Functions

func Acquire

Acquire attempts to acquire shared lock with given name on existing connection conn to Redist host. On success, the key name is set to expire after ttl or when context is canceled (if network connection is still alive). Returned context is set to cancel after ttl. CancelFunc should be used to release lock before it expires.

On success conn is bound to CancelFunc so it shouldn't be reused until CancelFunc is called otherwise it would break Redis session state. Normally conn should be created by caller specifically to be used by Acquire and closed once CancelFunc is called.

Types

This section is empty.

Jump to

Keyboard shortcuts

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