I can't find a design to avoid sending on a closed channel. Consider following scenario:
There are 3 go routines, producer, consumer, main. And a channel C.
"producer" sends new task to channel C
"consumer" receives a task from channel C, and processing it.
"main" is responsible for run cleanup when shutdown.
Following execution sequence may cause a "send on closed channel" panic.
1. "consumer" fetched a job from channel C then processing, the it is slow
1. "producer" trying to send, but blocked on C because there is no buffer available in channel C
2. service is going to shutdown, so "main" close the channel C
3. when the channel C is closed, the "producer" is waked up, and panic. If no recover() called, the job is lost too.
The code can be found here:
http://play.golang.org/p/MCQ2U33v2m
Do I have to wrap every send operation with a recover() to detect the panic?