blob: 90301943831dd7b239f93e102d4f93dfe202e3d4 [file] [log] [blame] [view]
Andrew Gerrand5bc444d2014-12-10 11:35:11 +11001# Timeouts and Deadlines
2
3To abandon synchronous calls that run too long, use the select statement with time.After:
Arsham Shirvanib6e07a32018-03-20 16:40:09 +00004```go
Andrew Gerrand5bc444d2014-12-10 11:35:11 +11005import "time"
6
7c := make(chan error, 1)
8go func() { c <- client.Call("Service.Method", args, &reply) } ()
9select {
10 case err := <-c:
11 // use err and reply
12 case <-time.After(timeoutNanoseconds):
13 // call timed out
14}
15```
16
17Note that the channel ` c ` has a buffer size of 1. If it were an unbuffered channel and the client.Call method took more than ` timeoutNanoseconds `, the channel send would block forever and the goroutine would never be destroyed.
18
19## References
20
21time.After: http://golang.org/pkg/time/#After
22
23select: http://golang.org/doc/go_spec.html#Select_statements
24
25blog post: http://blog.golang.org/2010/09/go-concurrency-patterns-timing-out-and.html