Skip to content
ZlatejPublic

About

go concurrency manager

Topics

Resources

Stars

3 stars

Watchers

0 watching

Forks

Latest commit

 

History

63 Commits

Folders and files

Repository files navigation

coma

Go Reference Go

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

gate := 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 := gate.AcquireContext(ctx); err != nil {
        // error means either the context is done or Drain has been called
        return
    }
    go func() {
        defer gate.Release() // releases slot
        process(task)
    }()
}

fmt.Printf("currently holding %d slots\n", gate.Held())

gate.Drain() // blocks until all slots are released

no context? use gate.Acquire() instead

remote graceful shutdown

if you want to close the Gate from one place, but still wait until all tasks are done in a different place.

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

go func() {
    <-sigint
    gate.Close() // close from remote call
}()

for {
    task := getTask()
    if err := gate.Acquire(); err != nil { // returns ErrClosed after gate.Close()
        break
    }
    go func() {
        defer gate.Release()
        process(task)
    }()
}

gate.Drain() // already closed, just wait until all slots are released

notes

  • New treats a max of less than 1 as 1.
  • Close is terminal: once called, the Gate cannot be reused, Acquire/AcquireContext will return ErrClosed.
  • Drain behaves like Close but then blocks until all slots are released.
  • Drain and Close can be combined and can safely be called any number of times, including concurrently from multiple goroutines.
  • every successful Acquire/AcquireContext must be matched by exactly one Release. An unmatched Release panics when no slot is held.

license

MIT

About

go concurrency manager

Topics

Resources

Stars

3 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages