net.ListenRangeConfig.Listen: allow to have a port rage of zero
This commit is contained in:
parent
cd39aa9988
commit
a4a83be2b9
|
@ -2,7 +2,6 @@ package net
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
|
@ -55,17 +54,16 @@ func (lc ListenRangeConfig) Listen(ctx context.Context) (*Listener, error) {
|
||||||
if lc.Max < lc.Min {
|
if lc.Max < lc.Min {
|
||||||
lc.Min, lc.Max = lc.Max, lc.Min
|
lc.Min, lc.Max = lc.Max, lc.Min
|
||||||
}
|
}
|
||||||
portRange := int(lc.Max - lc.Min)
|
portRange := lc.Max - lc.Min
|
||||||
if portRange == 0 {
|
|
||||||
// Intn will panic if portRange == 0
|
|
||||||
return nil, errors.New("ListenRandomPort: port range is zero")
|
|
||||||
}
|
|
||||||
for {
|
for {
|
||||||
if err := ctx.Err(); err != nil {
|
if err := ctx.Err(); err != nil {
|
||||||
return nil, err
|
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)
|
log.Printf("Trying port: %d", port)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue