-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_test.go
More file actions
54 lines (40 loc) · 997 Bytes
/
Copy pathexample_test.go
File metadata and controls
54 lines (40 loc) · 997 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
//nolint:goerr113
package shutdown_test
import (
"context"
"errors"
"time"
"github.com/burik666/shutdown"
)
func ExampleWatch() {
ctx, ctxCancel := shutdown.Watch(
// Allows pressing Ctrl+C twice to force shutdown
shutdown.WithDoubleSignal(),
// Timeout for graceful shutdown
shutdown.WithTimeout(30*time.Second))
defer ctxCancel()
go func() {
// Do something
}()
<-ctx.Done()
// shutdown and cleanup
}
func ExampleWithWatcher() {
ctx, ctxCancel := shutdown.Watch(
shutdown.WithWatcher(func(ctx context.Context, ch chan<- error) error {
// If something occurs and you need to shut down the application,
// send an error to the channel (ch) to trigger the shutdown process.
ch <- errors.New("shutdown")
// To shut down the application immediately,
// send another error to the channel (ch).
ch <- errors.New("shutdown NOW")
return nil
}),
)
defer ctxCancel()
go func() {
// Do something
}()
<-ctx.Done()
// Shutdown and cleanup
}