Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Manager ¶
type Manager struct {
// contains filtered or unexported fields
}
Manager provides channel-based suspend/resume with context cancellation support. It offers an alternative to sync.Cond for scenarios where context-aware waiting is needed.
Example usage:
type Worker struct {
lifecycle.BaseWorker
suspendMgr *lifecycle.SuspendManager
}
func (w *Worker) Run(ctx context.Context) error {
for {
// Respect suspend with context cancellation
if err := w.suspendMgr.Wait(ctx); err != nil {
return err
}
// Do work...
}
}
func (w *Worker) Suspend(ctx context.Context) error {
w.suspendMgr.Pause()
return nil
}
func (w *Worker) Resume(ctx context.Context) error {
w.suspendMgr.Resume()
return nil
}
func NewManager ¶
func NewManager() *Manager
NewManager creates a new suspend manager in the running state.
func (*Manager) Pause ¶
func (m *Manager) Pause()
Pause requests that work should stop. This method is idempotent and safe to call multiple times.
func (*Manager) Resume ¶
func (m *Manager) Resume()
Resume allows work to continue after a pause. This method is idempotent and safe to call multiple times.
func (*Manager) Wait ¶
Wait blocks until work can continue or the context is cancelled. Returns nil when resumed, or ctx.Err() if the context is cancelled.
Call this at strategic points in your worker loop:
for {
if err := mgr.Wait(ctx); err != nil {
return err // Context cancelled during pause
}
// Continue working...
}
Performance: Uses polling with 50ms interval when paused. For high-frequency suspend/resume (>10k/sec), consider sync.Cond instead.