0

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...\n", id)
    for {
        n, err := conn.Read(tmp)
        fmt.Printf("%v Read %v\n", id, n)
        if err != nil {
            fmt.Printf("%v Got err: %v\n", id, err)
            if err != io.EOF {
                fmt.Printf("%v read error: %v\n", id, err)
            }
            buf = append(buf, tmp[:n]...)
            break
        }
        buf = append(buf, tmp[:n]...)
    }
    fmt.Printf("%v Done Reading\n", 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\n")
        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\n")
    }
}

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

Community
  • 1
  • 1
Jamil Seaidoun
  • 949
  • 1
  • 11
  • 24
  • make sure you known what `SetReadDeadline` do. – Jiang YD Aug 17 '16 at 02:28
  • @ jamil-seaidoun I don't see any error, even with `SetReadDeadline` commented. what is your go version? and show your minimum client sample code. –  Aug 17 '16 at 04:39
  • @Amd The client code is the druid graphite emitter found here: https://github.com/druid-io/druid/tree/master/extensions-contrib/graphite-emitter. At it's core it's using the coda hale metrics library mentioned in the post above. – Jamil Seaidoun Aug 17 '16 at 04:49

0 Answers0