On Thu, Jun 21, 2012 at 09:30:24AM -0700, Brad Fitzpatrick wrote:
> On Thu, Jun 21, 2012 at 8:24 AM, Tim Harig <
timh...@gmx.com> wrote:
>
> > On Thu, Jun 21, 2012 at 02:49:51AM -0700, josvazg wrote:
> > > Ok, so I should NOT use ListenAndServe unless there is no comming back
> > from
> > > that.
> >
> > I can empathize. Maybe the documentation should be a little more explicit
> > about that the fact that you cannot return from ListenAndServe. I cannot
> > think of many situations where you do not want some graceful way to stop a
> > server which makes ListenAndServe useless for all but the most trivial of
> > applications. Yet, examples abound about creating servers in Go using
> > ListenAndServe. It is easy for the beginning gopher to get mislead.
> >
>
> I disagree. Most my servers run forever and only stop due to some machine
> failure.
I am glad that you can do that. Most servers that I work with need
to be taken down for one reason or another whether for software or
hardware upgrades. Sometimes, it is better to do so gracefully rather
then using the default SIGTERM handler which may drop connections and/or
leave anything that was being written in an inconsistent state.
The present development situation also means that a lot of servers may
not be traditional production servers. Since Go yet lacks a viable GUI
package that works across platforms, the default answer seems to be to
create a web application hosted on a local server. I see no reason why
these servers need to run forever after they have been started.
Furthermore, this isn't a problem with the package itself since a graceful
shutdown can be achieved by manually handing the listener. The
problem is that most examples you find for writing a server in Go point
new users to boilerplate code using ListenAndServe. They may not even
consider the problem of shutdown until that they have started development.
I just feel that it is wise to place some kind of warning to prevent people
from going down that rabbit hole blindly. Doing so doesn't affect *your*
ability to use ListenAndServe if that is really what you want to do. Such
a warning would have prevented this post and others like it that I have
seen at stackoverflow and other places.