dlm

package module
v0.0.0-...-9fac196 Latest Latest
Warning

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

Go to latest
Published: Feb 12, 2019 License: Apache-2.0 Imports: 6 Imported by: 0

README

go-dlm

GoDoc CircleCI

This package provides an abstraction library for multiple Distributed Lock Manager backends.

As the moment, go-dlm only supports Redis and an in-memory implementation intended to be used during development.

Usage


// Create a DLM
dlm, err := NewRedisLock("localhost:6379")
if err != nil {
    panic(err)
}

// Create a lock
lock, err := dlm.NewLock("resource", nil)
if err != nil {
    panic(err)
}

// Acquire the lock
if err := lock.Lock(); err != nil {
    panic(err)
}

// Release the lock
if err := lock.Unlock(); err != nil {
    panic(err)
}

Copyright © 2016 MEDIGO GmbH. go-dlm is licensed under the Apache License, Version 2.0. See LICENSE for the full license text.

Documentation

Overview

Package dlm provides an abstraction library for multiple Distributed Lock Manager backends.

Index

Constants

View Source
const (
	// DefaultTTL is the TTL of the lock after wich it will be automatically released.
	DefaultTTL = 15 * time.Second

	// DefaultWaitTime is how long will we wait to acquire a lock.
	DefaultWaitTime = 15 * time.Second

	// DefaultRetryTime is how long will we wait after a failed lock adquisition
	// before attempting to acquire the lock again.
	DefaultRetryTime = 5 * time.Second
)

Variables

View Source
var (
	// ErrLockHeld is returned if we attempt to acquire a lock that is already held.
	ErrLockHeld = fmt.Errorf("lock already held")

	// ErrLockNotHeld is returned if we attempt to release a lock that is not held.
	ErrLockNotHeld = fmt.Errorf("lock not held")

	// ErrCannotLock is returned when it's not possible to acquire the lock before
	// the configured wait time ends.
	ErrCannotLock = fmt.Errorf("timeout while trying to acquire lock")
)

Functions

This section is empty.

Types

type DLM

type DLM interface {
	// NewLock creates a lock for the given key. The returned lock is not held
	// and must be acquired with a call to .Lock.
	NewLock(string, *LockOptions) (Locker, error)
}

DLM describes a Distributed Lock Manager.

func NewInMemDLM

func NewInMemDLM(opts *Options) DLM

NewInMemDLM creates a new InMemDLM.

func NewRedisDLM

func NewRedisDLM(addr string, opts *Options) (DLM, error)

NewRedisDLM creates a new RedisDLM.

type InMemDLM

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

InMemDLM is a local implementation of a DLM that it's intended to be used during development.

func (*InMemDLM) NewLock

func (d *InMemDLM) NewLock(key string, opts *LockOptions) (Locker, error)

NewLock creates a lock for the given key. The returned lock is not held and must be acquired with a call to .Lock.

type LockOptions

type LockOptions struct {
	TTL       time.Duration // Optional, defaults to DefaultTTL
	WaitTime  time.Duration // Optional, defaults to DefaultWaitTime
	RetryTime time.Duration // Optional, defaults to DefaultRetryTime
}

LockOptions parameterizes a lock.

func (*LockOptions) WithDefaults

func (lo *LockOptions) WithDefaults() *LockOptions

WithDefaults returns the options with all default values set.

type Locker

type Locker interface {
	// Key returns the key to be locked.
	Key() string

	// Namespace returns the the prefix to be added to the key. If there is not a namespace,
	// it returns the empty string.
	Namespace() string

	// Lock acquires the lock. It fails with error if the lock is already held.
	Lock() error

	// Unlock releases the lock. It fails with error if the lock is not currently held.
	Unlock() error
}

Locker describes a lock that can be locked or unlocked.

type Options

type Options struct {
	Namespace string // Optional namespace prefixed to each lock key.
}

Options parameterizes a DLM.

type RedisDLM

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

RedisDLM is a DLM that uses Redis as a backend. The implementation is based in the single node algorithm described here: http://redis.io/topics/distlock

func (*RedisDLM) NewLock

func (d *RedisDLM) NewLock(key string, opts *LockOptions) (Locker, error)

NewLock creates a lock for the given key. The returned lock is not held and must be acquired with a call to .Lock.

Jump to

Keyboard shortcuts

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