I want to start an HTTP server, and then once it's started, log a message
that the server has started. Here are the options for doing that:
- log ahead of time. The message gets logged before the server starts
listening on a socket, which means you might get the message and then an
error
log.Info("starting server on port", "port", 4567)
server.ListenAndServe()
- create a net.Listener, listen on a socket, log a message, start the
server.
ln, err := net.Listen("tcp", addr)
log.Info("started server", "port", 4567)
server.Serve(ln)
This is fine howeverrrr the code in ListenAndServe() does not do the same
thing as net.Listen, it inserts a tcpKeepAliveListener in the middle there.
To get the same behavior I have to recreate the tcpKeepAliveListener.
ln, err := net.Listen("tcp", addr)
if err != nil { return err }
return srv.Serve(tcpKeepAliveListener{ln.(*net.TCPListener)})
- Spin up a goroutine that tries to connect to the socket and then logs.
This is more verbose
for {
conn, err := net.Dial("tcp", ":"+port)
if err == nil {
defer conn.Close()
log.Info("Started server", "port", port)
return
}
time.Sleep(10 * time.Millisecond)
}
- Petition for a new http.Listen(net, addr) (net.Listener, error) API (or
httputil.Listen) that gives you back the tcpKeepAliveListener in that
package.
Thoughts? Has anyone else ran into or cared about this problem, or just me?
--
You received this message because you are subscribed to the Google Groups
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.