forked from reddit/baseplate.go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
drainer_example_test.go
68 lines (59 loc) · 1.64 KB
/
drainer_example_test.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
package baseplate_test
import (
"context"
"flag"
"io"
"github.com/reddit/baseplate.go"
baseplatethrift "github.com/reddit/baseplate.go/internal/gen-go/reddit/baseplate"
"github.com/reddit/baseplate.go/log"
"github.com/reddit/baseplate.go/thriftbp"
)
// A placeholder thrift service for the example.
type Service struct {
drainer baseplate.HealthCheckCloser
}
func (s *Service) IsHealthy(ctx context.Context, req *baseplatethrift.IsHealthyRequest) (bool, error) {
switch req.GetProbe() {
default:
// For unknown probes, default to readiness.
fallthrough
case baseplatethrift.IsHealthyProbe_READINESS:
return s.drainer.IsHealthy(ctx) /* && other healthy dependencies */, nil
case baseplatethrift.IsHealthyProbe_LIVENESS:
return true /* && other healthy dependencies */, nil
}
}
// This example demonstrates how to use baseplate.Drainer in your main function
// and service's IsHealthy handler.
func ExampleDrainer() {
flag.Parse()
ctx, bp, err := baseplate.New(context.Background(), baseplate.NewArgs{
// TODO: fill in NewArgs.
})
if err != nil {
log.Fatal(err)
}
defer bp.Close()
// TODO: Other initializations
drainer := baseplate.Drainer()
// TODO: Other initializations
processor := baseplatethrift.NewBaseplateServiceV2Processor(&Service{
drainer: drainer,
})
server, err := thriftbp.NewBaseplateServer(bp, thriftbp.ServerConfig{
Processor: processor,
})
if err != nil {
log.Fatal(err)
}
log.Info(baseplate.Serve(ctx, baseplate.ServeArgs{
Server: server,
PreShutdown: []io.Closer{
drainer,
// TODO: Other pre-shutdown closers
},
PostShutdown: []io.Closer{
// TODO: Post-shutdown closers
},
}))
}