@cryptotaxi247 / kubo / commits / bf955f356

add closenotify and large timeout to gateway

License: MIT Signed-off-by: Jeromy <jeromyj@gmail.com>

Jeromy committed Nov 19, 2015 at 15:28 UTC bf955f35601e84989835fbf430e8e26b3e8f29a8
1 file changed +12 -1
core/corehttp/gateway_handler.go
+12 -1
@@ -95,9 +95,20 @@ func (i *gatewayHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
95 }
96
97 func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request) {
98 - ctx, cancel := context.WithCancel(i.node.Context())
98 + ctx, cancel := context.WithTimeout(i.node.Context(), time.Hour)
99 + // the hour is a hard fallback, we don't expect it to happen, but just in case
100 defer cancel()
101
102 + if cn, ok := w.(http.CloseNotifier); ok {
103 + go func() {
104 + select {
105 + case <-cn.CloseNotify():
106 + case <-ctx.Done():
107 + }
108 + cancel()
109 + }()
110 + }
111 +
112 urlPath := r.URL.Path
113
114 // If the gateway is behind a reverse proxy and mounted at a sub-path,