FAQ
In order to test a server that I am writing, I want to be able to start and
stop it in the testing framework.

To do so, I am hoping I can integrate the context package in with the
http.Server struct. I want to be able to stop the server when I call the
`Done` function and the `ctx.Done()` channel returns something.

What I would love to do would be to just modify the http.Server.Serve()
method
<https://github.com/golang/go/blob/release-branch.go1.4/src/net/http/server.go#L1721-L1753>,
to accept a context.Context and check if it is done, on each iteration of
the for loop, like this:

(srv *contextServer) Serve(ctx context.Context, l net.Listener) error {
  defer l.Close()
  var tempDelay time.Duration // how long to sleep on accept failure
  for {
  select {
  case <-ctx.Done():
  return nil
  default:
  }
  rw, e := l.Accept()
  ... rest is the same as original


However it seems like if I wanna add that check inside the for loop, I
would have to rewrite a lot of the methods, because this method calls other
private methods (like http.server.srv), which in turn call other private
methods....

I also notice that the for loop will stop when the Accept() method on the
listener returns an error.

However, I can't seem to figure out a way to get a listener to output an
error from it's accept method without accessing its private methods as well.

It seems like I am doing something very stupid and wrong if I have to copy
and paste half the http library just to let the server stop using the
context package.

I know there are lots of solutions around for supporting context canceling
for the ServeHTTP function, but that isn't what I am talking about. I wanna
pass a context to the whole server, not just to each incoming request.

Is this just impossible?

--
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+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

  • Tamás Gulácsi at Jun 26, 2015 at 7:58 am
    Just stop the listener.

    See http://www.hydrogen18.com/blog/stop-listening-http-server-go.html for example.

    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedJun 26, '15 at 7:28a
activeJun 26, '15 at 7:58a
posts2
users2
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase