-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_test.go
More file actions
189 lines (153 loc) · 3.8 KB
/
Copy pathexample_test.go
File metadata and controls
189 lines (153 loc) · 3.8 KB
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package probez_test
import (
"context"
"errors"
"fmt"
"net/http"
"sync/atomic"
"time"
"github.com/gokern/probez"
)
// Minimal lifecycle: start, mark ready, query /readyz.
func Example() {
err := probez.Start(0,
probez.WithHost("127.0.0.1"),
probez.WithStaleAfter(30*time.Second),
probez.WithReadinessCheck("db", func(_ context.Context) error {
return nil // simulate healthy DB
}),
)
if err != nil {
panic(err)
}
defer func() { _ = probez.Close(context.Background()) }()
probez.MarkStarted()
probez.MarkReady()
probez.Ping()
resp, err := http.Get("http://" + probez.Addr() + "/readyz")
if err != nil {
panic(err)
}
_ = resp.Body.Close()
fmt.Println(resp.StatusCode)
// Output: 200
}
// HTTP server with WithAutoLive: responding to /livez is itself proof of life,
// so no Ping() is needed. Fits API services whose request path already
// signals liveness.
func ExampleWithAutoLive() {
api := &http.Server{
Addr: "127.0.0.1:0",
ReadHeaderTimeout: 5 * time.Second,
Handler: http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
_, _ = w.Write([]byte("ok"))
}),
}
p, err := probez.New(0,
probez.WithHost("127.0.0.1"),
probez.WithAutoLive(),
)
if err != nil {
panic(err)
}
defer func() { _ = p.Close(context.Background()) }()
defer func() { _ = api.Shutdown(context.Background()) }()
// API came up; no background loop to heartbeat from.
p.MarkStarted()
p.MarkReady()
resp, err := http.Get("http://" + p.Addr() + "/livez")
if err != nil {
panic(err)
}
_ = resp.Body.Close()
fmt.Println(resp.StatusCode)
// Output: 200
}
// Background worker: a processing loop calls Ping() every iteration so /livez
// can detect a stuck consumer. If the loop blocks for longer than WithStaleAfter,
// /livez returns 503 and Kubernetes restarts the pod.
func ExampleProbe_Ping() {
p, err := probez.New(0,
probez.WithHost("127.0.0.1"),
probez.WithStaleAfter(2*time.Second),
probez.WithStartupGrace(0),
)
if err != nil {
panic(err)
}
defer func() { _ = p.Close(context.Background()) }()
p.MarkStarted()
p.MarkReady()
queue := make(chan int, 3)
for msg := range 3 {
queue <- msg + 1
}
close(queue)
var processed atomic.Int64
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
done := make(chan struct{})
go func() {
defer close(done)
for {
select {
case <-ctx.Done():
return
case msg, ok := <-queue:
if !ok {
return
}
p.Ping() // heartbeat before each unit of work
processed.Add(int64(msg))
}
}
}()
<-done
resp, err := http.Get("http://" + p.Addr() + "/livez")
if err != nil {
panic(err)
}
_ = resp.Body.Close()
fmt.Println("livez:", resp.StatusCode)
fmt.Println("processed:", processed.Load())
// Output:
// livez: 200
// processed: 6
}
// Readiness check that reports an external dependency's state. On /readyz
// every registered check runs with the request context; any non-nil error
// flips the response to 503.
func ExampleWithReadinessCheck() {
var dbUp atomic.Bool
dbUp.Store(false)
p, err := probez.New(0,
probez.WithHost("127.0.0.1"),
probez.WithStaleAfter(time.Hour),
probez.WithStartupGrace(time.Hour),
probez.WithReadinessCheck("db", func(_ context.Context) error {
if !dbUp.Load() {
return errors.New("db not connected")
}
return nil
}),
)
if err != nil {
panic(err)
}
defer func() { _ = p.Close(context.Background()) }()
p.MarkStarted()
p.MarkReady()
p.Ping()
// DB still down — /readyz fails.
resp, _ := http.Get("http://" + p.Addr() + "/readyz")
_ = resp.Body.Close()
fmt.Println("before:", resp.StatusCode)
// DB connects — /readyz recovers.
dbUp.Store(true)
resp, _ = http.Get("http://" + p.Addr() + "/readyz")
_ = resp.Body.Close()
fmt.Println("after:", resp.StatusCode)
// Output:
// before: 503
// after: 200
}