Skip to content

Instantly share code, notes, and snippets.

@mhewedy
Created August 19, 2019 12:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mhewedy/6e873a8c4339794f0498b3f46c3aafae to your computer and use it in GitHub Desktop.
Save mhewedy/6e873a8c4339794f0498b3f46c3aafae to your computer and use it in GitHub Desktop.
Detect request cancelation
package main
import (
"fmt"
"github.com/labstack/echo/v4"
"net/http"
"time"
)
func main() {
e := echo.New()
e.GET("/", func(context echo.Context) error {
var resp string
select {
case <-time.After(2 * time.Second): // long running process
resp = "request processed "
case <-context.Request().Context().Done():
resp = "request canceled"
}
fmt.Println(resp)
return context.String(http.StatusOK, resp)
})
e.Logger.Error(e.Start(":8080"))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment