locker

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Sep 18, 2019 License: MIT Imports: 1 Imported by: 10

README

Lightweight Golang Locker

Utility locker - simple and lightweight Golang locker based on sync.Map and sync.Pool

Installation:

To install via go get (needs golang version 1.11+ installed):

go get github.com/enfipy/locker

Usage:

To lock by key:

locker := locker.Initialize()
locker.Lock("key")
<code>
locker.Unlock("key")

To lock only read:

locker := locker.Initialize()
locker.RLock("key")
<code>
locker.RUnlock("key")

Test:

To run tests:

go test

Good explanation of sync package and why is RWMutex with RLock are useful

Reddit post where we discussed why removing the mutex after unlocking is worse than keeping

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Locker

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

Locker type for map of mutexes and self lock to add new mutex to map

func Initialize

func Initialize() *Locker

Initialize - return initialized locker struct

func (*Locker) Lock

func (lkr *Locker) Lock(key interface{}) func()

Lock - locks mutex

func (*Locker) RLock

func (lkr *Locker) RLock(key interface{}) func()

RLock - locks rw for reading

func (*Locker) RUnlock

func (lkr *Locker) RUnlock(key interface{})

RUnlock - unlocks a single RLock call

func (*Locker) Unlock

func (lkr *Locker) Unlock(key interface{})

Unlock - unlocks mutex

Jump to

Keyboard shortcuts

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