Documentation
¶
Overview ¶
Package powerlock provides named, context-aware, bounded, cancellable, observable, and watchdog mutexes.
The read/write lock types use FIFO waiter ordering. Context methods return typed errors whose causes remain available through errors.Is and errors.As. Blocking Lock methods retain sync.Locker behavior and panic when a specialized lock rejects an acquisition. Try methods never join or bypass an existing waiter queue.
Observed locks emit structured transition events. Watchdog locks add wait and exact-hold threshold events, while LockGuard permits exact pairing of individual reader acquisitions. DefaultProfileObserver exposes current waiters and exactly paired holders through runtime pprof profiles. RuntimeTraceObserver annotates watchdog thresholds in Go execution traces and can notify an application-owned flight recorder.
Use ContextRWMutex for cancellable FIFO waiting, CancelRWMutex for irreversible shutdown, MaxRWMutex to bound blocked acquisitions, ObservedRWMutex for complete event and metric reporting, WatchdogRWMutex for slow wait and hold diagnostics, and KeyedMutex to serialize work independently by key. Exclusive forms omit the read methods. LockEvent.String formats an event as a single actionable diagnostic line. Prefer sync.Mutex or sync.RWMutex when these controls and diagnostics are not needed.
Index ¶
- Constants
- Variables
- type AcquisitionError
- type CancelMutex
- func (m *CancelMutex) Cancel()
- func (m *CancelMutex) Cancelled() bool
- func (m *CancelMutex) Lock()
- func (m *CancelMutex) LockContext(ctx context.Context) error
- func (m *CancelMutex) Name() string
- func (m *CancelMutex) SetLocation(name string)
- func (m *CancelMutex) Snapshot() LockState
- func (m *CancelMutex) TryLock() bool
- func (m *CancelMutex) Unlock()
- type CancelRWMutex
- func (m *CancelRWMutex) Cancel()
- func (m *CancelRWMutex) Cancelled() bool
- func (m *CancelRWMutex) Lock()
- func (m *CancelRWMutex) LockContext(ctx context.Context) error
- func (m *CancelRWMutex) Name() string
- func (m *CancelRWMutex) RLock()
- func (m *CancelRWMutex) RLockContext(ctx context.Context) error
- func (m *CancelRWMutex) RLocker() sync.Locker
- func (m *CancelRWMutex) RUnlock()
- func (m *CancelRWMutex) SetLocation(location string)
- func (m *CancelRWMutex) Snapshot() LockState
- func (m *CancelRWMutex) TryLock() bool
- func (m *CancelRWMutex) TryRLock() bool
- func (m *CancelRWMutex) Unlock()
- type ContextMutex
- type ContextRWMutex
- func (m *ContextRWMutex) Lock()
- func (m *ContextRWMutex) LockContext(ctx context.Context) error
- func (m *ContextRWMutex) Name() string
- func (m *ContextRWMutex) RLock()
- func (m *ContextRWMutex) RLockContext(ctx context.Context) error
- func (m *ContextRWMutex) RLocker() sync.Locker
- func (m *ContextRWMutex) RUnlock()
- func (m *ContextRWMutex) SetLocation(location string)
- func (m *ContextRWMutex) Snapshot() LockState
- func (m *ContextRWMutex) TryLock() bool
- func (m *ContextRWMutex) TryRLock() bool
- func (m *ContextRWMutex) Unlock()
- type FairMutex
- type FairRWMutex
- type FlightRecorderCallback
- type KeyGuard
- type KeyedAcquisitionError
- type KeyedMutex
- func (m *KeyedMutex[K]) ActiveKeys() int
- func (m *KeyedMutex[K]) Lock(key K)
- func (m *KeyedMutex[K]) LockContext(ctx context.Context, key K) error
- func (m *KeyedMutex[K]) LockGuard(ctx context.Context, key K) (*KeyGuard[K], error)
- func (m *KeyedMutex[K]) MaxKeys() int
- func (m *KeyedMutex[K]) Name() string
- func (m *KeyedMutex[K]) SetLocation(name string)
- func (m *KeyedMutex[K]) Snapshot(key K) (LockState, bool)
- func (m *KeyedMutex[K]) TryLock(key K) bool
- func (m *KeyedMutex[K]) Unlock(key K)
- type LockAttemptID
- type LockEvent
- type LockEventKind
- type LockGuard
- type LockMode
- type LockObserver
- type LockObserverFunc
- type LockObserverGroup
- type LockResult
- type LockState
- type MaxMutex
- type MaxRWMutex
- func (m *MaxRWMutex) Lock()
- func (m *MaxRWMutex) LockContext(ctx context.Context) error
- func (m *MaxRWMutex) MaxWaiting() int
- func (m *MaxRWMutex) Name() string
- func (m *MaxRWMutex) RLock()
- func (m *MaxRWMutex) RLockContext(ctx context.Context) error
- func (m *MaxRWMutex) RLocker() sync.Locker
- func (m *MaxRWMutex) RUnlock()
- func (m *MaxRWMutex) SetLocation(location string)
- func (m *MaxRWMutex) Snapshot() LockState
- func (m *MaxRWMutex) TryLock() bool
- func (m *MaxRWMutex) TryRLock() bool
- func (m *MaxRWMutex) Unlock()
- type ObservedMutex
- func (m *ObservedMutex) Lock()
- func (m *ObservedMutex) LockContext(ctx context.Context) error
- func (m *ObservedMutex) LockGuard(ctx context.Context) (*LockGuard, error)
- func (m *ObservedMutex) Name() string
- func (m *ObservedMutex) SetLocation(name string)
- func (m *ObservedMutex) Snapshot() LockState
- func (m *ObservedMutex) TryLock() bool
- func (m *ObservedMutex) TryLockGuard() (*LockGuard, bool)
- func (m *ObservedMutex) Unlock()
- type ObservedRWMutex
- func (m *ObservedRWMutex) Lock()
- func (m *ObservedRWMutex) LockContext(ctx context.Context) error
- func (m *ObservedRWMutex) LockGuard(ctx context.Context) (*LockGuard, error)
- func (m *ObservedRWMutex) Name() string
- func (m *ObservedRWMutex) RLock()
- func (m *ObservedRWMutex) RLockContext(ctx context.Context) error
- func (m *ObservedRWMutex) RLockGuard(ctx context.Context) (*LockGuard, error)
- func (m *ObservedRWMutex) RLocker() sync.Locker
- func (m *ObservedRWMutex) RUnlock()
- func (m *ObservedRWMutex) SetLocation(location string)
- func (m *ObservedRWMutex) Snapshot() LockState
- func (m *ObservedRWMutex) TryLock() bool
- func (m *ObservedRWMutex) TryLockGuard() (*LockGuard, bool)
- func (m *ObservedRWMutex) TryRLock() bool
- func (m *ObservedRWMutex) TryRLockGuard() (*LockGuard, bool)
- func (m *ObservedRWMutex) Unlock()
- type ProfileObserver
- type RuntimeTraceObserver
- type WatchdogMutex
- func (m *WatchdogMutex) HoldThreshold() time.Duration
- func (m *WatchdogMutex) Lock()
- func (m *WatchdogMutex) LockContext(ctx context.Context) error
- func (m *WatchdogMutex) LockGuard(ctx context.Context) (*LockGuard, error)
- func (m *WatchdogMutex) Name() string
- func (m *WatchdogMutex) SetLocation(name string)
- func (m *WatchdogMutex) Snapshot() LockState
- func (m *WatchdogMutex) TryLock() bool
- func (m *WatchdogMutex) TryLockGuard() (*LockGuard, bool)
- func (m *WatchdogMutex) Unlock()
- func (m *WatchdogMutex) WaitThreshold() time.Duration
- type WatchdogRWMutex
- func (m *WatchdogRWMutex) HoldThreshold() time.Duration
- func (m *WatchdogRWMutex) Lock()
- func (m *WatchdogRWMutex) LockContext(ctx context.Context) error
- func (m *WatchdogRWMutex) LockGuard(ctx context.Context) (*LockGuard, error)
- func (m *WatchdogRWMutex) Name() string
- func (m *WatchdogRWMutex) RLock()
- func (m *WatchdogRWMutex) RLockContext(ctx context.Context) error
- func (m *WatchdogRWMutex) RLockGuard(ctx context.Context) (*LockGuard, error)
- func (m *WatchdogRWMutex) RLocker() sync.Locker
- func (m *WatchdogRWMutex) RUnlock()
- func (m *WatchdogRWMutex) SetLocation(name string)
- func (m *WatchdogRWMutex) Snapshot() LockState
- func (m *WatchdogRWMutex) TryLock() bool
- func (m *WatchdogRWMutex) TryLockGuard() (*LockGuard, bool)
- func (m *WatchdogRWMutex) TryRLock() bool
- func (m *WatchdogRWMutex) TryRLockGuard() (*LockGuard, bool)
- func (m *WatchdogRWMutex) Unlock()
- func (m *WatchdogRWMutex) WaitThreshold() time.Duration
Examples ¶
Constants ¶
const ( // DefaultWaitThreshold reports acquisitions that wait for at least one second. DefaultWaitThreshold = time.Second // DefaultHoldThreshold reports exact acquisitions that remain held for at least five seconds. DefaultHoldThreshold = 5 * time.Second )
const DefaultMaxKeys = 1024
DefaultMaxKeys is the maximum number of active keyed lock entries used by the zero value and NewKeyedMutex.
const DefaultMaxWaiting = 1
DefaultMaxWaiting is the waiter limit used by NewMaxRWMutex and the zero value.
Variables ¶
var ( // ErrBusy identifies an acquisition that could not proceed immediately. ErrBusy = errors.New("powerlock: lock is busy") // ErrCancelled identifies an acquisition rejected by context or permanent lock cancellation. ErrCancelled = errors.New("powerlock: lock was cancelled") // ErrMaxKeys identifies an acquisition rejected by a KeyedMutex active-key limit. ErrMaxKeys = errors.New("powerlock: maximum keyed locks reached") // ErrMaxWaiting identifies an acquisition rejected by a MaxRWMutex waiter limit. ErrMaxWaiting = errors.New("powerlock: maximum waiting acquisitions reached") // ErrInvalidConfig identifies an invalid constructor value or forbidden post-use reconfiguration. ErrInvalidConfig = errors.New("powerlock: invalid configuration") )
Functions ¶
This section is empty.
Types ¶
type AcquisitionError ¶
type AcquisitionError struct {
Name string
Mode LockMode
Result LockResult
MaxWaiting int
State LockState
Cause error
}
AcquisitionError reports why a lock acquisition did not complete.
func (*AcquisitionError) Error ¶
func (e *AcquisitionError) Error() string
Error returns the complete acquisition failure.
func (*AcquisitionError) Unwrap ¶
func (e *AcquisitionError) Unwrap() error
Unwrap returns the underlying acquisition failure.
type CancelMutex ¶
type CancelMutex struct {
// contains filtered or unexported fields
}
CancelMutex is the exclusive-only form of CancelRWMutex.
func NewCancelMutex ¶
func NewCancelMutex(name string) *CancelMutex
NewCancelMutex returns a cancellable exclusive lock.
func (*CancelMutex) Cancel ¶
func (m *CancelMutex) Cancel()
Cancel permanently rejects queued and future acquisitions.
func (*CancelMutex) Cancelled ¶
func (m *CancelMutex) Cancelled() bool
Cancelled reports whether Cancel has been called.
func (*CancelMutex) Lock ¶
func (m *CancelMutex) Lock()
Lock acquires the mutex and panics after cancellation.
func (*CancelMutex) LockContext ¶
func (m *CancelMutex) LockContext(ctx context.Context) error
LockContext acquires the mutex or returns a cancellation or deadline error.
func (*CancelMutex) Name ¶
func (m *CancelMutex) Name() string
Name returns the immutable diagnostic name.
func (*CancelMutex) SetLocation ¶
func (m *CancelMutex) SetLocation(name string)
SetLocation changes the diagnostic name before the first operation and panics after first use.
func (*CancelMutex) Snapshot ¶
func (m *CancelMutex) Snapshot() LockState
Snapshot returns the current lock state.
func (*CancelMutex) TryLock ¶
func (m *CancelMutex) TryLock() bool
TryLock attempts to acquire the mutex without waiting.
type CancelRWMutex ¶
type CancelRWMutex struct {
// contains filtered or unexported fields
}
CancelRWMutex is a FIFO read/write lock that permanently rejects acquisitions after cancellation.
func NewCancelRWMutex ¶
func NewCancelRWMutex(location string) *CancelRWMutex
NewCancelRWMutex returns a cancellable lock with the supplied diagnostic name.
func (*CancelRWMutex) Cancel ¶
func (m *CancelRWMutex) Cancel()
Cancel permanently rejects queued and future acquisitions while allowing current holders to unlock.
func (*CancelRWMutex) Cancelled ¶
func (m *CancelRWMutex) Cancelled() bool
Cancelled reports whether Cancel has been called.
func (*CancelRWMutex) Lock ¶
func (m *CancelRWMutex) Lock()
Lock acquires the write lock and panics if the lock has been cancelled.
func (*CancelRWMutex) LockContext ¶
func (m *CancelRWMutex) LockContext(ctx context.Context) error
LockContext acquires the write lock or returns a cancellation or deadline error.
func (*CancelRWMutex) Name ¶
func (m *CancelRWMutex) Name() string
Name returns the immutable diagnostic name.
func (*CancelRWMutex) RLock ¶
func (m *CancelRWMutex) RLock()
RLock acquires the read lock and panics if the lock has been cancelled.
func (*CancelRWMutex) RLockContext ¶
func (m *CancelRWMutex) RLockContext(ctx context.Context) error
RLockContext acquires the read lock or returns a cancellation or deadline error.
func (*CancelRWMutex) RLocker ¶
func (m *CancelRWMutex) RLocker() sync.Locker
RLocker returns a sync.Locker backed by RLock and RUnlock.
func (*CancelRWMutex) SetLocation ¶
func (m *CancelRWMutex) SetLocation(location string)
SetLocation changes the diagnostic name before the first operation and panics after first use.
func (*CancelRWMutex) Snapshot ¶
func (m *CancelRWMutex) Snapshot() LockState
Snapshot returns the current lock state.
func (*CancelRWMutex) TryLock ¶
func (m *CancelRWMutex) TryLock() bool
TryLock attempts to acquire the write lock without waiting.
func (*CancelRWMutex) TryRLock ¶
func (m *CancelRWMutex) TryRLock() bool
TryRLock attempts to acquire a read lock without waiting or bypassing queued acquisitions.
type ContextMutex ¶
type ContextMutex struct {
// contains filtered or unexported fields
}
ContextMutex is the exclusive-only form of ContextRWMutex.
func NewContextMutex ¶
func NewContextMutex(name string) *ContextMutex
NewContextMutex returns a FIFO context-aware exclusive lock.
func (*ContextMutex) LockContext ¶
func (m *ContextMutex) LockContext(ctx context.Context) error
LockContext acquires the mutex or returns a cancellation or deadline error.
func (*ContextMutex) Name ¶
func (m *ContextMutex) Name() string
Name returns the immutable diagnostic name.
func (*ContextMutex) SetLocation ¶
func (m *ContextMutex) SetLocation(name string)
SetLocation changes the diagnostic name before the first operation and panics after first use.
func (*ContextMutex) Snapshot ¶
func (m *ContextMutex) Snapshot() LockState
Snapshot returns the current lock state.
func (*ContextMutex) TryLock ¶
func (m *ContextMutex) TryLock() bool
TryLock attempts to acquire the mutex without waiting.
type ContextRWMutex ¶
type ContextRWMutex struct {
// contains filtered or unexported fields
}
ContextRWMutex is a FIFO read/write lock whose queued acquisitions can be cancelled.
Example ¶
package main
import (
"fmt"
"github.com/donomii/powerlock"
)
func main() {
lock := powerlock.NewContextRWMutex("cache")
lock.Lock()
fmt.Printf("name=%s writer=%t\n", lock.Name(), lock.Snapshot().Writer)
lock.Unlock()
}
Output: name=cache writer=true
func NewContextRWMutex ¶
func NewContextRWMutex(location string) *ContextRWMutex
NewContextRWMutex returns a context-aware lock with the supplied diagnostic name.
func (*ContextRWMutex) LockContext ¶
func (m *ContextRWMutex) LockContext(ctx context.Context) error
LockContext acquires the write lock or returns a cancellation or deadline error.
func (*ContextRWMutex) Name ¶
func (m *ContextRWMutex) Name() string
Name returns the immutable diagnostic name.
func (*ContextRWMutex) RLockContext ¶
func (m *ContextRWMutex) RLockContext(ctx context.Context) error
RLockContext acquires a read lock or returns a cancellation or deadline error.
func (*ContextRWMutex) RLocker ¶
func (m *ContextRWMutex) RLocker() sync.Locker
RLocker returns a sync.Locker backed by RLock and RUnlock.
func (*ContextRWMutex) SetLocation ¶
func (m *ContextRWMutex) SetLocation(location string)
SetLocation changes the diagnostic name before the first operation and panics after first use.
func (*ContextRWMutex) Snapshot ¶
func (m *ContextRWMutex) Snapshot() LockState
Snapshot returns the current lock state.
func (*ContextRWMutex) TryLock ¶
func (m *ContextRWMutex) TryLock() bool
TryLock attempts to acquire the write lock without waiting.
func (*ContextRWMutex) TryRLock ¶
func (m *ContextRWMutex) TryRLock() bool
TryRLock attempts to acquire a read lock without waiting or bypassing queued acquisitions.
type FairMutex ¶
type FairMutex = ContextMutex
FairMutex is the explicitly named FIFO form of ContextMutex.
func NewFairMutex ¶
NewFairMutex returns a FIFO context-aware exclusive lock.
type FairRWMutex ¶
type FairRWMutex = ContextRWMutex
FairRWMutex is the explicitly named FIFO form of ContextRWMutex.
func NewFairRWMutex ¶
func NewFairRWMutex(name string) *FairRWMutex
NewFairRWMutex returns a FIFO context-aware read/write lock.
type FlightRecorderCallback ¶
type FlightRecorderCallback func(LockEvent)
FlightRecorderCallback receives watchdog threshold events after their runtime trace annotation is written. Applications using Go 1.25 or later can use it to enqueue a snapshot of an active runtime trace flight recorder.
type KeyGuard ¶
type KeyGuard[K comparable] struct { // contains filtered or unexported fields }
KeyGuard owns one keyed acquisition and releases it once.
func (*KeyGuard[K]) AttemptID ¶
func (g *KeyGuard[K]) AttemptID() LockAttemptID
AttemptID returns the diagnostic identifier for the keyed acquisition.
type KeyedAcquisitionError ¶
type KeyedAcquisitionError[K comparable] struct { Name string Key K MaxKeys int ActiveKeys int Cause error }
KeyedAcquisitionError reports a keyed-lock failure with its capacity state.
func (*KeyedAcquisitionError[K]) Error ¶
func (e *KeyedAcquisitionError[K]) Error() string
Error returns the complete keyed acquisition failure.
func (*KeyedAcquisitionError[K]) Unwrap ¶
func (e *KeyedAcquisitionError[K]) Unwrap() error
Unwrap returns the underlying keyed acquisition failure.
type KeyedMutex ¶
type KeyedMutex[K comparable] struct { // contains filtered or unexported fields }
KeyedMutex serializes work by comparable key and removes entries after their last holder or waiter exits.
func NewKeyedMutex ¶
func NewKeyedMutex[K comparable](name string) *KeyedMutex[K]
NewKeyedMutex returns a keyed lock using DefaultMaxKeys.
func NewKeyedMutexWithLimit ¶
func NewKeyedMutexWithLimit[K comparable](name string, maxKeys int) *KeyedMutex[K]
NewKeyedMutexWithLimit returns a keyed lock with a maximum count of referenced keys.
func (*KeyedMutex[K]) ActiveKeys ¶
func (m *KeyedMutex[K]) ActiveKeys() int
ActiveKeys returns the number of keys currently held or awaited.
func (*KeyedMutex[K]) Lock ¶
func (m *KeyedMutex[K]) Lock(key K)
Lock acquires the lock for key and panics when the active-key limit has been reached.
func (*KeyedMutex[K]) LockContext ¶
func (m *KeyedMutex[K]) LockContext(ctx context.Context, key K) error
LockContext acquires the lock for key or returns a context or active-key-capacity error.
func (*KeyedMutex[K]) LockGuard ¶
func (m *KeyedMutex[K]) LockGuard(ctx context.Context, key K) (*KeyGuard[K], error)
LockGuard acquires key and returns an exact release token.
func (*KeyedMutex[K]) MaxKeys ¶
func (m *KeyedMutex[K]) MaxKeys() int
MaxKeys returns the configured active-key limit.
func (*KeyedMutex[K]) Name ¶
func (m *KeyedMutex[K]) Name() string
Name returns the immutable diagnostic name.
func (*KeyedMutex[K]) SetLocation ¶
func (m *KeyedMutex[K]) SetLocation(name string)
SetLocation changes the diagnostic name before the first operation and panics after first use.
func (*KeyedMutex[K]) Snapshot ¶
func (m *KeyedMutex[K]) Snapshot(key K) (LockState, bool)
Snapshot returns the state for key and reports whether the key is referenced.
func (*KeyedMutex[K]) TryLock ¶
func (m *KeyedMutex[K]) TryLock(key K) bool
TryLock attempts to acquire key without waiting or retaining a failed entry reference.
func (*KeyedMutex[K]) Unlock ¶
func (m *KeyedMutex[K]) Unlock(key K)
Unlock releases the lock for key.
type LockAttemptID ¶
type LockAttemptID uint64
LockAttemptID uniquely identifies one acquisition attempt within the process.
type LockEvent ¶
type LockEvent struct {
Name string
Mode LockMode
Kind LockEventKind
Result LockResult
AttemptID LockAttemptID
Contended bool
WaitDuration time.Duration
HoldDuration time.Duration
HoldDurationKnown bool
ExactHold bool
Callers []uintptr
State LockState
}
LockEvent describes one acquisition or release transition. Observers must treat the value and Callers slice as read-only.
Example ¶
package main
import (
"fmt"
"time"
"github.com/donomii/powerlock"
)
func main() {
event := powerlock.LockEvent{
Name: "cache",
Mode: powerlock.LockModeWrite,
Kind: powerlock.LockEventWaitExceeded,
Result: powerlock.LockResultBusy,
Contended: true,
WaitDuration: 2 * time.Second,
State: powerlock.LockState{
Name: "cache",
Readers: 1,
WaitingWriters: 1,
},
}
fmt.Println(event)
}
Output: powerlock event=wait_exceeded lock="cache" mode=write result=busy contended=true wait=2s hold=unmeasured readers=1 writer=false waiting_readers=0 waiting_writers=1 cancelled=false caller=unavailable
type LockEventKind ¶
type LockEventKind uint8
LockEventKind identifies a lock state transition reported to an observer.
const ( // LockEventWaitStarted reports that an acquisition joined the waiter queue. LockEventWaitStarted LockEventKind = iota + 1 // LockEventWaitExceeded reports that a queued acquisition crossed its wait threshold. LockEventWaitExceeded // LockEventAcquired reports that ownership was granted. LockEventAcquired // LockEventReleased reports that ownership was released. LockEventReleased // LockEventTryFailed reports that a non-blocking acquisition did not acquire ownership. LockEventTryFailed // LockEventRejected reports that cancellation, expiry, or capacity ended an acquisition. LockEventRejected // LockEventHoldExceeded reports that an exact acquisition crossed its hold threshold. LockEventHoldExceeded )
func (LockEventKind) String ¶
func (k LockEventKind) String() string
String returns the diagnostic name for a lock event kind.
type LockGuard ¶
type LockGuard struct {
// contains filtered or unexported fields
}
LockGuard owns one exact acquisition and releases it once.
func (*LockGuard) AttemptID ¶
func (g *LockGuard) AttemptID() LockAttemptID
AttemptID returns the diagnostic identifier for the acquisition.
type LockMode ¶
type LockMode uint8
LockMode identifies whether an acquisition is exclusive or shared.
type LockObserver ¶
type LockObserver interface {
ObserveLock(LockEvent)
}
LockObserver receives ordered lock events. Calls may be concurrent across acquisitions, so implementations must be concurrency-safe, return promptly, not panic, and not acquire or release the lock emitting the event.
type LockObserverFunc ¶
type LockObserverFunc func(LockEvent)
LockObserverFunc adapts a function to LockObserver.
func (LockObserverFunc) ObserveLock ¶
func (f LockObserverFunc) ObserveLock(event LockEvent)
ObserveLock calls the observer function.
type LockObserverGroup ¶
type LockObserverGroup []LockObserver
LockObserverGroup delivers each event to its observers in order and must not be mutated while in use.
func (LockObserverGroup) ObserveLock ¶
func (g LockObserverGroup) ObserveLock(event LockEvent)
ObserveLock delivers event to each non-nil observer.
type LockResult ¶
type LockResult uint8
LockResult identifies the result of an acquisition attempt.
const ( // LockResultAcquired means ownership was granted. LockResultAcquired LockResult = iota + 1 // LockResultBusy means a non-blocking acquisition found incompatible ownership or an earlier waiter. LockResultBusy // LockResultQueueFull means a bounded waiter queue had no remaining capacity. LockResultQueueFull // LockResultCancelled means a context or permanently cancellable lock was cancelled. LockResultCancelled // LockResultDeadlineExceeded means the acquisition context expired before ownership was granted. LockResultDeadlineExceeded )
func (LockResult) String ¶
func (r LockResult) String() string
String returns the metric and diagnostic name for an acquisition result.
type LockState ¶
type LockState struct {
Name string
Version uint64
Readers int
Writer bool
WaitingReaders int
WaitingWriters int
Cancelled bool
OldestWaitDuration time.Duration
WriterHoldDuration time.Duration
ReaderCohortHoldDuration time.Duration
}
LockState is a point-in-time view of a lock.
type MaxMutex ¶
type MaxMutex struct {
// contains filtered or unexported fields
}
MaxMutex is the exclusive-only form of MaxRWMutex.
func NewMaxMutex ¶
NewMaxMutex returns a bounded exclusive lock using DefaultMaxWaiting.
func NewMaxMutexWithLimit ¶
NewMaxMutexWithLimit returns a bounded exclusive lock with the supplied waiter limit.
func (*MaxMutex) Lock ¶
func (m *MaxMutex) Lock()
Lock acquires the mutex and panics when the waiter limit has already been reached.
func (*MaxMutex) LockContext ¶
LockContext acquires the mutex or returns a context or queue-saturation error.
func (*MaxMutex) MaxWaiting ¶
MaxWaiting returns the configured waiter limit.
func (*MaxMutex) SetLocation ¶
SetLocation changes the diagnostic name before the first operation and panics after first use.
type MaxRWMutex ¶
type MaxRWMutex struct {
// contains filtered or unexported fields
}
MaxRWMutex is a FIFO read/write mutex that limits blocked acquisitions without limiting current holders.
func NewMaxRWMutex ¶
func NewMaxRWMutex(location string) *MaxRWMutex
NewMaxRWMutex returns a bounded lock using DefaultMaxWaiting.
func NewMaxRWMutexWithLimit ¶
func NewMaxRWMutexWithLimit(location string, maxWaiting int) *MaxRWMutex
NewMaxRWMutexWithLimit returns a bounded lock that permits at most maxWaiting blocked acquisitions.
func (*MaxRWMutex) Lock ¶
func (m *MaxRWMutex) Lock()
Lock acquires the write lock and panics when the waiter limit has already been reached.
func (*MaxRWMutex) LockContext ¶
func (m *MaxRWMutex) LockContext(ctx context.Context) error
LockContext acquires the write lock or returns a context or queue-saturation error.
func (*MaxRWMutex) MaxWaiting ¶
func (m *MaxRWMutex) MaxWaiting() int
MaxWaiting returns the configured waiter limit.
func (*MaxRWMutex) Name ¶
func (m *MaxRWMutex) Name() string
Name returns the immutable diagnostic name.
func (*MaxRWMutex) RLock ¶
func (m *MaxRWMutex) RLock()
RLock acquires a read lock and panics when the waiter limit has already been reached.
func (*MaxRWMutex) RLockContext ¶
func (m *MaxRWMutex) RLockContext(ctx context.Context) error
RLockContext acquires a read lock or returns a context or queue-saturation error.
func (*MaxRWMutex) RLocker ¶
func (m *MaxRWMutex) RLocker() sync.Locker
RLocker returns a sync.Locker backed by RLock and RUnlock.
func (*MaxRWMutex) SetLocation ¶
func (m *MaxRWMutex) SetLocation(location string)
SetLocation changes the diagnostic name before the first operation and panics after first use.
func (*MaxRWMutex) Snapshot ¶
func (m *MaxRWMutex) Snapshot() LockState
Snapshot returns the current lock state.
func (*MaxRWMutex) TryLock ¶
func (m *MaxRWMutex) TryLock() bool
TryLock attempts to acquire the write lock without joining the waiter queue.
func (*MaxRWMutex) TryRLock ¶
func (m *MaxRWMutex) TryRLock() bool
TryRLock attempts to acquire a read lock without joining or bypassing the waiter queue.
type ObservedMutex ¶
type ObservedMutex struct {
// contains filtered or unexported fields
}
ObservedMutex is the exclusive-only form of ObservedRWMutex.
func NewObservedMutex ¶
func NewObservedMutex(name string, observer LockObserver) *ObservedMutex
NewObservedMutex returns an exclusive lock that emits structured events.
func (*ObservedMutex) LockContext ¶
func (m *ObservedMutex) LockContext(ctx context.Context) error
LockContext acquires the mutex or returns a cancellation or deadline error.
func (*ObservedMutex) LockGuard ¶
func (m *ObservedMutex) LockGuard(ctx context.Context) (*LockGuard, error)
LockGuard returns an exact acquisition guard.
func (*ObservedMutex) Name ¶
func (m *ObservedMutex) Name() string
Name returns the immutable diagnostic name.
func (*ObservedMutex) SetLocation ¶
func (m *ObservedMutex) SetLocation(name string)
SetLocation changes the diagnostic name before the first operation and panics after first use.
func (*ObservedMutex) Snapshot ¶
func (m *ObservedMutex) Snapshot() LockState
Snapshot returns the current lock state.
func (*ObservedMutex) TryLock ¶
func (m *ObservedMutex) TryLock() bool
TryLock attempts to acquire the mutex without waiting.
func (*ObservedMutex) TryLockGuard ¶
func (m *ObservedMutex) TryLockGuard() (*LockGuard, bool)
TryLockGuard attempts to acquire the mutex and returns an exact release token on success.
type ObservedRWMutex ¶
type ObservedRWMutex struct {
// contains filtered or unexported fields
}
ObservedRWMutex is a FIFO context-aware read/write lock that emits structured events.
func NewObservedRWMutex ¶
func NewObservedRWMutex(name string, observer LockObserver) *ObservedRWMutex
NewObservedRWMutex returns an observed lock. A nil observer disables event delivery.
func (*ObservedRWMutex) LockContext ¶
LockContext acquires the write lock or returns a cancellation or deadline error.
func (*ObservedRWMutex) LockGuard ¶
LockGuard acquires the write lock and returns an exact release token.
func (*ObservedRWMutex) Name ¶
func (m *ObservedRWMutex) Name() string
Name returns the immutable diagnostic name.
func (*ObservedRWMutex) RLockContext ¶
RLockContext acquires a read lock or returns a cancellation or deadline error.
func (*ObservedRWMutex) RLockGuard ¶
RLockGuard acquires a read lock and returns an exact release token.
func (*ObservedRWMutex) RUnlock ¶
func (m *ObservedRWMutex) RUnlock()
RUnlock releases one read lock.
func (*ObservedRWMutex) SetLocation ¶
func (m *ObservedRWMutex) SetLocation(location string)
SetLocation changes the diagnostic name before the first operation and panics after first use.
func (*ObservedRWMutex) Snapshot ¶
func (m *ObservedRWMutex) Snapshot() LockState
Snapshot returns the current lock state.
func (*ObservedRWMutex) TryLock ¶
func (m *ObservedRWMutex) TryLock() bool
TryLock attempts to acquire the write lock without waiting.
func (*ObservedRWMutex) TryLockGuard ¶
TryLockGuard attempts to acquire the write lock and returns an exact release token on success.
func (*ObservedRWMutex) TryRLock ¶
func (m *ObservedRWMutex) TryRLock() bool
TryRLock attempts to acquire a read lock without waiting or bypassing queued acquisitions.
func (*ObservedRWMutex) TryRLockGuard ¶
TryRLockGuard attempts to acquire a read lock and returns an exact release token on success.
type ProfileObserver ¶
type ProfileObserver struct {
// contains filtered or unexported fields
}
ProfileObserver exposes current waiters and exactly paired holders through runtime pprof profiles.
func DefaultProfileObserver ¶
func DefaultProfileObserver() *ProfileObserver
DefaultProfileObserver returns the process-wide Powerlock pprof observer.
func (*ProfileObserver) HolderCount ¶
func (o *ProfileObserver) HolderCount() int
HolderCount returns the number of acquisitions currently represented in the holder profile.
func (*ProfileObserver) HolderProfileName ¶
func (o *ProfileObserver) HolderProfileName() string
HolderProfileName returns the pprof profile containing exactly paired current holders.
func (*ProfileObserver) ObserveLock ¶
func (o *ProfileObserver) ObserveLock(event LockEvent)
ObserveLock updates the current waiter and exactly paired holder profiles.
func (*ProfileObserver) WaiterCount ¶
func (o *ProfileObserver) WaiterCount() int
WaiterCount returns the number of acquisitions currently represented in the waiter profile.
func (*ProfileObserver) WaiterProfileName ¶
func (o *ProfileObserver) WaiterProfileName() string
WaiterProfileName returns the pprof profile containing current waiters.
type RuntimeTraceObserver ¶
type RuntimeTraceObserver struct {
// contains filtered or unexported fields
}
RuntimeTraceObserver writes watchdog threshold events to the Go execution trace.
func NewRuntimeTraceObserver ¶
func NewRuntimeTraceObserver(onThreshold FlightRecorderCallback) *RuntimeTraceObserver
NewRuntimeTraceObserver returns a runtime trace observer with an optional flight-recorder callback.
func (*RuntimeTraceObserver) ObserveLock ¶
func (o *RuntimeTraceObserver) ObserveLock(event LockEvent)
ObserveLock records wait and hold threshold events under the powerlock trace category.
type WatchdogMutex ¶
type WatchdogMutex struct {
// contains filtered or unexported fields
}
WatchdogMutex is the exclusive-only form of WatchdogRWMutex.
func NewWatchdogMutex ¶
func NewWatchdogMutex(name string, observer LockObserver) *WatchdogMutex
NewWatchdogMutex returns an exclusive watchdog lock with the default thresholds.
func NewWatchdogMutexWithThresholds ¶
func NewWatchdogMutexWithThresholds(name string, waitThreshold time.Duration, holdThreshold time.Duration, observer LockObserver) *WatchdogMutex
NewWatchdogMutexWithThresholds returns an exclusive watchdog lock with explicit thresholds. Zero disables that threshold.
func (*WatchdogMutex) HoldThreshold ¶
func (m *WatchdogMutex) HoldThreshold() time.Duration
HoldThreshold returns the duration after which an exact active acquisition is reported.
func (*WatchdogMutex) LockContext ¶
func (m *WatchdogMutex) LockContext(ctx context.Context) error
LockContext acquires the mutex or returns a cancellation or deadline error.
func (*WatchdogMutex) LockGuard ¶
func (m *WatchdogMutex) LockGuard(ctx context.Context) (*LockGuard, error)
LockGuard returns an exact acquisition guard.
func (*WatchdogMutex) Name ¶
func (m *WatchdogMutex) Name() string
Name returns the immutable diagnostic name.
func (*WatchdogMutex) SetLocation ¶
func (m *WatchdogMutex) SetLocation(name string)
SetLocation changes the diagnostic name before the first operation and panics after first use.
func (*WatchdogMutex) Snapshot ¶
func (m *WatchdogMutex) Snapshot() LockState
Snapshot returns the current lock state.
func (*WatchdogMutex) TryLock ¶
func (m *WatchdogMutex) TryLock() bool
TryLock attempts to acquire the mutex without waiting.
func (*WatchdogMutex) TryLockGuard ¶
func (m *WatchdogMutex) TryLockGuard() (*LockGuard, bool)
TryLockGuard attempts to acquire the mutex and returns an exact release token on success.
func (*WatchdogMutex) WaitThreshold ¶
func (m *WatchdogMutex) WaitThreshold() time.Duration
WaitThreshold returns the duration after which a queued acquisition is reported.
type WatchdogRWMutex ¶
type WatchdogRWMutex struct {
// contains filtered or unexported fields
}
WatchdogRWMutex reports slow waits and long exact holds while providing FIFO context-aware locking.
func NewWatchdogRWMutex ¶
func NewWatchdogRWMutex(name string, observer LockObserver) *WatchdogRWMutex
NewWatchdogRWMutex returns a watchdog lock with the default wait and hold thresholds.
func NewWatchdogRWMutexWithThresholds ¶
func NewWatchdogRWMutexWithThresholds(name string, waitThreshold time.Duration, holdThreshold time.Duration, observer LockObserver) *WatchdogRWMutex
NewWatchdogRWMutexWithThresholds returns a watchdog lock with explicit thresholds. Zero disables that threshold.
func (*WatchdogRWMutex) HoldThreshold ¶
func (m *WatchdogRWMutex) HoldThreshold() time.Duration
HoldThreshold returns the duration after which an exact active acquisition is reported.
func (*WatchdogRWMutex) LockContext ¶
LockContext acquires the write lock or returns a cancellation or deadline error.
func (*WatchdogRWMutex) LockGuard ¶
LockGuard acquires the write lock and returns an exact release token.
func (*WatchdogRWMutex) Name ¶
func (m *WatchdogRWMutex) Name() string
Name returns the immutable diagnostic name.
func (*WatchdogRWMutex) RLockContext ¶
RLockContext acquires a read lock or returns a cancellation or deadline error.
func (*WatchdogRWMutex) RLockGuard ¶
RLockGuard acquires a read lock and returns an exact release token.
func (*WatchdogRWMutex) RUnlock ¶
func (m *WatchdogRWMutex) RUnlock()
RUnlock releases one read lock.
func (*WatchdogRWMutex) SetLocation ¶
func (m *WatchdogRWMutex) SetLocation(name string)
SetLocation changes the non-empty diagnostic name before the first operation and panics after first use.
func (*WatchdogRWMutex) Snapshot ¶
func (m *WatchdogRWMutex) Snapshot() LockState
Snapshot returns the current lock state.
func (*WatchdogRWMutex) TryLock ¶
func (m *WatchdogRWMutex) TryLock() bool
TryLock attempts to acquire the write lock without waiting.
func (*WatchdogRWMutex) TryLockGuard ¶
TryLockGuard attempts to acquire the write lock and returns an exact release token on success.
func (*WatchdogRWMutex) TryRLock ¶
func (m *WatchdogRWMutex) TryRLock() bool
TryRLock attempts to acquire a read lock without waiting or bypassing queued acquisitions.
func (*WatchdogRWMutex) TryRLockGuard ¶
TryRLockGuard attempts to acquire a read lock and returns an exact release token on success.
func (*WatchdogRWMutex) WaitThreshold ¶
func (m *WatchdogRWMutex) WaitThreshold() time.Duration
WaitThreshold returns the duration after which a queued acquisition is reported.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
cmd
|
|
|
checkfmt
command
|
|
|
examples
|
|
|
cancel
command
|
|
|
context
command
|
|
|
keyed
command
|
|
|
max
command
|
|
|
metrics
command
|
|
|
watchdog
command
|
|
|
Package powerlockprometheus converts Powerlock's structured lock events into bounded Prometheus metrics.
|
Package powerlockprometheus converts Powerlock's structured lock events into bounded Prometheus metrics. |
