From 96059650709df224d4989f402f80d09fa02995fd Mon Sep 17 00:00:00 2001 From: Jonathan Mercier-Ganady Date: Thu, 8 Oct 2020 17:45:47 +0100 Subject: [PATCH] Expose server listen port --- server/server.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/server/server.go b/server/server.go index 2945e510..b66993a4 100644 --- a/server/server.go +++ b/server/server.go @@ -259,6 +259,19 @@ func (s *Server) IsRunning() bool { return s.running } +// GetListenPort returns the port the server is listening to. Returns 0 if the +// server is not listening. +func (s *Server) GetListenPort() int { + s.mu.RLock() + defer s.mu.RUnlock() + + if s.listener == nil { + return 0 + } + + return s.listener.Addr().(*net.TCPAddr).Port +} + // recoverAndPersistState recovers any existing server metadata state from disk // to initialize the server then writes the metadata back to disk. func (s *Server) recoverAndPersistState() error {