From a4a83be2b928976a4ffae1d233f31ab99ad903ac Mon Sep 17 00:00:00 2001 From: Adrien Delorme Date: Tue, 26 Mar 2019 10:52:07 +0100 Subject: [PATCH] net.ListenRangeConfig.Listen: allow to have a port rage of zero --- common/net/configure_port.go | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/common/net/configure_port.go b/common/net/configure_port.go index 5117b60cf..b3feca38e 100644 --- a/common/net/configure_port.go +++ b/common/net/configure_port.go @@ -2,7 +2,6 @@ package net import ( "context" - "errors" "fmt" "log" "math/rand" @@ -55,17 +54,16 @@ func (lc ListenRangeConfig) Listen(ctx context.Context) (*Listener, error) { if lc.Max < lc.Min { lc.Min, lc.Max = lc.Max, lc.Min } - portRange := int(lc.Max - lc.Min) - if portRange == 0 { - // Intn will panic if portRange == 0 - return nil, errors.New("ListenRandomPort: port range is zero") - } + portRange := lc.Max - lc.Min for { if err := ctx.Err(); err != nil { return nil, err } - port := rand.Intn(portRange) + lc.Min + port := lc.Min + if portRange > 0 { + port += rand.Intn(portRange) + } log.Printf("Trying port: %d", port)