code: func proxyCanceler(proxy http.Handler) http.HandlerFunc {
  	return func(w http.ResponseWriter, r *http.Request) {
  		ctx, cancelFunc := context.WithTimeout(context.Background(), 60*time.Second)
  		defer cancelFunc()
  
  		cw := cancelingProxyResponseWriter{
  			redirect: false,
  			written:  false,
  			rw:       w,
  		}
  
  		r = r.WithContext(ctx)
  		done := make(chan bool)
  		go func() {
  			proxy.ServeHTTP(&cw, r)
  			close(done)
  		}()
  
  		select {
  		case <-done:
  			return
  		case <-ctx.Done():
  			if ctx.Err() != context.DeadlineExceeded {
  				return
  			}
  
  			cw.redirect = true
  			if !cw.written {
  				w.WriteHeader(http.StatusRequestTimeout)
  
  				enc := json.NewEncoder(w)
  				err := enc.Encode(&apitypes.Error{
  					Type: "request_timeout",
  					Err:  []string{"Request timed out"},
  				})
  				if err != nil {
  					log.Printf("Error writing response timeout: %s", err)
  				}
  			}
  		}
  	}
  }
docs: // proxyCanceler supports canceling proxied requests via a timeout, and
  // returning a custom error response.