Does it make sense to additionally use http.TimeoutHandler if I already set the server's ReadTimeout and WriteTimeout? It seems as if this scenario is mutually exclusive?
-
These two are completely unrelated. So you may use both.– FlimzyJul 10, 2018 at 8:01
-
Also see: stackoverflow.com/q/53051253/4213218– miáoNov 10, 2019 at 10:58
4 Answers
These two deal with different aspects of http request/response lifecycle.
http.TimeoutHandler
is used to limit execution time of the http.Handler. it will return 503
status code to the client, if http.Handler
doesn't finish in stipulated time.
While, ReadTimeout
and WriteTimeout
deals with network I/O timeout, i.e time required to read/write request/response body to your client respectively.
So, http.TimeoutHandler
handles the case where your handler (code block that handles http request) need to be complete in set amount of time, by wrapping your original handler. while http.WriteTimeout
or http.ReadTimeout
is used when you dont want to wait for network read/write of request/response indefinitely.
-
Thanks for your comment. Judging by @Zak's comment and graph,
http.TimeoutHandler
should be covered by the thehttp.Server.WriteTimeout
. Does that make sense?– mattesJul 10, 2018 at 8:03 -
2@mattes, No,
TimeoutHandler
handles the case where your handler (code block that handles http request) need to be complete in set amount of time, whileWriteTimeout
is used when you dont want to wait for network write indefinitely. Jul 10, 2018 at 8:21 -
@desaiparth hi, I'm using
http.TimeoutHandler
and after it return status503
, the handler (code block that handles http request) still executing. How can I terminate the execution of handler? Feb 29, 2020 at 7:17 -
@Souji Tendo You should listen channel of request context <-ctx.Done(). When there timeout you will get singal by listening channel of context.– StasVoJan 10 at 8:10
This blog article does a good job of explaining, but essentially they fulfil slightly different roles, as demonstrated here:
You've not included what your usecase is, but hopefully this is enough information to be able to make a decision.
-
Thanks @Zak! The use case is to limit the time a
http.Handler
can run. It seems as if I don't have to sethttp.TimeoutHandler
if I sethttp.Server.ReadTimeout
andhttp.Server.WriteTimeout
instead. Do you agree?– mattesJul 10, 2018 at 8:09 -
1Yes, I agree that setting read and write timeouts on the server is important. If the code is easy to include, I can see an argument for the added flexibility and control that you could have by using
http.TimeoutHandler
to enforce this case. But as already discussed, I think it's not strictly necessary if you have timeouts in other places. I think that the server timeouts are to defend against unresponsive clients, and thehttpTimeoutHandler
is to defend against slow handlers. In your case, I think you are worried about the slow handler?– ZakJul 10, 2018 at 8:23 -
If you are going to expose your app naked (directly) no HAproxy/Nginx in front, changing the defaults may help, for example (fine-tune them based on your requirements):
// configure server
srv := &http.Server{
Addr: ":8080",
Handler: router,
ReadTimeout: 5 * time.Second,
WriteTimeout: 7 * time.Second,
MaxHeaderBytes: 1 << 20,
}
log.Fatal(srv.ListenAndServe())
Here is a very nice article explaining more about the topic: https://blog.cloudflare.com/the-complete-guide-to-golang-net-http-timeouts/
-
Thanks. I read this blog post. I want to add that it seems important to eventually provide a custom
TCPListener
as well to override the 3 minutes default KeepAlive Timeout to something shorter. Source: blog.gopheracademy.com/advent-2016/exposing-go-on-the-internet– mattesJul 10, 2018 at 8:05 -
1If you sed
ReadTimeout
= 5s, how you can upload large file to the server? For example you need to upload 10mb image file, it takes 30s. So it would failed to serve? Nov 25, 2018 at 11:58 -
1@TomSawyer check this post/gist groups.google.com/forum/#!topic/golang-nuts/NBhJqZe25EA, gist.github.com/jbardin/9663312 it could give you some ideas– nbariNov 25, 2018 at 16:35
Also exists important difference between them if you are use Context. In case if you are use http.TimeoutHandler, you will get cancel signal from request context <-ctx.Done() with which you can terminate running processes.
In case if you are use WriteTimeout and if given time limit is reached you will not get cancel signal from request context.