|
|
@ -76,6 +76,9 @@ var ( |
|
|
|
// total is the total number of available ports in the block for use.
|
|
|
|
// total is the total number of available ports in the block for use.
|
|
|
|
total int |
|
|
|
total int |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// seededRand is a random generator that is pre-seeded from the current time.
|
|
|
|
|
|
|
|
seededRand *rand.Rand |
|
|
|
|
|
|
|
|
|
|
|
// stopCh is used to signal to background goroutines to terminate. Only
|
|
|
|
// stopCh is used to signal to background goroutines to terminate. Only
|
|
|
|
// really exists for the safety of reset() during unit tests.
|
|
|
|
// really exists for the safety of reset() during unit tests.
|
|
|
|
stopCh chan struct{} |
|
|
|
stopCh chan struct{} |
|
|
@ -114,6 +117,7 @@ func initialize() { |
|
|
|
panic("freeport: block size too big or too many blocks requested") |
|
|
|
panic("freeport: block size too big or too many blocks requested") |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
seededRand = rand.New(rand.NewSource(time.Now().UnixNano())) // This is compatible with go 1.19 but unnecessary in >= go1.20
|
|
|
|
firstPort, lockLn = alloc() |
|
|
|
firstPort, lockLn = alloc() |
|
|
|
|
|
|
|
|
|
|
|
condNotEmpty = sync.NewCond(&mu) |
|
|
|
condNotEmpty = sync.NewCond(&mu) |
|
|
@ -255,7 +259,7 @@ func adjustMaxBlocks() (int, error) { |
|
|
|
// be automatically released when the application terminates.
|
|
|
|
// be automatically released when the application terminates.
|
|
|
|
func alloc() (int, net.Listener) { |
|
|
|
func alloc() (int, net.Listener) { |
|
|
|
for i := 0; i < attempts; i++ { |
|
|
|
for i := 0; i < attempts; i++ { |
|
|
|
block := int(rand.Int31n(int32(effectiveMaxBlocks))) |
|
|
|
block := int(seededRand.Int31n(int32(effectiveMaxBlocks))) |
|
|
|
firstPort := lowPort + block*blockSize |
|
|
|
firstPort := lowPort + block*blockSize |
|
|
|
ln, err := net.ListenTCP("tcp", tcpAddr("127.0.0.1", firstPort)) |
|
|
|
ln, err := net.ListenTCP("tcp", tcpAddr("127.0.0.1", firstPort)) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|