Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ostretsov/8bce80d75c5123edb04bd9c5be0912b8 to your computer and use it in GitHub Desktop.
Save ostretsov/8bce80d75c5123edb04bd9c5be0912b8 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"time"
)
func Example_multipleReasonsForCancellation() {
ctx1, cancel1 := context.WithCancel(context.Background())
ctx2, cancel2 := context.WithTimeout(ctx1, 100*time.Millisecond)
ctx, cancel3 := context.WithDeadline(ctx2, time.Now().Add(70*time.Millisecond))
defer cancel3()
defer cancel2()
defer cancel1()
select {
case <-ctx.Done():
fmt.Println("ctx.Done()")
default:
fmt.Println("ctx is not done")
}
time.Sleep(80 * time.Millisecond)
select {
case <-ctx.Done():
fmt.Println("ctx.Done()")
default:
fmt.Println("ctx is not done")
}
// Output:
// ctx is not done
// ctx.Done()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment