title: How to create custom timeout handler based on request path?
body: <p>I wrote a timeout middleware. In this timeout middleware i created a goroutine for processing <strong>next</strong> handlers but after two seconds when timeout happens the response returns with 408 timeout status code but the goroutine runs in background and executes next handlers that are in handlers chain. <strong>So how to write a correct timeout middleware?</strong></p>
  
  <p><strong>Timeout Middleware</strong>: </p>
  
  <pre><code>type TimeoutHandler struct {
      Next http.Handler
  }
  
  func (handler *TimeoutHandler) ServeHTTP(responseWriter http.ResponseWriter, request *http.Request) {
       /*if nil 'Next' handler.*/
       if handler.Next == nil {
           /*delegate to default serve mux.*/
           handler.Next = http.DefaultServeMux
       }
  
       /*context with timeout.*/
       requestContext := request.Context()
       timeoutContext, cancelFunction := context.WithTimeout(requestContext, 2*time.Second)
       defer cancelFunction()
  
       /*request with new context.*/
       request = request.WithContext(timeoutContext)
       chanDone := make(chan bool)
  
       go func() {
            /*delegate request to `Next` handler*/
            handler.Next.ServeHTTP(responseWriter, request)
            chanDone &lt;- true
       }()
  
       select {
       case &lt;-timeoutContext.Done():
            /*status 408 request timeout.*/
            responseWriter.WriteHeader(http.StatusRequestTimeout)
       case &lt;-chanDone:
            return
      }
  }
  </code></pre>
  
  <p><strong>These are the problems that i have:</strong></p>
  
  <ol>
  <li>After timeout, next handler runs no matter what.</li>
  <li>If i use more handlers after timeout middleware, there is warning that says "http: multiple response.WriteHeader calls."</li>
  </ol>