timeout

Timeout

原文:https://echo.labstack.com/docs/middleware/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
 2
 3
 4
 5
 6
 7
 8
 9
10
e := echo.New()
e.Use(middleware.TimeoutWithConfig(middleware.TimeoutConfig{
  Skipper: Skipper,
  ErrorHandler: func(err error, e echo.Context) error {
      // you can handle your error here, the returning error will be 
      // passed down the middleware chain
      return err
  },
  Timeout: 30*time.Second,
}))

Configuration

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

TimeoutErrorHandlerWithContext is responsible for handling the errors when a timeout happens

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

Default Configuration*

1
2
3
4
5
DefaultTimeoutConfig = TimeoutConfig{
    Skipper:      DefaultSkipper,
    Timeout:      0,
    ErrorHandler: nil,
}
最后修改 February 5, 2024: 更新 (f57b279)