Gracefully Shutdown File Server

821 views
Skip to first unread message

Andrew

unread,
Mar 9, 2017, 6:29:28 PM3/9/17
to golang-nuts
Hi, 

How can I gracefully shutdown a file server like this in Go1.8?

func main() {
    http
.Handle("/", http.FileServer(http.Dir("./")))
    http.ListenAndServe(":8080", nil)
}

Thanks,
Andrew

Dave Cheney

unread,
Mar 9, 2017, 6:58:52 PM3/9/17
to golang-nuts
Here is a short example I wrote for the Go 1.8 release party.

https://talks.godoc.org/github.com/davecheney/go-1.8-release-party/presentation.slide#36

Andrew

unread,
Mar 10, 2017, 12:46:03 PM3/10/17
to golang-nuts
Thanks Dave. 

According to your example, the server is shutdown immediately even in the middle of downloading a large file.

I can use a timeout to close the connection. but is there some way to know we've finished serving all files before we close it?
 
Andrew

Bruno Albuquerque

unread,
Mar 10, 2017, 12:50:36 PM3/10/17
to Andrew, golang-nuts
https://golang.org/pkg/net/http/#Server.Shutdown

func (*Server) Shutdown

func (srv *Server) Shutdown(ctx context.Context) error

Shutdown gracefully shuts down the server without interrupting any active connections. Shutdown works by first closing all open listeners, then closing all idle connections, and then waiting indefinitely for connections to return to idle and then shut down. If the provided context expires before the shutdown is complete, then the context's error is returned.

Shutdown does not attempt to close nor wait for hijacked connections such as WebSockets. The caller of Shutdown should separately notify such long-lived connections of shutdown and wait for them to close, if desired.



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

Andrew

unread,
Mar 10, 2017, 1:58:45 PM3/10/17
to golang-nuts, angan...@gmail.com
I have this simple program to serve files in the root folder. It always terminates immediately whenever I press Ctrl+C key and the file download process will be failed right away. I tried it in Windows 7 and Ubuntu 16.04.

https://play.golang.org/p/Akv3se9kkZ

Bruno Albuquerque

unread,
Mar 10, 2017, 2:05:57 PM3/10/17
to Andrew, golang-nuts
I am not sure what you were expecting as a result of your example, but it appears to work for me in Ubuntu:

$ go run test.go 
2017/03/11 03:03:13 Server started at localhost:8080
^C2017/03/11 03:03:19 Shutting down server...
2017/03/11 03:03:19 Server gracefully stopped



On Fri, Mar 10, 2017 at 10:58 AM Andrew <angan...@gmail.com> wrote:
I have this simple program to serve files in the root folder. It always terminates immediately whenever I press Ctrl+C key and the file download process will be failed right away. I tried it in Windows 7 and Ubuntu 16.04.

https://play.golang.org/p/Akv3se9kkZ

--

Andrew

unread,
Mar 10, 2017, 2:33:07 PM3/10/17
to golang-nuts, angan...@gmail.com
Suppose I have a new folder with two files, main.go(code from the link) and myfile.iso(large file).

After "go run main.go" from the console, open browser and type "localhost:8080/myfile.iso" to download the file. 
Back to the console and Ctrl+c to shut down the server when the file is downloading. 

The server will stop immediately and the file downloading process failed.

Is this the correct behavior of Server.Shutdown? 

I think the server should wait for the file downloading process to finish or timeout after 5 seconds instead of terminate immediately, right? 


Bruno Albuquerque

unread,
Mar 10, 2017, 3:18:54 PM3/10/17
to Andrew, golang-nuts
I am not very familiar with the shutdown process yet (I did not try to use it anywhere), but the documentation seems to imply that what you expected to happen is what should happen. So if the download is being interrupted, I guess that is a bug.


jimmy frasche

unread,
Mar 10, 2017, 3:23:23 PM3/10/17
to Bruno Albuquerque, Andrew, golang-nuts
Maybe go run is eating the signal? Does it still misbehave with go build?

Andrew

unread,
Mar 10, 2017, 3:35:50 PM3/10/17
to golang-nuts, b...@bug-br.org.br, angan...@gmail.com
go install and run the program, get the same result.

Bruno Albuquerque

unread,
Mar 10, 2017, 3:49:38 PM3/10/17
to Andrew, golang-nuts
Wait, you are explicitly calling the context cancel function. As I understand it that will shutdown the server immediately).

On Fri, Mar 10, 2017 at 12:35 PM Andrew <angan...@gmail.com> wrote:
go install and run the program, get the same result.

--

Bruno Albuquerque

unread,
Mar 10, 2017, 3:52:22 PM3/10/17
to Andrew, golang-nuts
Forget it. It is in a defer call. I guess I have no idea why it is behaving how it is.
 

amk...@gmail.com

unread,
Mar 14, 2017, 1:30:21 AM3/14/17
to golang-nuts, angan...@gmail.com
Maybe this is a bug or need clear documentation about this. 

I've filed a bug report here: https://github.com/golang/go/issues/19541

Andrew

Martin Schnabel

unread,
Mar 14, 2017, 5:02:08 PM3/14/17
to golan...@googlegroups.com
i looked into the issue and it seems the problem is the early exit from
the program. this should work https://play.golang.org/p/LdXUYyzDxY
> --
> 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
> <mailto:golang-nuts...@googlegroups.com>.

Andrew

unread,
Mar 14, 2017, 6:32:30 PM3/14/17
to golang-nuts
You are the expert, It works great!
I may close the issue. 

Thank you very much!

Andrew
Reply all
Reply to author
Forward
0 new messages