types

package
v1.19.4 Latest Latest
Warning

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

Go to latest
Published: Feb 24, 2026 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package types defines the core interfaces for semaphore implementations with progress tracking. These interfaces provide the foundation for concurrent worker management with optional visual progress bars.

See the implementation packages:

  • github.com/nabbar/golib/semaphore/sem - Base semaphore implementations
  • github.com/nabbar/golib/semaphore/bar - Progress bar-enabled semaphores
  • github.com/nabbar/golib/semaphore - Combined semaphore with progress tracking

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Bar

type Bar interface {
	// Inc increments the progress bar by n.
	// This is a convenience method that calls Inc64 with int64(n).
	Inc(n int)

	// Dec decrements the progress bar by n.
	// This is a convenience method that calls Dec64 with int64(n).
	Dec(n int)

	// Inc64 increments the progress bar by n (64-bit version).
	// Use this for large progress values or when n is already int64.
	Inc64(n int64)

	// Dec64 decrements the progress bar by n (64-bit version).
	// Implemented internally as Inc64(-n).
	Dec64(n int64)

	// Reset resets the progress bar to new total and current values.
	// This allows reusing the same bar for different operations.
	//
	// Parameters:
	//   - tot: New total/maximum value
	//   - current: New current progress value
	Reset(tot, current int64)

	// Complete marks the progress bar as complete.
	// If MPB is enabled, this triggers the completion animation.
	Complete()

	// Completed returns true if the progress bar has completed or was aborted.
	// Without MPB, this always returns true.
	Completed() bool

	// Current returns the current progress value.
	// Without MPB, returns the total value.
	Current() int64

	// Total returns the total/maximum value of the progress bar.
	Total() int64
}

Bar defines the interface for progress bar operations. It provides methods for updating, querying, and controlling a progress bar's state.

The Bar interface is typically combined with Sem to create SemBar, allowing progress tracking alongside worker management.

See: github.com/nabbar/golib/semaphore/bar

type BarMPB

type BarMPB interface {
	// GetMPB returns the underlying MPB bar instance.
	// Returns nil if progress bar is disabled.
	//
	// Use this for advanced MPB operations not covered by the Bar interface.
	GetMPB() *sdkmpb.Bar
}

BarMPB provides access to the underlying MPB (Multi-Progress Bar) instance. This interface allows low-level manipulation of the progress bar when needed.

See: github.com/vbauerster/mpb/v8

type Progress

type Progress interface {
	// BarBytes creates a progress bar for tracking byte quantities (files, downloads, etc.).
	// The bar displays sizes in human-readable format (KB, MB, GB, etc.).
	//
	// Parameters:
	//   - name: Display name for the bar
	//   - job: Job description to display
	//   - tot: Total number of bytes to process
	//   - drop: If true, removes the bar from display when complete
	//   - bar: Optional existing bar to queue after (for sequential display)
	//
	// Returns:
	//   - SemBar: A semaphore with progress bar configured for byte tracking
	BarBytes(name, job string, tot int64, drop bool, bar SemBar) SemBar

	// BarTime creates a progress bar for time-based operations.
	// Displays progress with time estimates (ETA, elapsed time).
	//
	// Parameters:
	//   - name: Display name for the bar
	//   - job: Job description to display
	//   - tot: Total number of time units
	//   - drop: If true, removes the bar from display when complete
	//   - bar: Optional existing bar to queue after
	//
	// Returns:
	//   - SemBar: A semaphore with progress bar configured for time tracking
	BarTime(name, job string, tot int64, drop bool, bar SemBar) SemBar

	// BarNumber creates a progress bar for tracking numeric quantities.
	// Displays progress as a simple counter (e.g., "45/100").
	//
	// Parameters:
	//   - name: Display name for the bar
	//   - job: Job description to display
	//   - tot: Total number of items to process
	//   - drop: If true, removes the bar from display when complete
	//   - bar: Optional existing bar to queue after
	//
	// Returns:
	//   - SemBar: A semaphore with progress bar configured for number tracking
	BarNumber(name, job string, tot int64, drop bool, bar SemBar) SemBar

	// BarOpts creates a progress bar with custom MPB options.
	// Use this for full control over bar appearance and behavior.
	//
	// Parameters:
	//   - tot: Total number of items to process
	//   - drop: If true, removes the bar from display when complete
	//   - opts: MPB bar options for customization
	//
	// Returns:
	//   - SemBar: A semaphore with custom progress bar
	//
	// See: github.com/vbauerster/mpb/v8 for available options
	BarOpts(tot int64, drop bool, opts ...sdkmpb.BarOption) SemBar
}

Progress defines the interface for creating progress bars with different display formats. It provides factory methods for common progress bar types (bytes, time, numbers) with pre-configured decorators and formatting.

See: github.com/nabbar/golib/semaphore

type ProgressMPB

type ProgressMPB interface {
	// GetMPB returns the underlying MPB progress container.
	// Returns nil if progress tracking is disabled.
	//
	// Use this for:
	//   - Adding custom progress bars
	//   - Accessing MPB container-level operations
	//   - Coordinating multiple progress bars
	GetMPB() *sdkmpb.Progress
}

ProgressMPB provides access to the underlying MPB (Multi-Progress Bar) container. This interface allows access to the progress bar container for advanced use cases.

See: github.com/vbauerster/mpb/v8

type Sem

type Sem interface {
	context.Context // Inherits context functionality for cancellation and deadlines

	// NewWorker acquires a worker slot, blocking until one is available or context is cancelled.
	// Returns an error if the context is cancelled before acquiring a slot.
	//
	// Usage:
	//   if err := sem.NewWorker(); err != nil {
	//       return err
	//   }
	//   defer sem.DeferWorker()
	NewWorker() error

	// NewWorkerTry attempts to acquire a worker slot without blocking.
	// Returns true if successful, false if no slots are available.
	//
	// Usage:
	//   if sem.NewWorkerTry() {
	//       defer sem.DeferWorker()
	//       // process work
	//   }
	NewWorkerTry() bool

	// DeferWorker releases a previously acquired worker slot.
	// Should be called with defer immediately after NewWorker() or NewWorkerTry() succeeds.
	DeferWorker()

	// DeferMain cancels the semaphore's context and releases all resources.
	// Should be called with defer in the main goroutine that created the semaphore.
	DeferMain()

	// WaitAll blocks until all worker slots are available (all workers have completed).
	// Returns an error if the context is cancelled while waiting.
	//
	// Use this to wait for all spawned workers to complete before proceeding.
	WaitAll() error

	// Weighted returns the maximum number of concurrent workers allowed.
	// Returns -1 for unlimited concurrency (WaitGroup mode).
	// Returns the configured limit for weighted semaphore mode.
	Weighted() int64

	// New creates a new independent semaphore with the same concurrency limit.
	// The new semaphore inherits the parent's context but operates independently.
	New() Sem
}

Sem defines the core semaphore interface for managing concurrent goroutine execution. It implements context.Context for lifecycle management and provides worker slot acquisition/release.

The semaphore can operate in two modes:

  • Limited: Fixed number of concurrent workers (weighted semaphore)
  • Unlimited: No concurrency limit (WaitGroup-based)

See: github.com/nabbar/golib/semaphore/sem

type SemBar

type SemBar interface {
	Sem // Core semaphore functionality
	Bar // Progress bar operations
}

SemBar combines semaphore functionality with progress bar operations. It extends the base Sem interface with Bar methods for tracking progress.

Use this interface when you need worker management with progress bar updates. See: github.com/nabbar/golib/semaphore/bar

type SemPgb

type SemPgb interface {
	Sem         // Core semaphore functionality
	ProgressMPB // Access to MPB progress container
}

SemPgb combines semaphore functionality with MPB (Multi-Progress Bar) progress tracking. It extends the base Sem interface with access to the underlying MPB progress container.

Use this interface when you need both worker management and progress visualization. See: github.com/nabbar/golib/semaphore

Jump to

Keyboard shortcuts

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