func router(r *nsq.Reader, f *FileLogger, termChan chan os.Signal, hupChan chan os.Signal) { pos := 0 output := make([]*Message, *maxInFlight) sync := false ticker := time.NewTicker(time.Duration(30) * time.Second) closing := false for { select { case <-termChan: ticker.Stop() r.Stop() // ensures that we keep flushing whatever is left in the channels closing = true case <-hupChan: f.Close() f.updateFile() sync = true case <-ticker.C: f.updateFile() sync = true case m := <-f.logChan: if f.updateFile() { sync = true } _, err := f.Write(m.Body) if err != nil { log.Fatalf("ERROR: writing message to disk - %s", err.Error()) } _, err = f.Write([]byte("\n")) if err != nil { log.Fatalf("ERROR: writing newline to disk - %s", err.Error()) } output[pos] = m pos++ if pos == *maxInFlight { sync = true } } if closing || sync || r.IsStarved() { if pos > 0 { log.Printf("syncing %d records to disk", pos) err := f.Sync() if err != nil { log.Fatalf("ERROR: failed syncing messages - %s", err.Error()) } for pos > 0 { pos-- m := output[pos] m.returnChannel <- &nsq.FinishedMessage{m.Id, 0, true} output[pos] = nil } } sync = false } } }
func (f *FileLogger) router(r *nsq.Reader, termChan chan os.Signal, hupChan chan os.Signal) { pos := 0 output := make([]*Message, *maxInFlight) sync := false ticker := time.NewTicker(time.Duration(30) * time.Second) closing := false closeFile := false exit := false for { select { case <-r.ExitChan: sync = true closeFile = true exit = true case <-termChan: ticker.Stop() r.Stop() sync = true closing = true case <-hupChan: sync = true closeFile = true case <-ticker.C: if f.needsFileRotate() { if *skipEmptyFiles { closeFile = true } else { f.updateFile() } } sync = true case m := <-f.logChan: if f.updateFile() { sync = true } _, err := f.Write(m.Body) if err != nil { log.Fatalf("ERROR: writing message to disk - %s", err.Error()) } _, err = f.Write([]byte("\n")) if err != nil { log.Fatalf("ERROR: writing newline to disk - %s", err.Error()) } output[pos] = m pos++ if pos == *maxInFlight { sync = true } } if closing || sync || r.IsStarved() { if pos > 0 { log.Printf("syncing %d records to disk", pos) err := f.Sync() if err != nil { log.Fatalf("ERROR: failed syncing messages - %s", err.Error()) } for pos > 0 { pos-- m := output[pos] m.returnChannel <- &nsq.FinishedMessage{m.Id, 0, true} output[pos] = nil } } sync = false } if closeFile { f.Close() closeFile = false } if exit { close(f.ExitChan) break } } }