-
Notifications
You must be signed in to change notification settings - Fork 13
/
streaminterceptor.go
72 lines (59 loc) · 1.76 KB
/
streaminterceptor.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
package grpchealthcheck
import (
"context"
"time"
"github.com/pkg/errors"
"google.golang.org/grpc"
healthpb "google.golang.org/grpc/health/grpc_health_v1"
)
// NewStreamInterceptor returns a gRPC StreamClientInterceptor
// which performs server health checks on the given interval
// for any stream which is initiated.
//
// health := grpchealthcheck.NewStreamInterceptor(30 * time.Second)
// conn, _ := grpc.Dial("server", grpc.WithStreamInterceptor(health))
func NewStreamInterceptor(interval time.Duration) grpc.StreamClientInterceptor {
return func(ctx context.Context, desc *grpc.StreamDesc, cc *grpc.ClientConn, method string, streamer grpc.Streamer, opts ...grpc.CallOption) (grpc.ClientStream, error) {
stream, err := streamer(ctx, desc, cc, method, opts...)
if err != nil {
return nil, err
}
return &healthCheckStream{
health: healthpb.NewHealthClient(cc),
interval: interval,
ClientStream: stream,
}, nil
}
}
type healthCheckStream struct {
health healthpb.HealthClient
interval time.Duration
grpc.ClientStream
}
func (s *healthCheckStream) RecvMsg(m interface{}) error {
healthcheck := time.NewTimer(s.interval)
defer healthcheck.Stop()
result := make(chan error, 1)
go func() { result <- s.ClientStream.RecvMsg(m) }()
for {
select {
case err := <-result:
return err
case <-healthcheck.C:
}
if err := s.healthcheck(); err != nil {
return err
}
healthcheck.Reset(s.interval)
}
}
func (s *healthCheckStream) healthcheck() error {
r, err := s.health.Check(s.Context(), &healthpb.HealthCheckRequest{})
if err != nil {
return err
}
if r.Status != healthpb.HealthCheckResponse_SERVING {
return errors.Errorf("server status = %v, want %v", r.Status, healthpb.HealthCheckResponse_SERVING)
}
return nil
}