Home > Backend Development > Golang > How Can I Gracefully Stop a Go Listening Server Without Spurious Errors?

How Can I Gracefully Stop a Go Listening Server Without Spurious Errors?

Linda Hamilton
Release: 2024-12-12 18:59:10
Original
844 people have browsed it

How Can I Gracefully Stop a Go Listening Server Without Spurious Errors?

Gracefully Stopping a Listening Server in Go

Background

Go's net package provides the ability to create listening servers. However, gracefully stopping these servers can be challenging due to the blocking nature of the listen.Accept method.

Existing Approach

One common approach to stopping a server is to close the listening socket. However, this can result in errors that cannot be distinguished from genuine errors during connection acceptance. As a result, the following code snippet exhibits this issue:

// ...

// Listen for incoming connections
func (es *EchoServer) serve() {
  for {
    conn, err := es.listen.Accept()
    // FIXME: I'd like to detect "use of closed network connection" here
    // FIXME: but it isn't exported from net
    if err != nil {
      log.Printf("Accept failed: %v", err)
      break
    }
    // ...
  }
  // ...
}
Copy after login

Enhanced Approach

To address this limitation, we can utilize the es.done channel to send a signal before the server is stopped. Here's how the code can be improved:

// ...

// Listen for incoming connections
func (es *EchoServer) serve() {
  for {
    conn, err := es.listen.Accept()
    if err != nil {
      select {
      case <-es.done:
        // Server is being stopped, so we can exit without logging the error.
      default:
        log.Printf("Accept failed: %v", err)
      }
      return
    }
    // ...
  }
}

// Stop the server by closing the listening socket
func (es *EchoServer) stop() {
  es.done <- true
  es.listen.Close()
}
Copy after login

By sending a value to the es.done channel before closing the listening socket, the serve function can determine when the server is being stopped and gracefully exit without logging spurious errors.

The above is the detailed content of How Can I Gracefully Stop a Go Listening Server Without Spurious Errors?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template