Just tried it but it doesn't work. The problem I started from is that server.Shutdown didn't wait for my requests to complete. (To check this I put in the time.Sleep(6000 * time.Millisecond)). When I narrowed down the problem it showed me that code after the Shutdown call wasn't being executed. And now putting a time.Sleep() in the main didn't help either...
On Sunday, July 9, 2017 at 10:13:49 PM UTC+10, Elias Naur wrote: > > Your main goroutine probably exits before the handler gets to write "after > shutdown" to the console. Try adding a time.Sleep(1*time.Second) as the > very last line in main(). > > - elias > > On Sunday, July 9, 2017 at 1:53:36 PM UTC+2, char...@gmail.com wrote: >> >> Hi, >> >> Go code after calling srv.Shutdown() simply isn't called. There is no >> error message, no dump, nothing. This is the output after visiting >> http://localhost:8080/stop on my browser >> >> 2017/07/09 13:58:40 Server starting up... >> 2017/07/09 13:58:44 Server shutting down... >> test - before shutdown >> >> Notice that "test - after shutdown" doesn't show up. What am I doing >> wrong? >> >> func main() { >> srv := &http.Server{Addr: ":8080", Handler: http.DefaultServeMux} >> >> http.Handle("/web/", http.FileServer(http.Dir("./"))) >> http.HandleFunc("/stop", func(w http.ResponseWriter, r *http.Request) >> { >> log.Println("Server shutting down...") >> fmt.Println("test - before shutdown") >> err := srv.Shutdown(context.Background()) >> fmt.Println("test - after shutdown") >> log.Println("Error: %v", err) >> }) >> http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { >> log.Println("Got request") >> time.Sleep(6000 * time.Millisecond) >> fmt.Fprintf(w, "Hello @ %s", time.Now()) >> log.Println("Finished request") >> }) >> >> log.Println("Server starting up...") >> if err := srv.ListenAndServe(); err != http.ErrServerClosed { >> log.Fatalf("Server startup failed! Error: %v", err) >> } >> } >> >> >> -- 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+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.