-
-
Save cagodoy/42e7c6acfc84079caecb0e287686c08c to your computer and use it in GitHub Desktop.
context with timeout
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 ( | |
"context" | |
"errors" | |
"fmt" | |
"time" | |
) | |
func main() { | |
fmt.Println("testing time out") | |
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) | |
defer cancel() | |
fmt.Println("before") | |
go func() { | |
time.Sleep(10 * time.Second) | |
cancel() | |
}() | |
err := example(ctx) | |
fmt.Println("---------") | |
if err == context.Canceled { | |
fmt.Println("done: cancelled") | |
} else if err == context.DeadlineExceeded { | |
fmt.Println("done: deadline_exceeded") | |
} else if err != nil { | |
fmt.Println("done: error") | |
} else { | |
fmt.Println("done: ok") | |
} | |
} | |
func example(ctx context.Context) (err error) { | |
ticker := time.NewTicker(time.Second) | |
defer ticker.Stop() | |
// count := 0 | |
loop: | |
for count := 0; ; count++ { | |
select { | |
case <-ctx.Done(): | |
fmt.Println("FINISH in <-ctx.Done()") | |
err = ctx.Err() | |
break loop | |
case <-ticker.C: | |
fmt.Println("HERE IN TICKER (waiting 2 sec)") | |
// case finish: ok and error | |
if count == 3 { | |
fmt.Println("FINISH with count") | |
err = errors.New("finish with error") | |
break loop | |
} | |
} | |
} | |
fmt.Println("after the loop") | |
return err | |
} |
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
➜ go run main.go | |
testing time out | |
before | |
HERE IN TICKER (waiting 2 sec) | |
HERE IN TICKER (waiting 2 sec) | |
HERE IN TICKER (waiting 2 sec) | |
HERE IN TICKER (waiting 2 sec) | |
FINISH with count | |
after the loop | |
--------- | |
done: ok | |
++++++++++++++++++++++++++++++++++++++++ | |
➜ go run main.go | |
testing time out | |
before | |
HERE IN TICKER (waiting 2 sec) | |
HERE IN TICKER (waiting 2 sec) | |
HERE IN TICKER (waiting 2 sec) | |
HERE IN TICKER (waiting 2 sec) | |
HERE IN TICKER (waiting 2 sec) | |
FINISH in <-ctx.Done() | |
after the loop | |
--------- | |
done: deadline_exceeded | |
++++++++++++++++++++++++++++++++++++++++ | |
➜ go run main.go | |
testing time out | |
before | |
HERE IN TICKER (waiting 2 sec) | |
HERE IN TICKER (waiting 2 sec) | |
FINISH in <-ctx.Done() | |
after the loop | |
--------- | |
done: cancelled | |
+++++++++++++++++++++++++++++++++++++++++ | |
➜ go run main.go | |
testing time out | |
before | |
HERE IN TICKER (waiting 2 sec) | |
HERE IN TICKER (waiting 2 sec) | |
HERE IN TICKER (waiting 2 sec) | |
HERE IN TICKER (waiting 2 sec) | |
FINISH with count | |
after the loop | |
--------- | |
done: error |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment