Commit c31e4f72 authored by Lars Gierth's avatar Lars Gierth

gateway: move context/close-notify wiring

License: MIT
Signed-off-by: default avatarLars Gierth <larsg@systemli.org>
parent 036ca3a7
...@@ -60,6 +60,21 @@ func (i *gatewayHandler) newDagFromReader(r io.Reader) (node.Node, error) { ...@@ -60,6 +60,21 @@ func (i *gatewayHandler) newDagFromReader(r io.Reader) (node.Node, error) {
// TODO(btc): break this apart into separate handlers using a more expressive muxer // TODO(btc): break this apart into separate handlers using a more expressive muxer
func (i *gatewayHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { func (i *gatewayHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
ctx, cancel := context.WithTimeout(i.node.Context(), time.Hour)
// the hour is a hard fallback, we don't expect it to happen, but just in case
defer cancel()
if cn, ok := w.(http.CloseNotifier); ok {
clientGone := cn.CloseNotify()
go func() {
select {
case <-clientGone:
case <-ctx.Done():
}
cancel()
}()
}
defer func() { defer func() {
if r := recover(); r != nil { if r := recover(); r != nil {
log.Error("A panic occurred in the gateway handler!") log.Error("A panic occurred in the gateway handler!")
...@@ -83,7 +98,7 @@ func (i *gatewayHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { ...@@ -83,7 +98,7 @@ func (i *gatewayHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
} }
if r.Method == "GET" || r.Method == "HEAD" { if r.Method == "GET" || r.Method == "HEAD" {
i.getOrHeadHandler(w, r) i.getOrHeadHandler(ctx, w, r)
return return
} }
...@@ -113,21 +128,7 @@ func (i *gatewayHandler) optionsHandler(w http.ResponseWriter, r *http.Request) ...@@ -113,21 +128,7 @@ func (i *gatewayHandler) optionsHandler(w http.ResponseWriter, r *http.Request)
i.addUserHeaders(w) // return all custom headers (including CORS ones, if set) i.addUserHeaders(w) // return all custom headers (including CORS ones, if set)
} }
func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request) { func (i *gatewayHandler) getOrHeadHandler(ctx context.Context, w http.ResponseWriter, r *http.Request) {
ctx, cancel := context.WithTimeout(i.node.Context(), time.Hour)
// the hour is a hard fallback, we don't expect it to happen, but just in case
defer cancel()
if cn, ok := w.(http.CloseNotifier); ok {
clientGone := cn.CloseNotify()
go func() {
select {
case <-clientGone:
case <-ctx.Done():
}
cancel()
}()
}
urlPath := r.URL.Path urlPath := r.URL.Path
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment