From c666558f8cde087833a554d03bf5f45d3f560ead Mon Sep 17 00:00:00 2001 From: Asim Aslam Date: Fri, 25 May 2018 15:19:25 +0100 Subject: [PATCH] make the broker/transport listen on new addr when stop/started with addr :0 --- broker/http_broker.go | 8 +++++++- server/rpc_server.go | 7 +++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/broker/http_broker.go b/broker/http_broker.go index 5ff201c7..07a1e0d8 100644 --- a/broker/http_broker.go +++ b/broker/http_broker.go @@ -326,10 +326,16 @@ func (h *httpBroker) Connect() error { } log.Logf("Broker Listening on %s", l.Addr().String()) + addr := h.address h.address = l.Addr().String() go http.Serve(l, h.mux) - go h.run(l) + go func() { + h.run(l) + h.Lock() + h.address = addr + h.Unlock() + }() // get registry reg, ok := h.opts.Context.Value(registryKey).(registry.Registry) diff --git a/server/rpc_server.go b/server/rpc_server.go index 95d8d4a6..e600fb60 100644 --- a/server/rpc_server.go +++ b/server/rpc_server.go @@ -386,6 +386,8 @@ func (s *rpcServer) Start() error { log.Logf("Listening on %s", ts.Addr()) s.Lock() + // swap address + addr := s.opts.Address s.opts.Address = ts.Addr() s.Unlock() @@ -405,6 +407,11 @@ func (s *rpcServer) Start() error { // disconnect the broker config.Broker.Disconnect() + + s.Lock() + // swap back address + s.opts.Address = addr + s.Unlock() }() // TODO: subscribe to cruft