github.com/Cloud-Foundations/Dominator@v0.3.4/lib/lockwatcher/api.go (about) 1 package lockwatcher 2 3 import ( 4 "io" 5 "sync" 6 "time" 7 8 "github.com/Cloud-Foundations/Dominator/lib/log" 9 ) 10 11 type RWLock interface { 12 sync.Locker 13 RLock() 14 RUnlock() 15 TryLock() bool 16 } 17 18 type LockWatcher struct { 19 blockReadLock sync.Mutex 20 lock sync.Locker 21 LockWatcherOptions 22 rstopChannel chan<- struct{} 23 stopChannel chan<- struct{} 24 statsMutex sync.RWMutex 25 stats LockWatcherStats 26 } 27 28 type LockWatcherOptions struct { 29 CheckInterval time.Duration // Default: 5 seconds, minimum: 1 second. 30 Function func() 31 Logger log.DebugLogger 32 RFunction func() 33 LogTimeout time.Duration // Default: 1 second, min: 1 millisecond. 34 MaximumTryInterval time.Duration // Default/maximum: LogTimeout/32. 35 MinimumTryInterval time.Duration // Default/maximum: LogTimeout/256. 36 } 37 38 type LockWatcherStats struct { 39 NumLockTimeouts uint64 // Populated for sync.Mutex 40 NumRLockTimeouts uint64 // Populated for sync.RWMutex 41 NumWLockTimeouts uint64 // Populated for sync.RWMutex 42 WaitingForLock bool // Populated for sync.Mutex 43 WaitingForRLock bool // Populated for sync.RWMutex 44 WaitingForWLock bool // Populated for sync.RWMutex 45 } 46 47 func New(lock sync.Locker, options LockWatcherOptions) *LockWatcher { 48 return newLockWatcher(lock, options) 49 } 50 51 func (lw *LockWatcher) GetOptions() LockWatcherOptions { 52 return lw.LockWatcherOptions 53 } 54 55 func (lw *LockWatcher) GetStats() LockWatcherStats { 56 return lw.getStats() 57 } 58 59 func (lw *LockWatcher) Stop() { 60 lw.stop() 61 } 62 63 // WriteHtml will write HTML-formatted statistics information to writer, with an 64 // optional prefix. It returns true if something was written, else false. 65 func (lw *LockWatcher) WriteHtml(writer io.Writer, 66 prefix string) (bool, error) { 67 return lw.writeHtml(writer, prefix) 68 }