From e6d60881cba36335cdeb4e1df15ba54203958736 Mon Sep 17 00:00:00 2001 From: Ken Robertson Date: Tue, 27 May 2014 09:08:07 -0700 Subject: [PATCH] Added Server.Addr() helper to allow querying of the server's bound address This can be useful in testing scenarios by allowing you to have the system pick a random port by passing 0 on options.Port, but also give the user a way to query the server for what port it actually bound to. --- server/server.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/server/server.go b/server/server.go index 0f49d890..6bbbf5e9 100644 --- a/server/server.go +++ b/server/server.go @@ -480,3 +480,13 @@ func (s *Server) NumSubscriptions() uint32 { stats := s.sl.Stats() return stats.NumSubs } + +// Addr will return the net.Addr object for the current listener. +func (s *Server) Addr() net.Addr { + s.mu.Lock() + defer s.mu.Unlock() + if s.listener == nil { + return nil + } + return s.listener.Addr() +}