From 91ca68aea07185f989b4fe1b626a5a6252a51c76 Mon Sep 17 00:00:00 2001 From: Tom Clegg Date: Tue, 10 Mar 2020 10:41:31 -0400 Subject: [PATCH] 15954: Dedup find-available-port logic. Arvados-DCO-1.1-Signed-off-by: Tom Clegg --- lib/boot/supervisor.go | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/lib/boot/supervisor.go b/lib/boot/supervisor.go index 38e978cab2..dc1f616e1b 100644 --- a/lib/boot/supervisor.go +++ b/lib/boot/supervisor.go @@ -496,9 +496,9 @@ func (super *Supervisor) autofillConfig(cfg *arvados.Config) error { return err } usedPort := map[string]bool{} - nextPort := func() string { + nextPort := func(host string) string { for { - port, err := availablePort(super.ListenHost) + port, err := availablePort(host) if err != nil { panic(err) } @@ -518,11 +518,7 @@ func (super *Supervisor) autofillConfig(cfg *arvados.Config) error { h = super.ListenHost } if p == "0" { - p, err = availablePort(h) - if err != nil { - return err - } - usedPort[p] = true + p = nextPort(h) } cluster.Services.Controller.ExternalURL = arvados.URL{Scheme: "https", Host: net.JoinHostPort(h, p)} } @@ -549,11 +545,11 @@ func (super *Supervisor) autofillConfig(cfg *arvados.Config) error { svc == &cluster.Services.WebDAVDownload || svc == &cluster.Services.Websocket || svc == &cluster.Services.Workbench1) { - svc.ExternalURL = arvados.URL{Scheme: "https", Host: fmt.Sprintf("%s:%s", super.ListenHost, nextPort())} + svc.ExternalURL = arvados.URL{Scheme: "https", Host: fmt.Sprintf("%s:%s", super.ListenHost, nextPort(super.ListenHost))} } if len(svc.InternalURLs) == 0 { svc.InternalURLs = map[arvados.URL]arvados.ServiceInstance{ - arvados.URL{Scheme: "http", Host: fmt.Sprintf("%s:%s", super.ListenHost, nextPort())}: arvados.ServiceInstance{}, + arvados.URL{Scheme: "http", Host: fmt.Sprintf("%s:%s", super.ListenHost, nextPort(super.ListenHost))}: arvados.ServiceInstance{}, } } } @@ -581,7 +577,7 @@ func (super *Supervisor) autofillConfig(cfg *arvados.Config) error { } if super.ClusterType == "test" { // Add a second keepstore process. - cluster.Services.Keepstore.InternalURLs[arvados.URL{Scheme: "http", Host: fmt.Sprintf("%s:%s", super.ListenHost, nextPort())}] = arvados.ServiceInstance{} + cluster.Services.Keepstore.InternalURLs[arvados.URL{Scheme: "http", Host: fmt.Sprintf("%s:%s", super.ListenHost, nextPort(super.ListenHost))}] = arvados.ServiceInstance{} // Create a directory-backed volume for each keepstore // process. @@ -608,7 +604,7 @@ func (super *Supervisor) autofillConfig(cfg *arvados.Config) error { cluster.PostgreSQL.Connection = arvados.PostgreSQLConnection{ "client_encoding": "utf8", "host": "localhost", - "port": nextPort(), + "port": nextPort(super.ListenHost), "dbname": "arvados_test", "user": "arvados", "password": "insecure_arvados_test", -- 2.30.2