Improve unused port search to find ports more quickly

This commit is contained in:
Daniel 2021-12-05 14:06:45 +01:00
parent 0a02b89e60
commit 768a8b7df7

View file

@ -9,9 +9,7 @@ import (
// currently unused and is unlikely to be used within the next seconds.
func GetUnusedLocalPort(protocol uint8) (port uint16, ok bool) {
allConns := conns.clone()
tries := 1000
hundredth := tries / 100
// Try up to 1000 times to find an unused port.
nextPort:
@ -25,7 +23,7 @@ nextPort:
port := uint16(rN + 10000)
// Shrink range when we chew through the tries.
portRangeStart := port - uint16(100-(i/hundredth))
portRangeStart := port - 10
// Check if the generated port is unused.
nextConnection: