mirror of
https://github.com/gravitl/netmaker.git
synced 2025-09-12 16:14:37 +08:00
more efficient check for free ports
This commit is contained in:
parent
e790b87d81
commit
dc58c2c637
1 changed files with 11 additions and 30 deletions
|
@ -321,41 +321,22 @@ func GetRelatedHosts(hostID string) []models.Host {
|
||||||
// with the same endpoint have different listen ports
|
// with the same endpoint have different listen ports
|
||||||
// in the case of 64535 hosts or more with same endpoint, ports will not be changed
|
// in the case of 64535 hosts or more with same endpoint, ports will not be changed
|
||||||
func CheckHostPorts(h *models.Host) {
|
func CheckHostPorts(h *models.Host) {
|
||||||
h.ListenPort = checkPort(h, h.ListenPort)
|
portsInUse := make(map[int]bool)
|
||||||
h.ProxyListenPort = checkPort(h, h.ProxyListenPort)
|
|
||||||
// rerun if ports were set to same value
|
|
||||||
if h.ListenPort == h.ProxyListenPort {
|
|
||||||
updatePort(&h.ProxyListenPort)
|
|
||||||
h.ProxyListenPort = checkPort(h, h.ProxyListenPort)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func checkPort(h *models.Host, p int) int {
|
|
||||||
currentPort := p
|
|
||||||
count := 0
|
|
||||||
hosts, err := GetAllHosts()
|
hosts, err := GetAllHosts()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return currentPort
|
return
|
||||||
}
|
}
|
||||||
for _, host := range hosts {
|
for _, host := range hosts {
|
||||||
if host.ID == h.ID {
|
portsInUse[host.ListenPort] = true
|
||||||
//skip self
|
portsInUse[host.ProxyListenPort] = true
|
||||||
continue
|
}
|
||||||
}
|
// iterate until port is not found or max iteration is reached
|
||||||
if host.EndpointIP.Equal(h.EndpointIP) {
|
for i := 0; portsInUse[h.ListenPort] && i < maxPort-minPort+1; i++ {
|
||||||
if p == host.ListenPort || p == host.ProxyListenPort {
|
updatePort(&h.ListenPort)
|
||||||
updatePort(&p)
|
}
|
||||||
count++
|
for i := 0; portsInUse[h.ProxyListenPort] && i < maxPort-minPort+1; i++ {
|
||||||
//protect against endless recursion
|
updatePort(&h.ProxyListenPort)
|
||||||
if count > maxPort-minPort {
|
|
||||||
logger.Log(0, "hit max interations in checkport")
|
|
||||||
return currentPort
|
|
||||||
}
|
|
||||||
p = checkPort(h, p)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return p
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// HostExists - checks if given host already exists
|
// HostExists - checks if given host already exists
|
||||||
|
|
Loading…
Add table
Reference in a new issue