| 1 |
## RunnerGroup |
| 2 |
|
| 3 |
[](https://godoc.org/github.com/txthinking/runnergroup) |
| 4 |
[](https://github.com/txthinking/runnergroup/blob/master/LICENSE) |
| 5 |
|
| 6 |
RunnerGroup is like [sync.WaitGroup](https://pkg.go.dev/sync?tab=doc#WaitGroup), the diffrence is if one task stops, all will be stopped. |
| 7 |
|
| 8 |
❤️ A project by [txthinking.com](https://www.txthinking.com) |
| 9 |
|
| 10 |
### Install |
| 11 |
|
| 12 |
$ go get github.com/txthinking/runnergroup |
| 13 |
|
| 14 |
### Example |
| 15 |
|
| 16 |
``` |
| 17 |
import ( |
| 18 |
"context" |
| 19 |
"log" |
| 20 |
"net/http" |
| 21 |
"time" |
| 22 |
|
| 23 |
"github.com/txthinking/runnergroup" |
| 24 |
) |
| 25 |
|
| 26 |
func Example() { |
| 27 |
g := runnergroup.New() |
| 28 |
|
| 29 |
s := &http.Server{ |
| 30 |
Addr: ":9991", |
| 31 |
} |
| 32 |
g.Add(&runnergroup.Runner{ |
| 33 |
Start: func() error { |
| 34 |
return s.ListenAndServe() |
| 35 |
}, |
| 36 |
Stop: func() error { |
| 37 |
return s.Shutdown(context.Background()) |
| 38 |
}, |
| 39 |
}) |
| 40 |
|
| 41 |
s1 := &http.Server{ |
| 42 |
Addr: ":9992", |
| 43 |
} |
| 44 |
g.Add(&runnergroup.Runner{ |
| 45 |
Start: func() error { |
| 46 |
return s1.ListenAndServe() |
| 47 |
}, |
| 48 |
Stop: func() error { |
| 49 |
return s1.Shutdown(context.Background()) |
| 50 |
}, |
| 51 |
}) |
| 52 |
|
| 53 |
go func() { |
| 54 |
time.Sleep(5 * time.Second) |
| 55 |
log.Println(g.Done()) |
| 56 |
}() |
| 57 |
log.Println(g.Wait()) |
| 58 |
// Output: |
| 59 |
} |
| 60 |
|
| 61 |
``` |
| 62 |
|
| 63 |
## License |
| 64 |
|
| 65 |
Licensed under The MIT License |