https://github.com/ash2k/stager
A tiny Go library for deterministic shutdown of goroutines
https://github.com/ash2k/stager
context goroutine graceful-shutdown shutdown
Last synced: 6 months ago
JSON representation
A tiny Go library for deterministic shutdown of goroutines
- Host: GitHub
- URL: https://github.com/ash2k/stager
- Owner: ash2k
- License: apache-2.0
- Created: 2017-06-19T13:24:30.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2024-05-03T06:18:44.000Z (over 1 year ago)
- Last Synced: 2025-03-26T18:52:19.718Z (6 months ago)
- Topics: context, goroutine, graceful-shutdown, shutdown
- Language: Go
- Homepage:
- Size: 23.4 KB
- Stars: 10
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Stager
Stager is a library to help write code where you are in control of start and shutdown of concurrent operations.
I.e. you know when goroutines start and stop and in which order.An [example](example/main.go) is below:
```go
package mainimport (
"context"
"log"
"time""github.com/ash2k/stager"
)func main() {
defer log.Print("Exiting main")
st := stager.New()s := st.NextStage()
s.Go(func(ctx context.Context) error {
log.Print("Start 1.1")
defer log.Print("Stop 1.1")
<-ctx.Done()
return nil
})
s.Go(func(ctx context.Context) error {
log.Print("Start 1.2")
defer log.Print("Stop 1.2")
<-ctx.Done()
return nil
})s = st.NextStage()
s.Go(func(ctx context.Context) error {
log.Print("Start 2")
defer log.Print("Stop 2")
<-ctx.Done()
return nil
})s = st.NextStage()
s.Go(func(ctx context.Context) error {
log.Print("Start 3")
defer log.Print("Stop 3")
<-ctx.Done()
return nil
})ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
err := st.Run(ctx)
if err != nil {
log.Fatal(err)
}
}
```
Output:
```
2020/12/15 15:34:41 Start 3
2020/12/15 15:34:41 Start 1.2
2020/12/15 15:34:41 Start 1.1
2020/12/15 15:34:41 Start 2
2020/12/15 15:34:46 Stop 3
2020/12/15 15:34:46 Stop 2
2020/12/15 15:34:46 Stop 1.2
2020/12/15 15:34:46 Stop 1.1
2020/12/15 15:34:46 Exiting main
```Note the following:
- Shutdown order is deterministic - 3, 2, and then 1.
- Shutdown order within a stage is not deterministic - 1.1 and 1.2 are not ordered.