Skip to content

Request Timeouts

The timeout middleware changes the request context; downstream database, HTTP, and application calls must use c.Context() for cancellation to propagate.

package main
import (
"log"
"time"
"github.com/0mjs/zinc"
"github.com/0mjs/zinc/middleware"
)
func main() {
app := zinc.New()
app.Use(middleware.ContextTimeoutWithConfig(middleware.ContextTimeoutConfig{
Timeout: 3 * time.Second,
Skipper: func(c *zinc.Context) bool {
return c.Path() == "/events"
},
}))
app.Get("/report", func(c *zinc.Context) error {
select {
case <-time.After(2 * time.Second):
return c.JSON(zinc.Map{"status": "ready"})
case <-c.Context().Done():
return c.Context().Err()
}
})
log.Fatal(app.Listen(":8080"))
}

Reduce the middleware timeout below two seconds to see Zinc return the default 503 Service Unavailable timeout error.