Skip to content

Commit a2473f2

Browse files
committed
docs: document CloseContext and shutdown errors
1 parent ef91a1e commit a2473f2

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

docs/guides/error-handling.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,23 @@ if err != nil {
272272
}
273273
```
274274

275+
## Shutdown Errors
276+
277+
`App.Close()` aggregates multiple close failures into a single error using
278+
`errors.Join`. You can still test for specific errors with `errors.Is` or
279+
`errors.As`:
280+
281+
```go
282+
if err := app.Close(); err != nil {
283+
if errors.Is(err, errDBClose) {
284+
log.Printf("db close failed: %v", err)
285+
}
286+
if errors.Is(err, errCacheClose) {
287+
log.Printf("cache close failed: %v", err)
288+
}
289+
}
290+
```
291+
275292
## Tips
276293

277294
- Return errors, don't panic (except for truly unrecoverable situations)

docs/guides/lifecycle.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,37 @@ fmt.Println(svc1 == svc2) // true
135135

136136
modkit does not provide automatic cleanup hooks. You must manage cleanup manually:
137137

138+
### App.Close and CloseContext
139+
140+
If providers implement `io.Closer`, you can shut down the app explicitly:
141+
142+
```go
143+
// Close all io.Closer providers in reverse build order.
144+
if err := app.Close(); err != nil {
145+
log.Printf("shutdown error: %v", err)
146+
}
147+
```
148+
149+
`Close()` is idempotent: once it completes successfully (even with aggregated errors),
150+
subsequent calls return `nil` and do not re-close providers.
151+
152+
For context-aware shutdown, use `CloseContext(ctx)`:
153+
154+
```go
155+
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
156+
defer cancel()
157+
158+
if err := app.CloseContext(ctx); err != nil {
159+
// Returns ctx.Err() if canceled or timed out
160+
log.Printf("shutdown error: %v", err)
161+
}
162+
```
163+
164+
`CloseContext` checks `ctx.Err()` before closing and before each closer. If the context
165+
is canceled mid-close, it returns the context error and leaves the app eligible for
166+
a later `Close()` retry. While a close is in progress, concurrent close calls are
167+
no-ops.
168+
138169
### Pattern 1: Cleanup in main()
139170

140171
```go

0 commit comments

Comments
 (0)