avoid nil pointer exception when failing to get listener

instead of an opaque NPE on the next line, panic with explicit error
telling you what went wrong (in my case it was too many open files)
This commit is contained in:
Matthew Jaffee 2021-05-04 15:17:41 -05:00
parent 7d1f9e33b8
commit 8274a8cfee

View file

@ -30,6 +30,7 @@ import (
"github.com/pilosa/pilosa/v2/testhook"
"github.com/pilosa/pilosa/v2/topology"
. "github.com/pilosa/pilosa/v2/vprint" // nolint:staticcheck
"github.com/pkg/errors"
)
// GlobalPortMap avoids many races and port conflicts when setting
@ -53,7 +54,10 @@ func NewGlobalPortMapper(n int) (pm *GlobalPortMapper) {
availPorts: make(map[int]net.Listener),
}
for i := 0; i < n; i++ {
lsn, _ := net.Listen("tcp", ":0")
lsn, err := net.Listen("tcp", ":0")
if err != nil {
panic(errors.Wrap(err, "trying to listen on ephemeral port"))
}
r := lsn.Addr()
port := r.(*net.TCPAddr).Port
pm.availPorts[port] = lsn