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