How do I stop a Listening server in Go

前端 未结 4 1989
长发绾君心
长发绾君心 2020-12-29 06:09

I\'ve been trying to find a way to stop a listening server in Go gracefully. Because listen.Accept blocks it is necessary to close the listening socket to sign

4条回答
  •  感情败类
    2020-12-29 06:53

    I would handle this by using es.done to send a signal before it closes the connection. In addition to the following code you'd need to create es.done with make(chan bool, 1) so that we can put a single value in it without blocking.

    // Listen for incoming connections
    func (es *EchoServer) serve() {
      for {
        conn, err := es.listen.Accept()
        if err != nil {
          select {
          case <-es.done:
            // If we called stop() then there will be a value in es.done, so
            // we'll get here and we can exit without showing the error.
          default:
            log.Printf("Accept failed: %v", err)
          }
          return
        }
        go es.respond(conn.(*net.TCPConn))
      }
    }
    
    // Stop the server by closing the listening listen
    func (es *EchoServer) stop() {
      es.done <- true   // We can advance past this because we gave it buffer of 1
      es.listen.Close() // Now it the Accept will have an error above
    }
    

提交回复
热议问题