https://github.com/zhangpeihao/shutdown
Gracefully shutdown using context.Context and sync.GroupWait
https://github.com/zhangpeihao/shutdown
context golang graceful graceful-shutdown shutdown waitgroup
Last synced: 5 months ago
JSON representation
Gracefully shutdown using context.Context and sync.GroupWait
- Host: GitHub
- URL: https://github.com/zhangpeihao/shutdown
- Owner: zhangpeihao
- License: mit
- Created: 2017-03-10T14:10:48.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2017-03-12T10:49:04.000Z (over 9 years ago)
- Last Synced: 2024-06-20T12:01:49.209Z (about 2 years ago)
- Topics: context, golang, graceful, graceful-shutdown, shutdown, waitgroup
- Language: Go
- Size: 7.81 KB
- Stars: 10
- Watchers: 2
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Gracefully shutdown using context.Context and sync.WaitGroup
[](https://godoc.org/github.com/zhangpeihao/shutdown)
[](https://goreportcard.com/report/github.com/zhangpeihao/shutdown)
## Example
```
// Generate a new context
ctx := NewContext()
// Run service with this context
go func(ctx context.Context) {
if err := ExitWaitGroupAdd(ctx, 1); err != nil {
return
}
defer ExitWaitGroupDone(ctx)
otherEvent := make(chan struct{})
FOR_LOOP:
for {
select {
case <-ctx.Done():
break FOR_LOOP
case <-otherEvent:
// ...
}
}
// Some close processes
}(ctx)
// Wait interrupt signal and shutdown gracefully
if err := WaitAndShutdown(ctx, time.Second*5, func(timeout time.Duration) error {
log.Println("close")
return nil
}); err != nil {
log.Println("Shutdown error:", err)
return
}
```