Skip to content

kalafut/keylock

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

keylock

Keylock lets your application control exclusive access to many keys. Unlike a sync.Lock, it does not block, but instead returns true or false depending on whether the lock was acquired. Locks can be cheaply acquired for many keys without creating per-key sync.Mutexes. An additional difference it that a lock will be released automatically after a user-defined timeout, if they've not already been released manually.

At < 60 lines, you may find it easier to just copy the code into your project than to take the dependency. I've found it useful in a few projects, so I'm publishing it here.

Usage

// Locks can be keyed with any comparable type. The timeout can be set to 0 to
// disable automatic unlocking.
l := NewLock[string](time.Second)

l.Lock("foo")      // true
l.Lock("foo")      // false
l.Lock("bar")      // true
l.Unlock("foo")    // Note: this is safe to call even if the lock is not held.

time.Sleep(1100*time.Millisecond)
l.Lock("bar")      // true (the lock was released after 1 second)

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages