forked from go-chi/chi
/
main.go
97 lines (78 loc) · 2.31 KB
/
main.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
//
// Limits
// ======
// This example demonstrates the use of Timeout, and Throttle middlewares.
//
// Timeout:
// cancel a request if processing takes longer than 2.5 seconds,
// server will respond with a http.StatusGatewayTimeout.
//
// Throttle:
// limit the number of in-flight requests along a particular
// routing path and backlog the others.
//
package main
import (
"context"
"fmt"
"math/rand"
"time"
"github.com/swaggest/fchi"
"github.com/swaggest/fchi/middleware"
"github.com/valyala/fasthttp"
)
func main() {
r := fchi.NewRouter()
r.Use(middleware.RequestID)
r.Use(middleware.Recoverer)
r.Get("/", fchi.HandlerFunc(func(ctx context.Context, rc *fasthttp.RequestCtx) {
rc.Write([]byte("root."))
}))
r.Get("/ping", fchi.HandlerFunc(func(ctx context.Context, rc *fasthttp.RequestCtx) {
rc.Write([]byte("pong"))
}))
r.Get("/panic", fchi.HandlerFunc(func(ctx context.Context, rc *fasthttp.RequestCtx) {
panic("test")
}))
// Slow handlers/operations.
r.Group(func(r fchi.Router) {
// Stop processing after 2.5 seconds.
r.Use(middleware.Timeout(2500 * time.Millisecond))
r.Get("/slow", fchi.HandlerFunc(func(ctx context.Context, rc *fasthttp.RequestCtx) {
rand.Seed(time.Now().Unix())
// Processing will take 1-5 seconds.
processTime := time.Duration(rand.Intn(4)+1) * time.Second
select {
case <-ctx.Done():
return
case <-time.After(processTime):
// The above channel simulates some hard work.
}
rc.Write([]byte(fmt.Sprintf("Processed in %v seconds\n", processTime)))
}))
})
// Throttle very expensive handlers/operations.
r.Group(func(r fchi.Router) {
// Stop processing after 30 seconds.
r.Use(middleware.Timeout(30 * time.Second))
// Only one request will be processed at a time.
r.Use(middleware.Throttle(1))
r.Get("/throttled", fchi.HandlerFunc(func(ctx context.Context, rc *fasthttp.RequestCtx) {
select {
case <-ctx.Done():
switch ctx.Err() {
case context.DeadlineExceeded:
rc.Response.Header.SetStatusCode(504)
rc.Write([]byte("Processing too slow\n"))
default:
rc.Write([]byte("Canceled\n"))
}
return
case <-time.After(5 * time.Second):
// The above channel simulates some hard work.
}
rc.Write([]byte("Processed\n"))
}))
})
fasthttp.ListenAndServe(":3333", fchi.RequestHandler(r))
}