• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

.github/workflows/H08-Jan-2020-

.gitignoreH A D08-Jan-2020275

LICENSEH A D08-Jan-202011.1 KiB

README.mdH A D08-Jan-20202.5 KiB

actors.goH A D08-Jan-2020949

example_test.goH A D08-Jan-20202.2 KiB

go.modH A D08-Jan-202037

group.goH A D08-Jan-20201.8 KiB

group_test.goH A D08-Jan-20201.2 KiB

README.md

1# run
2
3[![GoDoc](https://godoc.org/github.com/oklog/run?status.svg)](https://godoc.org/github.com/oklog/run)
4[![Build Status](https://img.shields.io/endpoint.svg?url=https%3A%2F%2Factions-badge.atrox.dev%2Foklog%2Frun%2Fbadge&style=flat-square&label=build)](https://github.com/oklog/run/actions?query=workflow%3ATest)
5[![Go Report Card](https://goreportcard.com/badge/github.com/oklog/run)](https://goreportcard.com/report/github.com/oklog/run)
6[![Apache 2 licensed](https://img.shields.io/badge/license-Apache2-blue.svg)](https://raw.githubusercontent.com/oklog/run/master/LICENSE)
7
8run.Group is a universal mechanism to manage goroutine lifecycles.
9
10Create a zero-value run.Group, and then add actors to it. Actors are defined as
11a pair of functions: an **execute** function, which should run synchronously;
12and an **interrupt** function, which, when invoked, should cause the execute
13function to return. Finally, invoke Run, which concurrently runs all of the
14actors, waits until the first actor exits, invokes the interrupt functions, and
15finally returns control to the caller only once all actors have returned. This
16general-purpose API allows callers to model pretty much any runnable task, and
17achieve well-defined lifecycle semantics for the group.
18
19run.Group was written to manage component lifecycles in func main for
20[OK Log](https://github.com/oklog/oklog).
21But it's useful in any circumstance where you need to orchestrate multiple
22goroutines as a unit whole.
23[Click here](https://www.youtube.com/watch?v=LHe1Cb_Ud_M&t=15m45s) to see a
24video of a talk where run.Group is described.
25
26## Examples
27
28### context.Context
29
30```go
31ctx, cancel := context.WithCancel(context.Background())
32g.Add(func() error {
33	return myProcess(ctx, ...)
34}, func(error) {
35	cancel()
36})
37```
38
39### net.Listener
40
41```go
42ln, _ := net.Listen("tcp", ":8080")
43g.Add(func() error {
44	return http.Serve(ln, nil)
45}, func(error) {
46	ln.Close()
47})
48```
49
50### io.ReadCloser
51
52```go
53var conn io.ReadCloser = ...
54g.Add(func() error {
55	s := bufio.NewScanner(conn)
56	for s.Scan() {
57		println(s.Text())
58	}
59	return s.Err()
60}, func(error) {
61	conn.Close()
62})
63```
64
65## Comparisons
66
67Package run is somewhat similar to package
68[errgroup](https://godoc.org/golang.org/x/sync/errgroup),
69except it doesn't require actor goroutines to understand context semantics.
70
71It's somewhat similar to package
72[tomb.v1](https://godoc.org/gopkg.in/tomb.v1) or
73[tomb.v2](https://godoc.org/gopkg.in/tomb.v2),
74except it has a much smaller API surface, delegating e.g. staged shutdown of
75goroutines to the caller.
76