Skip to content

Commit d91fd92

Browse files
doc: context utility documentation
1 parent ecfae1a commit d91fd92

1 file changed

Lines changed: 34 additions & 0 deletions

File tree

docs/internals/utils/context.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Context Utility
2+
3+
## Introduction
4+
Context Utility provides functions to deal with context creation, callbacks.
5+
6+
## Structure
7+
8+
```go
9+
// DefaultTerminateSignals default signals to handle if no signals are provided
10+
DefaultTerminateSignals = []os.Signal{
11+
syscall.SIGTERM, syscall.SIGHUP, syscall.SIGINT,
12+
syscall.SIGQUIT,
13+
}
14+
```
15+
16+
```go
17+
// WithSignal return a context that is canceled if any of the specified signals was received
18+
// if no signals are passed, default to DefaultTerminateSignals
19+
func WithSignal(ctx context.Context, sig ...os.Signal) (context.Context, context.CancelFunc)
20+
21+
22+
// OnDone registers a callback on a context when it's done
23+
// The ctx.Err() is passed as is to the callback function
24+
func OnDone(ctx context.Context, cb func(error))
25+
```
26+
27+
## Usage
28+
29+
```go
30+
ctx, _ := utils.WithSignal(context.Background())
31+
utils.OnDone(ctx, func(_ error) {
32+
log.Info().Msg("shutting down")
33+
})
34+
```

0 commit comments

Comments
 (0)