diff options
author | Francisco Souza <franciscossouza@gmail.com> | 2012-03-14 13:03:11 +1100 |
---|---|---|
committer | Francisco Souza <franciscossouza@gmail.com> | 2012-03-14 13:03:11 +1100 |
commit | 3e6c856dab8ec5b27089d226205799c1d44da7cc (patch) | |
tree | c50839531d637db8a0e0a7e0a4a928702bf32cc2 /doc/progs | |
parent | 8d937cffa9645f184654f82ec9f55a4af3928602 (diff) | |
download | go-3e6c856dab8ec5b27089d226205799c1d44da7cc.tar.gz |
doc: add Go Concurrency Patterns: Timing out, moving on article
Originally published on The Go Programming Language Blog, September 23, 2010.
http://blog.golang.org/2010/09/go-concurrency-patterns-timing-out-and.html
Update issue 2547.
R=golang-dev, adg
CC=golang-dev
http://codereview.appspot.com/5815044
Committer: Andrew Gerrand <adg@golang.org>
Diffstat (limited to 'doc/progs')
-rwxr-xr-x | doc/progs/run | 7 | ||||
-rw-r--r-- | doc/progs/timeout1.go | 28 | ||||
-rw-r--r-- | doc/progs/timeout2.go | 27 |
3 files changed, 61 insertions, 1 deletions
diff --git a/doc/progs/run b/doc/progs/run index 4d183530c..3bd50beda 100755 --- a/doc/progs/run +++ b/doc/progs/run @@ -41,7 +41,12 @@ if [ "$goos" == "freebsd" ]; then c_go_cgo="cgo3 cgo4" fi -all=$(echo $defer_panic_recover $effective_go $error_handling $law_of_reflection $c_go_cgo slices go1) +timeout=" + timeout1 + timeout2 +" + +all=$(echo $defer_panic_recover $effective_go $error_handling $law_of_reflection $c_go_cgo $timeout slices go1) for i in $all; do go build $i.go diff --git a/doc/progs/timeout1.go b/doc/progs/timeout1.go new file mode 100644 index 000000000..a6c95624c --- /dev/null +++ b/doc/progs/timeout1.go @@ -0,0 +1,28 @@ +// Copyright 2012 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. +package timeout + +import ( + "time" +) + +func Timeout() { + ch := make(chan bool, 1) + timeout := make(chan bool, 1) + go func() { + time.Sleep(1e9) // one second + timeout <- true + }() + + // STOP OMIT + + select { + case <-ch: + // a read from ch has occurred + case <-timeout: + // the read from ch has timed out + } + + // STOP OMIT +} diff --git a/doc/progs/timeout2.go b/doc/progs/timeout2.go new file mode 100644 index 000000000..7145bc93e --- /dev/null +++ b/doc/progs/timeout2.go @@ -0,0 +1,27 @@ +// Copyright 2012 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. +package query + +type Conn string + +func (c Conn) DoQuery(query string) Result { + return Result("result") +} + +type Result string + +func Query(conns []Conn, query string) Result { + ch := make(chan Result, 1) + for _, conn := range conns { + go func(c Conn) { + select { + case ch <- c.DoQuery(query): + default: + } + }(conn) + } + return <-ch +} + +// STOP OMIT |