Files
w3cjandClaude Opus 5.5 ae7c8b619f refactor(platform): parsePortRangeFirst and its test move to the linux files
Its only caller is ports_linux.go's ephemeralFirst, so on darwin
mise run deadcode listed it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-05 16:58:07 -07:00

45 lines
1.4 KiB
Go

package platform
import (
"context"
"errors"
"math/rand/v2"
)
// PortRange is the ports from Lo to Hi, both included.
type PortRange struct{ Lo, Hi int }
// The ports devvm chooses from: from portsLo to below the host's ephemeral
// range, which the system gives outgoing connections (a listen on a port
// a live connection holds fails on Linux), or to portsFallbackHi when
// that range can't be read or leaves fewer than portsMin.
const (
portsLo = 20000
portsFallbackHi = 32767
portsMin = 1000
)
// PortsBelow returns the range devvm chooses ports from on a host whose
// ephemeral range starts at first (ok false: unknown).
func PortsBelow(first int, ok bool) PortRange {
if !ok || first-1-portsLo+1 < portsMin {
return PortRange{portsLo, portsFallbackHi}
}
return PortRange{portsLo, first - 1}
}
// ChoosablePorts returns the range devvm chooses ports from on this host
// (PortsBelow its ephemeral range).
func ChoosablePorts() PortRange { return PortsBelow(ephemeralFirst()) }
// FreePort returns a random port of r that usable accepts, trying 1000.
func FreePort(ctx context.Context, r PortRange, usable func(ctx context.Context, port int) bool) (int, error) {
for range 1000 {
port := r.Lo + rand.IntN(r.Hi-r.Lo+1) //nolint:gosec // a port, not a secret
if usable(ctx, port) {
return port, nil
}
}
return 0, errors.New("no free port on 127.0.0.1")
}