Last active
December 15, 2015 12:48
-
-
Save jgrahamc/5262578 to your computer and use it in GitHub Desktop.
The code associated with this talk: http://www.slideshare.net/jgrahamc/go-oncurrency
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
func worker(die chan bool) { | |
for { | |
select { | |
// ... do stuff cases | |
case <- die: | |
return | |
} | |
} | |
} | |
func main() { | |
die := make(chan bool) | |
for i := 0; i < 100; i++ { | |
go worker(die) | |
} | |
close(die) | |
} |
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
func worker(die chan bool) { | |
for { | |
select { | |
// ... do stuff cases | |
case <- die: | |
// ... do termination tasks | |
die <- true | |
return | |
} | |
} | |
} | |
func main() { | |
die := make(chan bool) | |
go worker(die) | |
die <- true | |
<- die | |
} |
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
id := make(chan string) | |
go func() { | |
var counter int64 = 0 | |
for { | |
id <- fmt.Sprintf("%x", counter) | |
counter += 1 | |
} | |
}() | |
x := <- id // x will be 1 | |
x = <- id // x will be 2 |
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
func recycler(give, get chan []byte) { | |
q := new(list.List) | |
for { | |
if q.Len() == 0 { | |
q.PushFront(make([]byte, 100)) | |
} | |
e := q.Front() | |
select { | |
case s := <-give: | |
q.PushFront(s[:0]) | |
case get <- e.Value.([]byte): | |
q.Remove(e) | |
} | |
} | |
} |
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
func worker(start chan bool) { | |
for { | |
timeout := time.After(30 * time.Second) | |
select { | |
// ... do some stuff | |
case <- timeout: | |
return | |
} | |
} | |
} | |
func worker(start chan bool) { | |
timeout := time.After(30 * time.Second) | |
for { | |
select { | |
// ... do some stuff | |
case <- timeout: | |
return | |
} | |
} | |
} |
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
func worker(start chan bool) { | |
heartbeat := time.Tick(30 * time.Second) | |
for { | |
select { | |
// ... do some stuff | |
case <- heartbeat: | |
// ... do heartbeat stuff | |
} | |
} | |
} |
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
func worker(messages chan string) { | |
for { | |
var msg string // ... generate a message | |
messages <- msg | |
} | |
} | |
func main() { | |
messages := make(chan string) | |
conn, _ := net.Dial("tcp", "example.com") | |
for i := 0; i < 100; i++ { | |
go worker(messages) | |
} | |
for { | |
msg := <- messages | |
conn.Write([]byte(msg)) | |
} | |
} |
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
type response struct { | |
resp *http.Response | |
url string | |
} | |
func get(url string, r chan response ) { | |
if resp, err := http.Get(url); err == nil { | |
r <- response{resp, url} | |
} | |
} | |
func main() { | |
first := make(chan response) | |
for _, url := range []string{"http://code.jquery.com/jquery-1.9.1.min.js", | |
"http://cdnjs.cloudflare.com/ajax/libs/jquery/1.9.1/jquery.min.js", | |
"http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js", | |
"http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.9.1.min.js"} { | |
go get(url, first) | |
} | |
r := <- first | |
// ... do something | |
} |
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
func generator(strings chan string) { | |
strings <- "Five hour's New York jet lag" | |
strings <- "and Cayce Pollard wakes in Camden Town" | |
strings <- "to the dire and ever-decreasing circles" | |
strings <- "of disrupted circadian rhythm." | |
close(strings) | |
} | |
func main() { | |
strings := make(chan string) | |
go generator(strings) | |
for s := range strings { | |
fmt.Printf("%s ", s) | |
} | |
fmt.Printf("\n"); | |
} |
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
type work struct { | |
url string | |
resp chan *http.Response | |
} | |
func getter(w chan work) { | |
for { | |
do := <- w | |
resp, _ := http.Get(do.url) | |
do.resp <- resp | |
} | |
} | |
func main() { | |
w := make(chan work) | |
go getter(w) | |
resp := make(chan *http.Response) | |
w <- work{"http://cdnjs.cloudflare.com/jquery/1.9.1/jquery.min.js", | |
resp} | |
r := <- resp | |
} |
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" | |
"net/http" | |
"runtime" | |
) | |
type job struct { | |
url string | |
resp chan *http.Response | |
} | |
type worker struct { | |
jobs chan *job | |
count int | |
} | |
func (w *worker) getter(done chan *worker) { | |
for { | |
j := <- w.jobs | |
resp, _ := http.Get(j.url) | |
j.resp <- resp | |
done <- w | |
} | |
} | |
func balancer(count int, depth int) chan *job { | |
jobs := make(chan *job) | |
done := make(chan *worker) | |
workers := make([]*worker, count) | |
for i := 0; i < count; i++ { | |
workers[i] = &worker{make(chan *job, depth), 0} | |
go workers[i].getter(done) | |
} | |
go func() { | |
for { | |
var free *worker | |
min := depth | |
for _, w := range workers { | |
if w.count < min { | |
free = w | |
min = w.count | |
} | |
} | |
var jobsource chan *job | |
if free != nil { | |
jobsource = jobs | |
} | |
select { | |
case j := <- jobsource: | |
free.jobs <- j | |
free.count++ | |
case w := <- done: | |
w.count-- | |
} | |
} | |
}() | |
return jobs | |
} | |
func get(jobs chan *job, url string, answer chan string) { | |
resp := make(chan *http.Response) | |
jobs <- &job{url, resp} | |
r := <- resp | |
if r != nil { | |
answer <- r.Request.URL.String() | |
} else { | |
answer <- url | |
} | |
} | |
func main() { | |
runtime.GOMAXPROCS(runtime.NumCPU()) | |
jobs := balancer(10, 10) | |
answer := make(chan string) | |
for { | |
var url string | |
if _, err := fmt.Scanln(&url); err != nil { | |
break | |
} | |
go get(jobs, url, answer) | |
} | |
for u := range answer { | |
fmt.Printf("%s\n", u) | |
} | |
} |
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
func from(connection chan int) { | |
connection <- rand.Intn(100) | |
} | |
func to(connection chan int) { | |
i := <- connection | |
fmt.Printf("Someone sent me %d\n", i) | |
} | |
func main() { | |
cpus := runtime.NumCPU() | |
runtime.GOMAXPROCS(cpus) | |
connection := make(chan int) | |
go from(connection) | |
go to(connection) | |
} |
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
c := make(chan bool) | |
go func() { | |
// ... do some stuff | |
close(c) | |
}() | |
// ... do some other stuff | |
<- c |
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
func worker(start chan bool) { | |
<- start | |
// ... do stuff | |
} | |
func main() { | |
start := make(chan bool) | |
for i := 0; i < 100; i++ { | |
go worker(start) | |
} | |
close(start) | |
// ... all workers running now | |
} |
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
select { | |
case x := <- somechan: | |
// ... do stuff with x | |
case y, ok := <- someOtherchan: | |
// ... do stuff with y | |
// check ok to see if someOtherChan | |
// is closed | |
case outputChan <- z: | |
// ... ok z was sent | |
default: | |
// ... no one wants to communicate | |
} |
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
for { | |
select { | |
case x := <- somechan: | |
// ... do stuff with x | |
case y, ok := <- someOtherchan: | |
// ... do stuff with y | |
// check ok to see if someOtherChan | |
// is closed | |
case outputChan <- z: | |
// ... ok z was sent | |
default: | |
// ... no one wants to communicate | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment