Homework 0 forum

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

 
Picture of Francesco Intoci
Purpose of ready chan in Run method of Gossiper in gossiper.go
by Francesco Intoci - Saturday, 19 September 2020, 13:12
 

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

Picture of Cédric Hölzl
Re: Purpose of ready chan in Run method of Gossiper in gossiper.go
by Cédric Hölzl - Saturday, 19 September 2020, 13:35
 

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

Hope it helps :)

Picture of Morten Borup Petersen
Re: Purpose of ready chan in Run method of Gossiper in gossiper.go
by Morten Borup Petersen - Saturday, 19 September 2020, 13:40
 

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.

Picture of Francesco Intoci
Re: Purpose of ready chan in Run method of Gossiper in gossiper.go
by Francesco Intoci - Saturday, 19 September 2020, 14:28
 

Got it, thanks!