How to know when ListenAndServe() is ready to handle connections

2,377 views
Skip to first unread message

Tony Worm

unread,
Jan 13, 2014, 10:32:37 PM1/13/14
to golan...@googlegroups.com
I'm trying to log when a server is actually up and ready to handle connections

With ListenAndServe() only returning on error, how does one get this information.


reaches the infinite loop on line 1621

there doesn't seem to be a state variable which could be exposed either

Jesse McNelis

unread,
Jan 13, 2014, 10:36:58 PM1/13/14
to Tony Worm, golang-nuts
ListenAndServe() is a convenience function for the most common case.
If you have less common requirements then writing your own Listen and Serve loop is trivial.


 -- 
=====================
http://jessta.id.au

Felix Boamah

unread,
Jan 15, 2014, 3:32:33 AM1/15/14
to golan...@googlegroups.com
what I'll do is that before I call ListenAndServe I'll log, like

log.Println("Up and Serving")
http.ListenAndServe(port, nil) 

roger peppe

unread,
Jan 15, 2014, 5:36:46 AM1/15/14
to Tony Worm, golang-nuts
As Jesse says, don't use ListenAndServe if you want be certain
of connecting to a newly started server.

listener, err := net.Listen("tcp", ":8080")
if err != nil { .... }
go http.Serve(listener, nil)
// now you can connect to the server without risk that it's not up.
> --
> You received this message because you are subscribed to the Google Groups
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to golang-nuts...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.

Felix Boamah

unread,
Jan 15, 2014, 10:49:30 AM1/15/14
to golan...@googlegroups.com, Tony Worm
How will the implementation look like rog
Reply all
Reply to author
Forward
0 new messages