coma

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jul 22, 2026 License: MIT Imports: 3 Imported by: 0

README

coma

a tiny concurrency manager for go

coma makes sure your program doesn't get overwhelmed with goroutines and fall into a coma.

  1. limits how many goroutines run at once
  2. waits until all of them are done
  3. is simple
  4. works

install

go get github.com/zlatej/coma

usage

cm := coma.New(5) // at most 5 goroutines at a time

for _, task := range tasks {
    // blocks until a slot is free and acquires it
    if err := cm.AcquireContext(ctx); err != nil {
        // error means either the context is done or Wait has been called
        return
    } 
    go func(t Task) {
        defer cm.Release() // releases slot
        process(t)
    }(task)
}

cm.Wait() // blocks until all slots are released

no context? use cm.Acquire() instead

notes

  • Wait is terminal: once called, the manager cannot be reused, Acquire/AcquireContext will return ErrClosed.
  • calling Wait again from the same goroutine is a safe no-op, however calling it concurrently from multiple goroutines may panic.

license

MIT

Documentation

Overview

Package coma limits how many goroutines run concurrently and waits for all of them to finish.

Index

Constants

This section is empty.

Variables

View Source
var ErrClosed = errors.New("coma: manager is shut down")

ErrClosed is returned by Acquire and AcquireContext when Wait has been called.

Functions

This section is empty.

Types

type ConcurrencyManager

type ConcurrencyManager struct {
	// contains filtered or unexported fields
}

ConcurrencyManager limits how many goroutines can run concurrently.

func New

func New(max int32) *ConcurrencyManager

New creates a ConcurrencyManager that allows at most max concurrently running goroutines.

func (*ConcurrencyManager) Acquire

func (c *ConcurrencyManager) Acquire() error

Acquire blocks until a slot is available and claims it for a new goroutine. If Wait has been called, Acquire returns ErrClosed.

func (*ConcurrencyManager) AcquireContext

func (c *ConcurrencyManager) AcquireContext(ctx context.Context) error

AcquireContext blocks until a slot is available and claims it for a new goroutine, or returns ctx.Err() if the context is done first. If Wait has been called, AcquireContext returns ErrClosed.

func (*ConcurrencyManager) Release

func (c *ConcurrencyManager) Release()

Release marks a goroutine as finished and releases one slot.

func (*ConcurrencyManager) RunningCount

func (c *ConcurrencyManager) RunningCount() int32

RunningCount returns the number of currently running goroutines.

func (*ConcurrencyManager) Wait

func (c *ConcurrencyManager) Wait()

Wait waits until all goroutines are done. Wait is terminal, meaning ConcurrencyManager cannot be reused. Wait is not safe for concurrent use - calling it from multiple goroutines at once may panic. A repeated call from the same goroutine is a safe no-op.

Jump to

Keyboard shortcuts

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