为工人goroutine排队项目的正确方法?

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:

  • the processing goroutine is in the select{} block — the first case runs and the next item is processed
  • the processing goroutine is executing processNextItem() — the next item will be processed once processNextItem() returns since the loop condition will be true
  • the processing goroutine has exited the loop but has not yet entered the select{} block

The 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.