In the following snippet for having a function representing work send a 
heartbeat to any listeners.

Why is a select statement used in `sendPulse` instead of just executing 
`heartbeat <- struct{}{}`?
And why provide an empty default case?


doWork := func(
done <-chan interface{},
pulseInterval time.Duration,
) (<-chan interface{}, <-chan time.Time) {
heartbeat := make(chan interface{})
results := make(chan time.Time)
go func() {
defer close(heartbeat)
defer close(results)

pulse := time.Tick(pulseInterval)
workGen := time.Tick(2 * pulseInterval)

sendPulse := func() {
select {         // why have a select statement?
case heartbeat <- struct{}{}:
default:         // why have the default case?
}
}
sendResult := func(r time.Time) {
for {
select {
case <-done:
return
case <-pulse:
sendPulse()
case results <- r:
return
}
}
}

for {
select {
case <-done:
return
case <-pulse:
sendPulse()
case r := <-workGen:
sendResult(r)
}
}
}()
return heartbeat, results
}


-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to