Skip to content

Latest commit

 

History

33 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

fmt.Printf("currently running %d goroutines\n", cm.RunningCount())

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.
  • Wait can safely be called any number of times, including concurrently from multiple goroutines.
  • every successful Acquire/AcquireContext must be matched by exactly one Release.

license

MIT

About

go concurrency manager

Topics

Resources

Stars

4 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages