soheilhy / cmux

Connection multiplexer for GoLang: serve different services on the same port!
Apache License 2.0
2.53k stars 197 forks source link

Cannot check closed error with errors.Is #85

Closed ahmetb closed 3 years ago

ahmetb commented 3 years ago

I have a gRPC server behind cmux. In this server, I follow graceful shutdown practices.

So when the program receives a termination signal, I stop servers in this order:

    go func() {
        <-ctx.Done()
        grpcServer.GracefulStop()
        mux.Close()
    }()

However, my grpcServer.Serve(grpcListener) receives an error like:

mux: Server closed

This is not great, because I am trying to do this and I do not have a way of seemingly checking this error with errors.Is or with something like err == cmux.ErrServerClosed because no such type is defined.

    go func() {
        defer wg.Done()
        if err := grpcServer.Serve(grpcLis); err != nil && !errors.Is(err, http.ErrServerClosed) {
            log.Fatal("grpc: server failed", zap.Error(err))
        }
        log.Debug("grpc: server closed without error")
    }()

I know docs say users can just do go grpcServer.Serve(grpcListener) but I do not find that to be reliable enough.

ahmetb commented 3 years ago

My bad, gopls did not provide the right autocomplete results so I did not see ErrServerClosed exposed on cmux package.