Documentation ¶
Overview ¶
Package lockmap provides a thread-safe map of auto-expiring locks with promises to wait on. The underlying map will be periodically sweeped to remove expired promises. All promises returned by the map are guaranteed to be resolved roughly within its expiry. If a promise expires, is resolved manually, or is replaced, the channel will be closed.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type LockMap ¶
type LockMap interface { // Wait returns the Promise for a key. // If the Promise is expired, it will be resolved, but this function will return nil Wait(key interface{}) Promise // WaitOrLock takes the lock, but only if none exists for that key. // If the Promise is expired, it will successfully replace it and the previous Promise will be resolved. WaitOrLock(key interface{}, ttl time.Duration) (promise Promise, gotLock bool) // Release unlocks a key and resolved the previous Promise, if any. Release(key interface{}) // Allows the LockMap to be started and stopped externally Tomb() *tomb.Tomb Run() error }
func New ¶
Example ¶
m := New(1*time.Minute, &tomb.Tomb{}) m.Tomb().Go(m.Run) output := "" done := make(chan struct{}) waitOrWork := func() { promise, gotLock := m.WaitOrLock("foo", 10*time.Second) if gotLock { // We just grabbed a new lock, do work with it. <-time.After(100 * time.Millisecond) output = "done" // Once we're done, remove it, which will close the channel on the promise. m.Release("foo") } else { <-promise fmt.Println(output) close(done) } } // Either one could be trigger first go waitOrWork() go waitOrWork() <-done
Output: done
type PromiseMap ¶
type PromiseMap map[interface{}]Promise
Click to show internal directories.
Click to hide internal directories.