Interesting - now I get the same messages:

2017/07/10 13:23:17 Server starting up...
2017/07/10 13:23:22 Server shutting down...
test - before shutdown

But the program doesn't stop! I need to Ctrl+C to stop it...

I don't get it - why doesn't server.Shutdown() just work?


On Monday, July 10, 2017 at 12:50:20 PM UTC+10, Matt Harden wrote:
>
> Try a channel to wait for shutdown in main.
>
> func main() {
>     srv := &http.Server{Addr: ":8080", Handler: http.DefaultServeMux}
> *    done := make(chan struct{})*
>
>     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)
> *        close(done)*
>     })
>     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)
>     }
> *    <-done*
> }
>
>
> On Sun, Jul 9, 2017 at 5:45 AM <char...@gmail.com <javascript:>> wrote:
>
>> 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...@googlegroups.com <javascript:>.
>> For more options, visit https://groups.google.com/d/optout.
>>
>

-- 
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.

Reply via email to