-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
150 lines (122 loc) · 2.88 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package main
import (
"context"
"errors"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"github.com/paulbellamy/ratecounter"
)
type Handler struct{}
func logger(w http.ResponseWriter, parts ...any) {
line := fmt.Sprintln(parts...)
log.Println(line)
w.Write([]byte(line))
}
func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/healthz" {
w.WriteHeader(http.StatusOK)
logger(w, "/healthz", "from", r.RemoteAddr, "current rate", rate.Rate(), "requests in", interval)
return
}
if r.URL.Path == "/readyz" {
status := http.StatusOK
if shutdown {
status = http.StatusServiceUnavailable
}
w.WriteHeader(status)
logger(w, "/readyz", "from", r.RemoteAddr, "status", status, "current rate", rate.Rate(), "requests in", interval)
return
}
if r.URL.Path == "/waitz" {
log.Println("/waitz", "from", r.RemoteAddr, "current rate", rate.Rate(), "requests in", interval)
for {
if completed {
break
}
select {
case <-r.Context().Done():
log.Println("/waitz", "gone")
return
default:
time.Sleep(time.Second)
}
}
w.WriteHeader(http.StatusGone)
return
}
rate.Incr(1)
status := http.StatusOK
if shutdown {
status = http.StatusServiceUnavailable
}
w.WriteHeader(status)
logger(w, "hit", r.URL.Path, "from", r.RemoteAddr, "status", status, "current rate", rate.Rate(), "requests in", interval)
}
var shutdown = false
var completed = false
var rate *ratecounter.RateCounter
var interval time.Duration
func main() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
if os.Getenv("LOG") != "yes" {
log.SetOutput(ioutil.Discard)
}
interval = 10 * time.Second
if s := os.Getenv("INTERVAL"); s != "" {
if d, err := time.ParseDuration(s); err != nil {
panic(err)
} else {
interval = d
}
}
rate = ratecounter.NewRateCounter(interval)
cooldown := 10 * time.Second
if s := os.Getenv("COOLDOWN"); s != "" {
if d, err := time.ParseDuration(s); err != nil {
panic(err)
} else {
cooldown = d
}
}
var port = "8080"
if p := os.Getenv("PORT"); p != "" {
port = p
}
handler := &Handler{}
server := &http.Server{Addr: ":" + port, Handler: handler}
go func() {
if err := server.ListenAndServe(); !errors.Is(err, http.ErrServerClosed) {
panic(err)
}
}()
log.Println("k8s-prestop-sidecar started")
s := <-sigs
log.Println("received signal", s)
shutdown = true
for {
r := rate.Rate()
log.Println("shutdown", "current rate", r, "requests in", interval)
if r == 0 {
break
}
time.Sleep(time.Second)
}
log.Println("cooldown for", cooldown, "before releasing /waitz")
time.Sleep(cooldown)
log.Println("completed")
completed = true
log.Println("exiting")
if err := server.Shutdown(ctx); err != nil {
panic(err)
}
log.Println("bye")
}