Best way to implement multiplexed web server in go

336 Views Asked by At

I know 2 ways of implementing a multiplexed web-server in golang:

  • cmux: the approach with this package is to start a server for every kind of connection you want to serve (HTTP, HTTPS, gRPC, etc.), and then an additional one, the cmux multiplexer (example).
  • built-in http.Server + router handler: this approach is completely different from the previous one, and basically consists of starting only 1 built-in http.Server with a handler that redirects the incoming requests to the correct server based on their header's content type. Example:
server := &http.Server{
   Addr: ":4444",
   Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
     if isHttpRequest(r) {
        httpServer.ServeHTTP(w, r)
        return
     }
     if isGrpcWebRequest(r) {
        grpcWebServer.ServeHTTP(w, r)
        return
     }
     grpcServer.ServeHTTP(w,r)
   }
}

Both approaches are valid, but I wonder what's the best one.

The second one seems to be preferable because:

  • it doesn't make use of external dependencies
  • it starts only one server instance with N "routes", instead of N+1 instances (where N is the total number of connections the server serves), therefore a lower number of long-running goroutines
  • less and cleaner code
0

There are 0 best solutions below