Ограничить ожидание зависимостей и отвечать 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
+20
View File
@@ -307,6 +307,26 @@ func TestWithUserWithoutIdentityIsAWiringError(t *testing.T) {
assertError(t, rec, eb, http.StatusInternalServerError, "internal_error")
}
// Everything below Timeout sees one deadline, so waiting on a dependency
// ends with the request instead of holding the client.
func TestTimeoutBoundsTheRequestContext(t *testing.T) {
e := newEngine(t, nil)
e.GET("/slow", httpx.Timeout(50*time.Millisecond), func(c *gin.Context) {
select {
case <-c.Request.Context().Done():
httpx.Error(c, apperr.Wrap(apperr.KindUnavailable, "gave up", c.Request.Context().Err()))
case <-time.After(5 * time.Second):
httpx.JSON(c, http.StatusOK, gin.H{})
}
})
start := time.Now()
rec, eb := do(t, e, http.MethodGet, "/slow", "", nil)
assertError(t, rec, eb, http.StatusServiceUnavailable, "unavailable")
if elapsed := time.Since(start); elapsed > time.Second {
t.Fatalf("request took %v, want it cut at the 50ms deadline", elapsed)
}
}
func logEntry(t *testing.T, logs *bytes.Buffer) map[string]any {
t.Helper()
var entry map[string]any