Properly handling incoming records incrementally

rather than waiting for ioutil.ReadAll to work, which appeared to only
happen when the remote rsyslog disconnected.
This commit is contained in:
Dan Buch 2013-01-24 23:20:44 -05:00
parent fb3009574f
commit 4dc472e207

View File

@ -1,12 +1,13 @@
package sltcpsrv
import (
"bytes"
"bufio"
"flag"
"fmt"
"io/ioutil"
"io"
"log"
"net"
"strings"
)
var (
@ -35,27 +36,22 @@ func ListenAndServe(port int) {
func handleConnection(conn net.Conn) {
defer conn.Close()
inbytes, err := ioutil.ReadAll(conn)
connBuf := bufio.NewReader(conn)
for {
line, err := connBuf.ReadString('\n')
if err != nil {
log.Printf("Failed to read request body: %+v\n", err)
if err == io.EOF {
break
}
log.Printf("Failed to read line: %+v\n", err)
return
}
records := bytes.Split(inbytes, []byte("\n"))
nonempty := [][]byte{}
for _, rec := range records {
if len(rec) != 0 {
nonempty = append(nonempty, rec)
}
}
n := len(nonempty)
for i, rec := range nonempty {
msg := newMessage(rec)
msg := newMessage([]byte(strings.TrimSpace(line)))
if msg != nil {
fmt.Printf("%v [%d of %d]: %+v\n", conn.RemoteAddr(), i+1, n, msg)
fmt.Printf("%v: %+v\n", conn.RemoteAddr(), msg)
}
}
}