Skip to content

Golang library that allows locking resources for concurrent writes identified by a string

License

Notifications You must be signed in to change notification settings

montexristos/hashlock

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

hashlock

codecov Go Reference

Description

Package hashlock provides a way to lock multiple string indexes and only allow write access to a resource to one thread at a time

Battle-tested in multi-threaded applications with thousands concurrent writes and reads per minute

Background

Most lock mechanisms in golang use sync.Mutex which works quite well most of the time.

However, if someone tries to acquire an uninitialized Mutex, a non-recoverable error will occur. Checking if the Mutex is initialized may require complex custom logic

That is the reason this library uses boolean channels to handle locking which handle pretty well in terms of stability and performance

IMPORTANT NOTE

This library is not designed to be a queue, so do not expect a FIFO or any other behavior

The goal is to avoid multiple threads writing to the same resource, while keeping it available for concurrent reads

Usage

import "github.com/montexristos/hashlock"

hashLock := (&HashLock{}).New(1 * time.Second)

// lock the value for read/write
hashLock.Lock("awesomeKey")
defer hashLock.Unlock("awesomeKey")

//
go hashLock.Lock("awesomeKey")

// the goroutine initiated above will wait for the current function to release the lock

About

Golang library that allows locking resources for concurrent writes identified by a string

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages