Skip to content

Commit 74b075c

Browse files
authored
Merge pull request #173 from ilijamt/timeout-middleware
Added documentation about the timeout middleware
2 parents 6491840 + 220e69a commit 74b075c

File tree

3 files changed

+108
-0
lines changed

3 files changed

+108
-0
lines changed

cookbook/timeouts/server.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package main
2+
3+
import (
4+
"net/http"
5+
"time"
6+
7+
"github.com/labstack/echo/v4"
8+
"github.com/labstack/echo/v4/middleware"
9+
)
10+
11+
func main() {
12+
// Echo instance
13+
e := echo.New()
14+
15+
// Middleware
16+
e.Use(middleware.TimeoutWithConfig(middleware.TimeoutConfig{
17+
Timeout: 5 * time.Second,
18+
}))
19+
20+
// Route => handler
21+
e.GET("/", func(c echo.Context) error {
22+
time.Sleep(10 * time.Second)
23+
return c.String(http.StatusOK, "Hello, World!\n")
24+
})
25+
26+
// Start server
27+
e.Logger.Fatal(e.Start(":1323"))
28+
}

website/content/cookbook/timeouts.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
+++
2+
title = "Timeouts Recipe"
3+
description = "Timeout recipe for Echo"
4+
[menu.main]
5+
name = "Timeouts"
6+
parent = "cookbook"
7+
+++
8+
9+
`server.go`
10+
11+
{{< embed "timeouts/server.go" >}}
12+
13+
## [Source Code]({{< source "timeouts" >}})
14+
15+
## Maintainers
16+
17+
- [ilijamt](https://github.com/ilijamt)

website/content/middleware/timeout.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
+++
2+
title = "Timeout Middleware"
3+
description = "Timeout middleware for Echo"
4+
[menu.main]
5+
name = "Timeout"
6+
parent = "middleware"
7+
+++
8+
9+
Timeout middleware is used to timeout at a long running operation within a predefined period.
10+
11+
*Usage*
12+
13+
`e.Use(middleware.Timeout())`
14+
15+
## Custom Configuration
16+
17+
*Usage*
18+
19+
```go
20+
e := echo.New()
21+
e.Use(middleware.TimeoutWithConfig(middleware.TimeoutConfig{
22+
Skipper: Skipper,
23+
ErrorHandler: func(err error, e echo.Context) error {
24+
// you can handle your error here, the returning error will be
25+
// passed down the middleware chain
26+
return err
27+
},
28+
Timeout: 30*time.Second,
29+
}))
30+
```
31+
32+
## Configuration
33+
34+
```go
35+
// TimeoutConfig defines the config for Timeout middleware.
36+
TimeoutConfig struct {
37+
// Skipper defines a function to skip middleware.
38+
Skipper Skipper
39+
// ErrorHandler defines a function which is executed for a timeout
40+
// It can be used to define a custom timeout error
41+
ErrorHandler TimeoutErrorHandlerWithContext
42+
// Timeout configures a timeout for the middleware, defaults to 0 for no timeout
43+
Timeout time.Duration
44+
}
45+
```
46+
47+
*TimeoutErrorHandlerWithContext* is responsible for handling the errors when a timeout happens
48+
```go
49+
// TimeoutErrorHandlerWithContext is an error handler that is used
50+
// with the timeout middleware so we can handle the error
51+
// as we see fit
52+
TimeoutErrorHandlerWithContext func(error, echo.Context) error
53+
```
54+
55+
*Default Configuration*
56+
57+
```go
58+
DefaultTimeoutConfig = TimeoutConfig{
59+
Skipper: DefaultSkipper,
60+
Timeout: 0,
61+
ErrorHandler: nil,
62+
}
63+
```

0 commit comments

Comments
 (0)