Skip to content

41north/async.go

Repository files navigation

async.go

Build Coverage Status License

Status: EXPERIMENTAL

This library is primarily intended as a directed learning exercise and eventual collection of utilities and patterns for working asynchronously in Go.

Documentation

GoDoc

Full go doc style documentation for the project can be viewed online without installing this package by using the excellent GoDoc site here: http://godoc.org/github.com/41north/async.go

You can also view the documentation locally once the package is installed with the godoc tool by running godoc -http=":6060" and pointing your browser to http://localhost:6060/pkg/github.com/41north/async.go

Installation

$ go get -u github.com/41north/async.go

Add this import line to the file you're working in:

import "github.com/41north/async.go"

Quick Start

Future

A basic example:

// create a string future
f := NewFuture[string]()

// create a consumer channel
ch := f.Get()
go func() {
	println(fmt.Sprintf("Value: %s", <-ch))
}()

// set the value
f.Set("hello")

Counting Semaphore

A basic example:

// we create an input and output channel for work needing to be done
inCh := make(chan string, 128)
outCh := make(chan int, 128)

// we want a max of 10 in-flight processes
s := NewCountingSemaphore(10)

// we create more workers than tokens available
for i := 0; i < 100; i++ {
	go func() {
		for {
			// acquire a token, waiting until one is available
			s.Acquire(1)

			// consume from the input channel
			v, ok := <-inCh
			if !ok {
				// channel was closed
				return
			}

			// do some work and produce an output value
			outCh <- len(v)

			// you need to be careful about releasing, if possible perform it with defer
			s.Release(1)
		}
	}()
}

// generate some work and put it into the work queue
// ...
// ...

There are more examples available in the go doc.

License

Go-async is licensed under the Apache 2.0 License

Contact

If you want to get in touch drop us an email at hello@41north.dev

About

A collection of utilities for async code in Go.

Topics

Resources

License

Code of conduct

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published