func (app *AppContext) Start() { httpListener, err := net.Listen("tcp", app.appConfig.Listen) if err != nil { panic(err) } app.listener = stoppableListener.Handle(httpListener) http.Serve(app.listener, app.negroni) if app.listener.Stopped { var alive int /* Wait at most 5 seconds for the clients to disconnect */ for i := 0; i < 5; i++ { /* Get the number of clients still connected */ alive = app.listener.ConnCount.Get() if alive == 0 { break } log.Printf("%d client(s) still connected…\n", alive) time.Sleep(1 * time.Second) } alive = app.listener.ConnCount.Get() if alive > 0 { log.Fatalf("Server stopped after 5 seconds with %d client(s) still connected.", alive) } else { log.Println("Server stopped gracefully.") os.Exit(0) } } else if err != nil { log.Fatal(err) } }
// StartHTTP runs the HTTP server. func (l *LocalServer) StartHTTP() { listener, error := net.Listen("tcp", l.hostPort) if nil != error { log.Fatalln(error) } l.listener = stoppableListener.Handle(listener) l.startServing() }
// StartTLS runs the TLS server. func (l *LocalServer) StartTLS(caPemPath, caKeyPath string) { config := l.tlsConfigProvider.Provide(caPemPath, caKeyPath) listener, error := tls.Listen("tcp", l.hostPort, config) if nil != error { log.Fatalln(error) } l.listener = stoppableListener.Handle(listener) l.startServing() }
func main() { /* Listen on port 8080 */ listener, err := net.Listen("tcp", ":8080") log.Println("Server listening on http://*:8080/") /* Make the listener stoppable to be able to shutdown the server gracefully */ stoppable := stoppableListener.Handle(listener) /* Handle SIGTERM (Ctrl+C) */ k := make(chan os.Signal, 1) signal.Notify(k, os.Interrupt) go func() { <-k stoppable.Stop <- true }() /* Our HelloWorld function */ http.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) { io.WriteString(w, "Hello World!\n") w.(http.Flusher).Flush() log.Println("Incoming request for", req.URL.Path) }) /* Serve until we receive a SIGTERM */ err = http.Serve(stoppable, nil) /* Check why the Serve loop exited */ if stoppable.Stopped { var alive int /* Wait at most 5 seconds for the clients to disconnect */ for i := 0; i < 5; i++ { /* Get the number of clients still connected */ alive = stoppable.ConnCount.Get() if alive == 0 { break } log.Printf("%d client(s) still connected…\n", alive) time.Sleep(1 * time.Second) } alive = stoppable.ConnCount.Get() if alive > 0 { log.Fatalf("Server stopped after 5 seconds with %d client(s) still connected.", alive) } else { log.Println("Server stopped gracefully.") os.Exit(0) } } else if err != nil { log.Fatal(err) } }
func (s *Server) ListenAndServe() error { l, err := net.Listen("tcp", s.Addr) if err != nil { return err } sl := stoppableListener.Handle(l) s.listener = sl s.startTime = time.Now() go s.updateStats() s.Serve(s.listener) return nil }
// Serve executes a HTTP server and blocks until the passed channel closes. func Serve(terminated <-chan struct{}, address string, handler http.Handler) error { listener, err := net.Listen("tcp", address) stoppable := stoppableListener.Handle(listener) go func() { select { case <-terminated: stoppable.Stop <- true } }() err = http.Serve(stoppable, handler) if !stoppable.Stopped && err != nil { return err } // Give up to 5 seconds for in-flight requests to complete killAt := time.Now().Add(5 * time.Second) for time.Now().Before(killAt) && stoppable.ConnCount.Get() > 0 { } return nil }
// SetListener can be used to set a different listener that should be used by the // HTTP server. This is primarily used during seamless binary upgrade. func (h *HTTP) SetListener(l net.Listener) { /* Make the listener stoppable to be able to shutdown the server gracefully */ h.SListener = stoppableListener.Handle(l) }