func proxyCanceler(proxy http.Handler) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		ctx, cancelFunc := context.WithTimeout(context.Background(), 6*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)
				}
			}
		}
	}
}
// proxyCanceler supports canceling proxied requests via a timeout, and
// returning a custom error response.
proxyCanceler supports canceling proxied requests via a timeout, and
returning a custom error response.
// proxyCanceler supports canceling proxied requests via a timeout and // returning a custom error response .
func proxyCanceler(proxy http.Handler) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		ctx, cancelFunc := context.WithTimeout(context.Background(), 6*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)
				}
			}
		}
	}
}
func proxyCanceler ( proxy http . Handler ) http . HandlerFunc { return func ( w http . ResponseWriter , r * http . Request ) { ctx , cancelFunc := context . WithTimeout ( context . Background ( ) , 6 * 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 : " " , Err : [ ] string { " " } , } ) if err != nil { log . Printf ( " " , err ) } } } } }
proxyCanceler supports canceling proxied requests via a timeout, and
returning a custom error response.
proxyCanceler supports canceling proxied requests via a timeout and returning a custom error response .