I have a code segment that is reading from a TCP connection and after the first few connections, the server is outputing broken pipe however no error is occurring in my go code. The server sending the messages is at its core the coda hale metrics library, more specifically the PickledGraphite class.
here is the Go code that is reading:
func handleConn(conn net.Conn, id int) {
fmt.Println("handleConn")
defer conn.Close()
buf := make([]byte, 0, 10240)
tmp := make([]byte, 256)
fmt.Printf("%v Reading...
", id)
for {
n, err := conn.Read(tmp)
fmt.Printf("%v Read %v
", id, n)
if err != nil {
fmt.Printf("%v Got err: %v
", id, err)
if err != io.EOF {
fmt.Printf("%v read error: %v
", id, err)
}
buf = append(buf, tmp[:n]...)
break
}
buf = append(buf, tmp[:n]...)
}
fmt.Printf("%v Done Reading
", id)
// Do stuff with buf
}
func main() {
ln, err := net.Listen("tcp", ":5555")
if err != nil {
fmt.Println(err)
os.Exit(-1)
}
id := 1
for {
fmt.Println("getting connection
")
conn, err := ln.Accept()
if err != nil {
fmt.Println(err)
break
}
conn.SetReadDeadline(time.Now().Add(20 * time.Second))
fmt.Println("Got connection")
go handleConn(conn, id)
id = id + 1
fmt.Println("sent handleConn
")
}
}
My code is still running, I can still execute nc
commands and see my code receive it, so I am not sure how I am losing the connection. If I remove the conn.SetReadDeadline()
line then what happens is my code no longer receives a EOF after the first message. Thanks in advance