suspend

package
v1.7.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 26, 2026 License: AGPL-3.0 Imports: 3 Imported by: 0

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) IsPaused

func (m *Manager) IsPaused() bool

IsPaused returns true if currently paused.

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

func (m *Manager) Wait(ctx context.Context) error

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL