1
0
mirror of https://github.com/gofiber/fiber.git synced 2025-02-21 20:33:08 +00:00
fiber/middleware/timeout.md
2020-07-02 12:16:12 +02:00

784 B

Timeout

Wrapper function which provides a handler with a timeout.

If the handler takes longer than the given duration to return, the timeout error is set and forwarded to the error handler.

Example

Import the middleware package that is part of the Fiber web framework

import (
  "github.com/gofiber/fiber"
  "github.com/gofiber/fiber/middleware"
)

After you initiate your Fiber app, you can use the following possibilities:

func main() {
  app := fiber.New()
    
  handler := func(ctx *fiber.Ctx) {
    ctx.Send("Hello, World 👋!")
  }

  // Wrap the handler with a timeout
  app.Get("/foo", middleware.Timeout(handler, 5 * time.Second))

  // ...
}

Signatures

func Timeout(handler fiber.Handler, timeout time.Duration) fiber.Handler {}