multilocker

package module
v0.0.0-...-e675c4a Latest Latest
Warning

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

Go to latest
Published: Nov 28, 2019 License: MIT Imports: 3 Imported by: 0

README

go-multilocker

This package aim to provide a funtionality to lock multiple resources at once using deadlock avoidance algorythms.

Status

Go Report Card

Usage

See multilocker_test.go.

Reason

For that rare cases where you need to acquire multiple resources at once and don't want to deal with all the scenarios of possible deadlocks and panics. I've just encountered such case in my code and decided to make a package for it. Unfortunatelly, go-multilock didn't work for me.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Lockable

type Lockable interface {
	Lock()
	Unlock()
}

Lockable defines minimal interface for types that are supported

type Locker

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

Locker is a struct used for locking/unlocking It's possible to implement with free functions but the code will become more complex. Feel free to make a PR if you need such functionality

func (*Locker) Lock

func (l *Locker) Lock(locks ...Lockable)

Lock is a function that locks all resources provided. It's guaranteed to avoid deadlock for TryLockable types but might potentially fail into livelock. It has high chance of avoiding deadlock on regular locks as well, but deadlock avoidance is not guaranteed. If a panic is thrown during locking of one of the resouces it'll unlock all the acquired resources.

func (*Locker) TryLock

func (l *Locker) TryLock(locks ...TryLockable) bool

TryLock tries to acquire all provided resources. If a panic is thrown during locking of one of the resouces it'll unlock all the acquired resources.

func (*Locker) Unlock

func (l *Locker) Unlock()

Unlock just releases all acquired resources

type TryLockable

type TryLockable interface {
	Lockable
	TryLock() bool
}

TryLockable extended Lockable interface that allows better functionality

Jump to

Keyboard shortcuts

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