Ограничить ожидание зависимостей и отвечать 503 при их недоступности

This commit is contained in:
2026-10-06 15:13:04 +03:00
parent 9890482747
commit f6acdf91e0
10 changed files with 313 additions and 21 deletions
+13
View File
@@ -1,6 +1,7 @@
package httpx
import (
"context"
"crypto/rand"
"encoding/hex"
"fmt"
@@ -93,6 +94,18 @@ func recovery() gin.HandlerFunc {
}
}
// Timeout bounds the whole request with one deadline: waiting for a pooled
// connection, SQL statements and session lookups all observe it, so a hung
// dependency ends in a 503 within d instead of holding the client.
func Timeout(d time.Duration) gin.HandlerFunc {
return func(c *gin.Context) {
ctx, cancel := context.WithTimeout(c.Request.Context(), d)
defer cancel()
c.Request = c.Request.WithContext(ctx)
c.Next()
}
}
// bodyLimit caps request bodies; exceeding it surfaces from the JSON
// decoder as *http.MaxBytesError.
func bodyLimit(maxBytes int64) gin.HandlerFunc {