Merge pull request #5782 from ipfs/feat/http-context

Switch to using request.Context()
This commit is contained in:
Steven Allen 2018-11-19 14:49:12 -08:00 committed by GitHub
commit ebc7cdceb5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -64,23 +64,11 @@ func (i *gatewayHandler) newDagFromReader(r io.Reader) (ipld.Node, error) {
chunker.DefaultSplitter(r))
}
// TODO(btc): break this apart into separate handlers using a more expressive muxer
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
ctx, cancel := context.WithTimeout(r.Context(), time.Hour)
defer cancel()
if cn, ok := w.(http.CloseNotifier); ok {
clientGone := cn.CloseNotify()
go func() {
select {
case <-clientGone:
case <-ctx.Done():
}
cancel()
}()
}
defer func() {
if r := recover(); r != nil {
log.Error("A panic occurred in the gateway handler!")