concurrency

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Apr 14, 2025 License: Apache-2.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 manages concurrent executions with enhanced features

func NewManager

func NewManager(max int32) (*Manager, error)

NewManager creates a new concurrency manager with validation

Usage:

// Create a manager that allows up to 10 concurrent operations
cm, err := NewManager(10)
if err != nil {
    log.Fatal(err)
}

// Example 1: Basic usage with context timeout
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()

if err := cm.Acquire(ctx); err != nil {
    // Handle acquisition failure (timeout/cancellation)
    return err
}
defer cm.Release()  // Always release when done

// Example 2: Non-blocking attempt
if acquired := cm.TryAcquire(); acquired {
    defer cm.Release()
    // Do work...
} else {
    // Handle busy case
}

// Example 3: Check metrics
metrics := cm.GetMetrics()
log.Printf("Current usage: %d/%d", metrics["current"], 10)

func (*Manager) Acquire

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

Acquire attempts to acquire a concurrency slot with timeout

func (*Manager) Available

func (m *Manager) Available() int32

Available returns the number of available slots

func (*Manager) GetMetrics

func (m *Manager) GetMetrics() map[string]int64

GetMetrics returns current metrics

func (*Manager) Release

func (m *Manager) Release()

Release releases a concurrency slot

func (*Manager) TryAcquire

func (m *Manager) TryAcquire() bool

TryAcquire attempts to acquire without blocking

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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