Update context example
This commit is contained in:
parent
77d0dffbbf
commit
d4dbfa776e
2 changed files with 5 additions and 1 deletions
|
@ -35,7 +35,7 @@ func doSlowJob(ctx context.Context) error {
|
|||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return ctx.Err()
|
||||
return context.Cause(ctx)
|
||||
default:
|
||||
// perform a portion of slow job
|
||||
time.Sleep(1 * time.Second)
|
||||
|
|
|
@ -372,6 +372,10 @@ Concurrency with Shared Memory
|
|||
ctx, cancel := context.WithCancel(ctx)
|
||||
defer cancel()
|
||||
|
||||
// manual cancel with explicit cause
|
||||
ctx, cancel := context.WithCancelCause(ctx)
|
||||
defer cancel(fmt.Errorf("job not needed"))
|
||||
|
||||
// cancel by timeout
|
||||
ctx, cancel := context.WithTimeout(ctx, 100*time.Millisecond)
|
||||
defer cancel()
|
||||
|
|
Loading…
Reference in a new issue