-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_test.go
More file actions
224 lines (179 loc) · 5.16 KB
/
Copy pathexample_test.go
File metadata and controls
224 lines (179 loc) · 5.16 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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package controls_test
import (
"context"
"fmt"
"net/http"
"time"
"gitlab.com/phpboyscout/go/errors"
"gitlab.com/phpboyscout/go/controls"
)
func ExampleNewController() {
ctx := context.Background()
// Create a controller. No OS signal handler is installed by default; a
// standalone daemon that owns signals adds controls.WithSignals().
controller := controls.NewController(ctx)
// Register an HTTP service
controller.Register("http-api",
controls.WithStart(func(ctx context.Context) error {
fmt.Println("HTTP server starting")
return nil
}),
controls.WithStop(func(ctx context.Context) {
fmt.Println("HTTP server stopping")
}),
controls.WithStatus(func() error {
return nil // healthy
}),
)
// Start all services
controller.Start()
// Graceful shutdown
time.Sleep(10 * time.Millisecond)
controller.Stop()
controller.Wait()
}
func ExampleWithRestartPolicy() {
controller := controls.NewController(context.Background())
controller.Register("worker",
controls.WithStart(func(ctx context.Context) error {
return nil
}),
controls.WithRestartPolicy(controls.RestartPolicy{
MaxRestarts: 3,
InitialBackoff: time.Second,
MaxBackoff: 30 * time.Second,
}),
)
_ = controller
}
func ExampleWithLiveness() {
controller := controls.NewController(context.Background())
controller.Register("api",
controls.WithStart(func(ctx context.Context) error { return nil }),
controls.WithLiveness(func() error {
// Check if the service can respond
resp, err := http.Get("http://localhost:8080/healthz")
if err != nil {
return err
}
_ = resp.Body.Close()
return nil
}),
)
_ = controller
}
// A stop that reports how it ended lands on ServiceInfo.StopErr, and the
// controller changes nothing else on the strength of it.
func ExampleWithStopErr() {
controller := controls.NewController(context.Background())
controller.Register("holder",
controls.WithStart(func(ctx context.Context) error {
<-ctx.Done()
return ctx.Err()
}),
controls.WithStopErr(func(context.Context) error {
return errors.New("listener still open")
}),
)
controller.Start()
controller.Stop()
controller.Wait()
info, _ := controller.GetServiceInfo("holder")
fmt.Println("stop error:", info.StopErr)
// Output:
// stop error: listener still open
}
// A Supervisor accepts children before and after Start. A child that returns
// nil has finished; one with no restart policy that returns an error is
// reported to the consumer once, as a Failure.
func ExampleSupervisor() {
sup := controls.NewSupervisor(
controls.WithOnFailure(func(f controls.Failure) {
fmt.Println("failed:", f.Name, "after", f.Restarts, "restarts")
}),
)
_ = sup.Attach(controls.Child{
Name: "once",
Start: func(context.Context) error {
fmt.Println("once ran")
return nil
},
})
if err := sup.Start(context.Background()); err != nil {
fmt.Println("start:", err)
return
}
// Attaching after Start is the point: the child is supervised identically.
_ = sup.Attach(controls.Child{
Name: "worker",
Start: func(ctx context.Context) error {
<-ctx.Done()
return ctx.Err()
},
})
_ = sup.Attach(controls.Child{
Name: "broken",
Start: func(context.Context) error { return errors.New("no such queue") },
})
for {
h := sup.Health()
if h["once"].State == controls.ChildStopped &&
h["worker"].State == controls.ChildRunning &&
h["broken"].State == controls.ChildFailed {
break
}
time.Sleep(time.Millisecond)
}
// The failed child does not make the supervisor unready.
fmt.Println("ready:", sup.Readiness() == nil)
fmt.Println("check:", sup.HealthCheck("children").Check(context.Background()).Message)
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
sup.Stop(ctx)
fmt.Println("after stop:", errors.Is(sup.Readiness(), controls.ErrSupervisorStopped))
// Unordered output:
// once ran
// failed: broken after 0 restarts
// ready: true
// check: 1 of 3 supervised children have failed
// after stop: true
}
type conn struct{ id int }
// A Generational hands each Start a value built fresh, refuses a second Start
// while one is live, and refuses Use once stopped rather than serving a stale
// handle.
func ExampleGenerational() {
built := 0
g := &controls.Generational[*conn]{
Build: func(context.Context) (*conn, error) {
built++
return &conn{id: built}, nil
},
Release: func(_ context.Context, c *conn) error {
fmt.Println("released", c.id)
return nil
},
}
ctx := context.Background()
show := func(c *conn) error {
fmt.Println("using", c.id, "in generation", g.Generation())
return nil
}
fmt.Println("use before start:", errors.Is(g.Use(show), controls.ErrNoGeneration))
_ = g.Start(ctx)
_ = g.Use(show)
fmt.Println("second start:", errors.Is(g.Start(ctx), controls.ErrGenerationRunning))
_ = g.Stop(ctx)
fmt.Println("use after stop:", errors.Is(g.Use(show), controls.ErrNoGeneration), "generation", g.Generation())
_ = g.Start(ctx)
_ = g.Use(show)
_ = g.Stop(ctx)
// Output:
// use before start: true
// using 1 in generation 1
// second start: true
// released 1
// use after stop: true generation 0
// using 2 in generation 2
// released 2
}