I'm setting up a golang grpc server that will use TLS for client authorization/authentication. All the examples of server setup that I've seen use net.Listen()
instead of tls.Listen()
. Why is this?
import (
"crypto/tls"
"github.com/pkg/errors"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"net"
)
func startGRPCListener(hostport string, tlsconfig *tls.Config) (grpcServer *grpc.Server) {
listener, _ = net.Listen("tcp", hostport)
cred := grpc.Creds(credentials.NewTLS(tlsconfig))
grpcServer := grpc.NewServer(cred)
go func() {
serveErr := grpcServer.Serve(listener)
log.WithError(serveErr).Info("GRPC server exited")
}()
return
}
I've noticed in a couple of working examples if I swap to tls.Listen, the connection can no longer be made. However, in other cases, this doesn't cause a problem!
I guess my question is "should I use net.Listen or tls.Listen, or does it not make a difference?"