forked from go-chi/chi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
115 lines (91 loc) · 2.68 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package main
import (
"context"
"fmt"
"net/http"
"os"
"os/signal"
"time"
"github.com/go-chi/chi"
"github.com/go-chi/chi/middleware"
"github.com/go-chi/valve"
)
func main() {
// Our graceful valve shut-off package to manage code preemption and
// shutdown signaling.
valv := valve.New()
baseCtx := valv.Context()
// Example of a long running background worker thing..
go func(ctx context.Context) {
for {
<-time.After(1 * time.Second)
func() {
valve.Lever(ctx).Open()
defer valve.Lever(ctx).Close()
// actual code doing stuff..
fmt.Println("tick..")
time.Sleep(2 * time.Second)
// end-logic
// signal control..
select {
case <-valve.Lever(ctx).Stop():
fmt.Println("valve is closed")
return
case <-ctx.Done():
fmt.Println("context is cancelled, go home.")
return
default:
}
}()
}
}(baseCtx)
// HTTP service running in this program as well. The valve context is set
// as a base context on the server listener at the point where we instantiate
// the server - look lower.
r := chi.NewRouter()
r.Use(middleware.RequestID)
r.Use(middleware.Logger)
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("sup"))
})
r.Get("/slow", func(w http.ResponseWriter, r *http.Request) {
valve.Lever(r.Context()).Open()
defer valve.Lever(r.Context()).Close()
select {
case <-valve.Lever(r.Context()).Stop():
fmt.Println("valve is closed. finish up..")
case <-time.After(5 * time.Second):
// The above channel simulates some hard work.
// We want this handler to complete successfully during a shutdown signal,
// so consider the work here as some background routine to fetch a long running
// search query to find as many results as possible, but, instead we cut it short
// and respond with what we have so far. How a shutdown is handled is entirely
// up to the developer, as some code blocks are preemptable, and others are not.
time.Sleep(5 * time.Second)
}
w.Write([]byte(fmt.Sprintf("all done.\n")))
})
srv := http.Server{Addr: ":3333", Handler: chi.ServerBaseContext(baseCtx, r)}
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
go func() {
for range c {
// sig is a ^C, handle it
fmt.Println("shutting down..")
// first valv
valv.Shutdown(20 * time.Second)
// create context with timeout
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
defer cancel()
// start http shutdown
srv.Shutdown(ctx)
// verify, in worst case call cancel via defer
select {
case <-time.After(21 * time.Second):
fmt.Println("not all connections done")
case <-ctx.Done():
}
}
}()
srv.ListenAndServe()
}