I am writing an app that allows users to upload files and queue them for processing. My approach to this is to create a goroutine for processing uploaded files and to use a channel to signal that a new file is ready for processing.
Basically, the processing goroutine does this:
for {
while itemForProcessing() {
processNextItem()
}
select {
case <-signalChan:
case <-stopChan:
return
}
}
The code for signaling that a new item is ready for processing looks something like this:
select {
case signalChan <- true:
default:
}
Note that this is a non-blocking send on the channel.
There are three possible scenarios:
select{}
block — the first case runs and the next item is processedprocessNextItem()
— the next item will be processed once processNextItem()
returns since the loop condition will be true
select{}
blockThe last scenario will result in a problem. The non-blocking send will not send anything on the channel and the goroutine will wait in the select{}
block until something else happens.
How can I avoid this problem? I cannot use a blocking send because the processing goroutine may be running processNextItem()
, which would cause the send to block for an extremely long period of time.
To avoid a lost signal, use a channel with capacity 1.