Timeout

Timeout middleware is used to timeout at a long running operation within a predefined period.

Usage

  1. e.Use(middleware.Timeout())

Custom Configuration

Usage

  1. e := echo.New()
  2. e.Use(middleware.TimeoutWithConfig(middleware.TimeoutConfig{
  3. Skipper: Skipper,
  4. ErrorHandler: func(err error, e echo.Context) error {
  5. // you can handle your error here, the returning error will be
  6. // passed down the middleware chain
  7. return err
  8. },
  9. Timeout: 30*time.Second,
  10. }))

Configuration

  1. // TimeoutConfig defines the config for Timeout middleware.
  2. TimeoutConfig struct {
  3. // Skipper defines a function to skip middleware.
  4. Skipper Skipper
  5. // ErrorHandler defines a function which is executed for a timeout
  6. // It can be used to define a custom timeout error
  7. ErrorHandler TimeoutErrorHandlerWithContext
  8. // Timeout configures a timeout for the middleware, defaults to 0 for no timeout
  9. Timeout time.Duration
  10. }

TimeoutErrorHandlerWithContext is responsible for handling the errors when a timeout happens

  1. // TimeoutErrorHandlerWithContext is an error handler that is used
  2. // with the timeout middleware so we can handle the error
  3. // as we see fit
  4. TimeoutErrorHandlerWithContext func(error, echo.Context) error

Default Configuration*

  1. DefaultTimeoutConfig = TimeoutConfig{
  2. Skipper: DefaultSkipper,
  3. Timeout: 0,
  4. ErrorHandler: nil,
  5. }