Created
July 22, 2023 03:11
-
-
Save ostretsov/8bce80d75c5123edb04bd9c5be0912b8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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