Stop FIFO reader after terminal errors
This commit is contained in:
committed by
Brandon McGinty
parent
6aeab4fd5c
commit
41759b95f6
@@ -88,18 +88,27 @@ func setup_fifo(fn string) (chan string, error) {
|
||||
if err != nil {
|
||||
return t, err
|
||||
}
|
||||
go func(fh io.Reader, out chan string) {
|
||||
reader := bufio.NewReader(fh)
|
||||
for {
|
||||
line, err := reader.ReadBytes('\n')
|
||||
if err == nil {
|
||||
out <- strings.TrimSpace(string(line))
|
||||
}
|
||||
}
|
||||
}(file, t)
|
||||
go readFIFO(file, t)
|
||||
return t, nil
|
||||
}
|
||||
|
||||
// readFIFO forwards complete commands and terminates on EOF or any read
|
||||
// failure. Retrying an unrecoverable FIFO error used to spin a CPU forever.
|
||||
func readFIFO(fh io.ReadCloser, out chan<- string) {
|
||||
defer fh.Close()
|
||||
defer close(out)
|
||||
reader := bufio.NewReader(fh)
|
||||
for {
|
||||
line, err := reader.ReadBytes('\n')
|
||||
if len(line) != 0 {
|
||||
out <- strings.TrimSpace(string(line))
|
||||
}
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
// Command line flags
|
||||
server := flag.String("server", "localhost:64738", "the server to connect to")
|
||||
|
||||
Reference in New Issue
Block a user