From 75d62f9c0bfbdb159f4753b4c5c4515ef049c435 Mon Sep 17 00:00:00 2001 From: Iwan Budi Kusnanto Date: Wed, 28 Feb 2018 15:05:31 +0700 Subject: [PATCH] Add server.Addr() : returns server's listen address. Useful to get actual listen address when we listen to random port (localhost:0). Also move the `s.ln=ln` assignment from `serve` to `ListenServeAndSignal` to avoid race condition. --- redcon.go | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/redcon.go b/redcon.go index 90c9e9e..1974bbe 100644 --- a/redcon.go +++ b/redcon.go @@ -178,6 +178,11 @@ func (s *Server) ListenAndServe() error { return s.ListenServeAndSignal(nil) } +// Addr returns server's listen address +func (s *Server) Addr() net.Addr { + return s.ln.Addr() +} + // Close stops listening on the TCP address. // Already Accepted connections will be closed. func (s *TLSServer) Close() error { @@ -247,10 +252,11 @@ func (s *Server) ListenServeAndSignal(signal chan error) error { } return err } + s.ln = ln if signal != nil { signal <- nil } - return serve(s, ln) + return serve(s) } // ListenServeAndSignal serves incoming connections and passes nil or error @@ -263,18 +269,16 @@ func (s *TLSServer) ListenServeAndSignal(signal chan error) error { } return err } + s.ln = ln if signal != nil { signal <- nil } - return serve(s.Server, ln) + return serve(s.Server) } -func serve(s *Server, ln net.Listener) error { - s.mu.Lock() - s.ln = ln - s.mu.Unlock() +func serve(s *Server) error { defer func() { - ln.Close() + s.ln.Close() func() { s.mu.Lock() defer s.mu.Unlock() @@ -285,7 +289,7 @@ func serve(s *Server, ln net.Listener) error { }() }() for { - lnconn, err := ln.Accept() + lnconn, err := s.ln.Accept() if err != nil { s.mu.Lock() done := s.done