Purpose of ready chan in Run method of Gossiper in gossiper.go

Purpose of ready chan in Run method of Gossiper in gossiper.go

by Francesco Intoci -
Number of replies: 3

Hello,

I couldn't manage to figure out what is the purpose of the ready chan. Even the description is a bit confusing for me, especially when it states "The ready chan must be closed when it is running."

Regards

In reply to Francesco Intoci

Re: Purpose of ready chan in Run method of Gossiper in gossiper.go

by Cédric Hölzl -

https://tour.golang.org/concurrency/1

Hope it helps :)

In reply to Francesco Intoci

Re: Purpose of ready chan in Run method of Gossiper in gossiper.go

by Morten Borup Petersen -

The ready channel is an unbuffered channel - the implication of this is that when a value is sent to the channel, the channel will block until another goroutine reads from the channel. Hence in this case, the ready channel is used as a method to synchronize separate goroutines.

The ready channel is used to prevent the controller to start until your gossiper is ready to handle requests (in main.go, `<-ready`). When closing the ready channel (from your gossiper), this will cause the `<-ready` call to return with an error (stating that the channel was closed). However, in this case, we do not care about any possible value returned on the channel nor the error; the pattern is simply used as a method for synchronizing the two separate goroutines which execute the controller and gossiper.