Google I/O 2013 - Advanced Go Concurrency Patterns

Google for Developers · 245K views · 2013 · 34:11

Open on YouTube

C, C++, Rust, Go & systems languagesSoftware architecture & designtalk

Why Stack Overflow recommends it

Stack Overflow users link it when answering questions about goroutines, channels, cleanup, and when to use channels instead of mutexes.

Recommended by 7 people across 7 different months, 2013 to 2024.

What it is

Sameer Ajmani's Google I/O 2013 talk follows up the earlier Go Concurrency Patterns session. It digs into Go's concurrency primitives and shows how tricky problems such as ping-pong goroutines, channel fan-in, timeouts and cancellation can be solved with short, clear Go code.

What people said

Recommends Dustin · 2013-06 · Stack Overflow · on “How to do nothing when no channel is ready to be read?”

sleep is almost always a bad idea as well. You have a ticker spitting stuff into the channel and requests for work. You can always wait until there's work and keep a really tight schedule. This talk describes a bit of why and what to do about it: [this video]

Recommends Not_a_Golfer · 2014-01 · Stack Overflow · on “What difficulties should I expect if I write a NoSQL db using golang but want to run Hadoop mapreduce on it?”

Ok, I'm not much of a Hadoop user so I'll give you some more general lessons learned about the issues you'll face: Protocol. If you're going with REST Go will be fine, but expect to find some gotchas in the default HTTP library's defaults (not expiring idle keepalive connections, not necessarily knowing when a reader has closed a stream). But if you want something more compact, know that: a. the Thrift implementation for Go, last I checked, was lacking and relatively slow. b. Go has great support for RPC but it might not play well with other languages. So you might want to check out protobuf, or work on top the redis protocol or something like that. GC. Go's GC is very simplistic (STW, not g

Recommends Not_a_Golfer · 2014-01 · Stack Overflow · on “Shared resources with channels in google go”

@Martol1ni I recommend watching this talk to get a better perspective about channels and concurrency patterns [this video]

Recommends Simon Whitehead · 2014-08 · Stack Overflow · on “How to test Golang channels / go-routines”

Just as an aside - I much prefer the channel fanning as described in this video: [this video] to relying on other synchronization mechanisms such as Mutexes. They are easier to reason about if you're coming from another language sure (it was the same for me coming from C#) - but the techniques described in that video make much more sense in the Go ecosystem. Just leaving it here as an alternative.

Recommends John Weldon · 2016-04 · Stack Overflow · on “What are best resources to understand concurrency in golang?”

The golang.org docs are great: [LINK] Specifically: Share Memory by Communicating [LINK] Go Concurrency Patterns [LINK] Advanced Go Concurrency Patterns [this video]

Recommends Felix · 2016-08 · Stack Overflow · on “Ensuring goroutine cleanup, bestpractice”

I have a fundamental understanding problem about how to make sure that spawned goroutines are "closed" properly in the context of long-running processes. I watched talks regarding that topic and read about best practices. In order to understand my question please refer to the video "Advanced Go Concurrency Patterns" here [this video] For the following, if you run code on your machine please export the environment variable GOTRACEBACK=all so you are able to see routine states after panic. I put the code for the original example here: naive [LINK] (it does not execute on go playground, I guess bacause a time statement is used. Please copy the code and execute it locally) The result of the panic of

Recommends MichalG · 2018-07 · Stack Overflow · on “What does mean when create channel like "reloadCh chan chan error"”

chan chan error is a channel of channels of errors. One of usual applications of using channels of channels is getting a response or error to a request (this is in fact the case in prometheus exporters you were looking at). The reason for using channel of channels is that it allows you to choose where you want to have your answer communicated to (in contrary to using the same channel for sending request and receiving response). An example: package main import ( "errors" "fmt" "math/rand" "time" ) func main() { requestChan := make(chan chan error) // Starting the service goroutine go goroutine(requestChan) // Send 5 requests and collect errors for i := 0; i < 5; i++ { fmt.Printf("Request %v\n

Recommends Kolom · 2023-08 · Stack Overflow · on “Run scheduled tasks within a web application”

@Jessie Thanks for the explanation. After reading your comment, I came across a talk on youtube spontaneously just mentioning this very question. It's along the lines of how you explained it. But if you are curious here is the link. It's around time 14:13 - [this video]

Cites softshipper · 2014-09 · Stack Overflow · on “Get deadlock when try to received value twice”

I watch the awesome video about Advanced Go Concurrency Patterns [this video] . At the beginning Sameer Ajmani shows a ping pong application. package main import ( "fmt" "time" ) type Ball struct{ hits int } func main() { table := make(chan *Ball) go player("ping", table) go player("pong", table) table <- new(Ball) // game on; toss the ball time.Sleep(1 * time.Second) fmt.Println(<-table) // game over; grab the ball } func player(name string, table chan *Ball) { for { ball := <-table ball.hits++ fmt.Println(name, ball.hits) time.Sleep(100 * time.Millisecond) table <- ball } } The code how it works, I understand to 90 percent. They are two goroutines they send to each other messages, ping and pong

Cites muiiu · 2017-11 · Stack Overflow · on “When should you use a mutex over a channel?”

For the past few weeks I've been wrestling with one (not-so) simple question: When is it best to use sync.Mutex and, conversely, when is it best use a chan ? It seems that for a lot of problems either strategy is interchangeable with the other - and that's just the problem! Take this video [this video] found in the Golang documentation. Below, I've taken the liberty to dictate the code in the playground and also translate it to a sync.Mutex equivalent. Is there a certain problem - encountered in the real world - that warrants the use of one other? Notes: I am a huge fan of this use of chan [LINK] and struggle to think of a more elegant implementation using sync.Mutex . It's worth noting that the