I have a go program that opens a large collection of named pipes at their read end. It has to read data from all of these, do some internal processing, and output a single stream of data.
Since reading from (or opening) a named pipe blocks the thread until it's written to (or opened at the other end), I have a goroutine for each named pipe. The goroutines read lines from their corresponding named pipe, do a bit of buffering, and eventually send data on a shared channel.
The main goroutine reads data from this channel, processes it and outputs it until all the goroutines signal that their pipes have closed.
It's a reasonable design, but the code to set up this infrastructure is quite verbose and there's a compromise between:
- not wanting to block the main goroutine on any operations on a named pipe, and
- wanting to effectively handle errors found when trying to operate on these named pipes
Is there a different approach available? What I really want conceptually is a select loop, but with reads from an io.ReadCloser, instead of channel receives / sends. Is this possible at all, without having to have a goroutine handling the work on each io.ReadCloser, so that the main select loop can work with channels? Or is my current design the go way?
Thanks,