|
1 | 1 | # Changelog |
2 | 2 |
|
| 3 | +## v4.15.0 - TBD |
| 4 | + |
| 5 | +**DEPRECATION NOTICE** Timeout Middleware Deprecated - Use ContextTimeout Instead |
| 6 | + |
| 7 | +The `middleware.Timeout` middleware has been **deprecated** due to fundamental architectural issues that cause |
| 8 | +data races. Use `middleware.ContextTimeout` or `middleware.ContextTimeoutWithConfig` instead. |
| 9 | + |
| 10 | +**Why is this being deprecated?** |
| 11 | + |
| 12 | +The Timeout middleware manipulates response writers across goroutine boundaries, which causes data races that |
| 13 | +cannot be reliably fixed without a complete architectural redesign. The middleware: |
| 14 | + |
| 15 | +- Swaps the response writer using `http.TimeoutHandler` |
| 16 | +- Must be the first middleware in the chain (fragile constraint) |
| 17 | +- Can cause races with other middleware (Logger, metrics, custom middleware) |
| 18 | +- Has been the source of multiple race condition fixes over the years |
| 19 | + |
| 20 | +**What should you use instead?** |
| 21 | + |
| 22 | +The `ContextTimeout` middleware (available since v4.12.0) provides timeout functionality using Go's standard |
| 23 | +context mechanism. It is: |
| 24 | + |
| 25 | +- Race-free by design |
| 26 | +- Can be placed anywhere in the middleware chain |
| 27 | +- Simpler and more maintainable |
| 28 | +- Compatible with all other middleware |
| 29 | + |
| 30 | +**Migration Guide:** |
| 31 | + |
| 32 | +```go |
| 33 | +// Before (deprecated): |
| 34 | +e.Use(middleware.Timeout()) |
| 35 | + |
| 36 | +// After (recommended): |
| 37 | +e.Use(middleware.ContextTimeout(30 * time.Second)) |
| 38 | +``` |
| 39 | + |
| 40 | +With configuration: |
| 41 | +```go |
| 42 | +// Before (deprecated): |
| 43 | +e.Use(middleware.TimeoutWithConfig(middleware.TimeoutConfig{ |
| 44 | + Timeout: 30 * time.Second, |
| 45 | + Skipper: func(c echo.Context) bool { |
| 46 | + return c.Path() == "/health" |
| 47 | + }, |
| 48 | +})) |
| 49 | + |
| 50 | +// After (recommended): |
| 51 | +e.Use(middleware.ContextTimeoutWithConfig(middleware.ContextTimeoutConfig{ |
| 52 | + Timeout: 30 * time.Second, |
| 53 | + Skipper: func(c echo.Context) bool { |
| 54 | + return c.Path() == "/health" |
| 55 | + }, |
| 56 | +})) |
| 57 | +``` |
| 58 | + |
| 59 | +**Important Behavioral Differences:** |
| 60 | + |
| 61 | +1. **Handler cooperation required**: With ContextTimeout, your handlers must check `context.Done()` for cooperative |
| 62 | + cancellation. The old Timeout middleware would send a 503 response regardless of handler cooperation, but had |
| 63 | + data race issues. |
| 64 | + |
| 65 | +2. **Error handling**: ContextTimeout returns errors through the standard error handling flow. Handlers that receive |
| 66 | + `context.DeadlineExceeded` should handle it appropriately: |
| 67 | + |
| 68 | +```go |
| 69 | +e.GET("/long-task", func(c echo.Context) error { |
| 70 | + ctx := c.Request().Context() |
| 71 | + |
| 72 | + // Example: database query with context |
| 73 | + result, err := db.QueryContext(ctx, "SELECT * FROM large_table") |
| 74 | + if err != nil { |
| 75 | + if errors.Is(err, context.DeadlineExceeded) { |
| 76 | + // Handle timeout |
| 77 | + return echo.NewHTTPError(http.StatusServiceUnavailable, "Request timeout") |
| 78 | + } |
| 79 | + return err |
| 80 | + } |
| 81 | + |
| 82 | + return c.JSON(http.StatusOK, result) |
| 83 | +}) |
| 84 | +``` |
| 85 | + |
| 86 | +3. **Background tasks**: For long-running background tasks, use goroutines with context: |
| 87 | + |
| 88 | +```go |
| 89 | +e.GET("/async-task", func(c echo.Context) error { |
| 90 | + ctx := c.Request().Context() |
| 91 | + |
| 92 | + resultCh := make(chan Result, 1) |
| 93 | + errCh := make(chan error, 1) |
| 94 | + |
| 95 | + go func() { |
| 96 | + result, err := performLongTask(ctx) |
| 97 | + if err != nil { |
| 98 | + errCh <- err |
| 99 | + return |
| 100 | + } |
| 101 | + resultCh <- result |
| 102 | + }() |
| 103 | + |
| 104 | + select { |
| 105 | + case result := <-resultCh: |
| 106 | + return c.JSON(http.StatusOK, result) |
| 107 | + case err := <-errCh: |
| 108 | + return err |
| 109 | + case <-ctx.Done(): |
| 110 | + return echo.NewHTTPError(http.StatusServiceUnavailable, "Request timeout") |
| 111 | + } |
| 112 | +}) |
| 113 | +``` |
| 114 | + |
| 115 | + |
3 | 116 | ## v4.14.0 - 2025-12-11 |
4 | 117 |
|
5 | 118 | `middleware.Logger` has been deprecated. For request logging, use `middleware.RequestLogger` or |
|
0 commit comments