I'm trying to set up an HTTP and HTTPS server on a Go application using http.server with default http.ServeMux. Specifically, I want one listener to handle HTTP traffic and the other to handle HTTPS traffic. However, I'm concerned about the safety and performance implications of running both listeners on the same server.
Is it safe and advisable to use multiple listeners in this way? Are there any potential issues with the default http.ServeMux when handling both HTTP and HTTPS requests simultaneously?
I tried creating separate listeners for HTTP and HTTPS.
server := &http.server{
...
} // http server
httpListener, err := net.Listen("tcp", ":8080") // Http listener
httpsListener, err := net.Listen("tcp", ":8081") // Https listener
wg := &sync.WaitGroup{}
wg.Add(2)
// Start the http and https server via different go-routines
go func(){
defer wg.Done()
server.ServerTLS(httpsListener, certFile, keyFile)
}
go func(){
defer wg.Done()
server.Serve(httpListener)
}
wg.Wait()
I expected it to work, but I'm uncertain whether this setup is safe or if it might cause any issues with concurrency, security, or the mux itself.
I had to write a microservice at work issuing the API on a separate port and the prometheus metrics on a different port. I did exactly as you did. It's been running for five years now. So far I have not noticed any security problems.
There is one thing I didn't consider five years ago. If kubernetes closes POD then I should sweep before closing. And I'm not doing that. As a suggestion I would suggest looking at “Respond to Ctrl+C interrupt signals gracefully” by Mat Ryer.