title: How golang gin stop handler function immediately if connection is lost
body: <p>I am using <a href="https://github.com/gin-gonic/gin" rel="nofollow noreferrer">gin-gonic/gin</a> to write my server.</p>
  
  <p>It seems even if the connection is lost, the handler function is still running. For example, if I visit <a href="http://127.0.0.1:8080/ping" rel="nofollow noreferrer">http://127.0.0.1:8080/ping</a> and close the browser suddenly, the screen will continue to print all the numbers.</p>
  
  <pre><code>package main
  
  import (
      "github.com/gin-gonic/gin"
      "log"
      "time"
  )
  
  func main() {
      r := gin.Default()
      r.GET("/ping", func(c *gin.Context) {
          for i := 1; i &lt; 15; i++ {
              time.Sleep(time.Second * 1)
              log.Println(i)
          }
          c.JSON(200, gin.H{
              "message": "pong",
          })
      })
      r.Run("127.0.0.1:8080")
  }
  </code></pre>
  
  <p>How should I stop handler function immediately (e.g. to reduce server load)?</p>