get notified when http.Server starts listening

前端 未结 1 1906
不知归路
不知归路 2020-12-21 11:50

When I look at the net/http server interface, I don\'t see an obvious way to get notified and react when the http.Server comes up and starts listening:

相关标签:
1条回答
  • 2020-12-21 12:33

    ListenAndServe is a helper function that opens a listening socket and then serves connections on that socket. Write the code directly in your application to signal when the socket is open:

    l, err := net.Listen("tcp", ":8080")
    if err != nil {
        // handle error
    }
    
    // Signal that server is open for business. 
    
    if err := http.Serve(l, rootHandler); err != nil {
        // handle error
    }
    

    If the signalling step does not block, then http.Serve will easily consume any backlog on the listening socket.

    Related question: https://stackoverflow.com/a/32742904/5728991

    0 讨论(0)
提交回复
热议问题