-
Notifications
You must be signed in to change notification settings - Fork 11
/
transport.go
31 lines (26 loc) · 845 Bytes
/
transport.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
package grpc_health_v1
import (
"context"
"github.com/marmotedu/errors"
"google.golang.org/grpc/health/grpc_health_v1"
)
type healthCheckRequest struct {
Service string `json:"service"`
}
type healthCheckResponse struct {
Status HealthStatus `json:"status"`
}
func encodeClientRequest(ctx context.Context, r interface{}) (interface{}, error) {
switch r := r.(type) {
case healthCheckRequest:
return &grpc_health_v1.HealthCheckRequest{Service: r.Service}, nil
}
return nil, errors.New("invalid request for gRPC health check")
}
func decodeClientResponse(ctx context.Context, r interface{}) (interface{}, error) {
switch r := r.(type) {
case *grpc_health_v1.HealthCheckResponse:
return healthCheckResponse{Status: HealthStatus(r.GetStatus())}, nil
}
return nil, errors.New("invalid health check response from gRPC server")
}