How can i interrupt a goroutine executing (*TCPListener) Accept?

Here is what i was looking for. Maybe helps someone in the future. Notice the use of select and the "c" channel to combine it with the exit channel

    ln, err := net.Listen("tcp", ":8080")
    if err != nil {
        // handle error
    }
    defer ln.Close()
    for {
        type accepted struct {
            conn net.Conn
            err  error
        }
        c := make(chan accepted, 1)
        go func() {
            conn, err := ln.Accept()
            c <- accepted{conn, err}
        }()
        select {
        case a := <-c:
            if a.err != nil {
                // handle error
                continue
            }
            go handleConnection(a.conn)
        case e := <-ev:
            // handle event
            return
        }
    }

TCPListener Deadline

You don't necessarily need an extra go routine (that keeps accepting), simply specify a Deadline.

for example:

for {

    // Check if someone wants to interrupt accepting
    select {
    case <- someoneWantsToEndMe: 
        return // runs into "defer listener.Close()"
    default: // nothing to do
    }

    // Accept with Deadline
    listener.SetDeadline(time.Now().Add(1 * time.Second)
    conn, err := listener.Accept()
    if err != nil {
        // TODO: Could do some err checking (to be sure it is a timeout), but for brevity
        continue
    }

    go handleConnection(conn)
}

Simply Close() the net.Listener you get from the net.Listen(...) call and return from the executing goroutine.

Tags:

Go