forked from danielgtaylor/huma
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimeout.go
42 lines (34 loc) · 1.13 KB
/
timeout.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package main
import (
"context"
"net/http"
"time"
"github.com/danielgtaylor/huma"
"github.com/danielgtaylor/huma/cli"
"github.com/danielgtaylor/huma/responses"
)
func main() {
app := cli.NewRouter("Timeout Example", "1.0.0")
app.Resource("/timeout").Get("timeout", "Timeout example",
responses.String(http.StatusOK),
responses.InternalServerError(),
).Run(func(ctx huma.Context) {
// Add a timeout to the context. No outgoing request should take longer
// than 2 seconds or we abort.
newCtx, cancel := context.WithTimeout(ctx, 2*time.Second)
defer cancel()
// Create a new request that will take 5 seconds to complete.
req, _ := http.NewRequestWithContext(
newCtx, http.MethodGet, "https://httpstat.us/418?sleep=5000", nil)
// Make the request. This will return with an error because the context
// deadline of 2 seconds is shorter than the request duration of 5 seconds.
_, err := http.DefaultClient.Do(req)
if err != nil {
ctx.WriteError(http.StatusInternalServerError, "Problem with HTTP request", err)
return
}
// Success case, which we never get to.
ctx.Write([]byte("success!"))
})
app.Run()
}