change the health check implementation

This commit is contained in:
yangzhouhan 2015-06-16 12:16:25 -07:00
parent 89311abb67
commit dbf1048b08
4 changed files with 160 additions and 8 deletions

28
health/health.go Normal file
View File

@ -0,0 +1,28 @@
package health
//import proto "github.com/golang/protobuf/proto"
import (
"time"
"golang.org/x/net/context"
"google.golang.org/grpc"
)
func HealthCheck(t time.Duration, cc *grpc.ClientConn, req interface{}, rep interface{}) error {
ctx, _ := context.WithTimeout(context.Background(), t)
//out :=
if err := grpc.Invoke(ctx, "health.HealthCheck/HealthCheck", req, rep, cc); err != nil {
return err
}
return nil
}
type HealthServer struct {
}
func (s *HealthServer) HealthCheck(ctx context.Context, in *HealthCheckRequest)(*HealthCheckResponse, error){
out := new(HealthCheckResponse)
return out,nil
}

103
health/health.pb.go Normal file
View File

@ -0,0 +1,103 @@
// Code generated by protoc-gen-go.
// source: health.proto
// DO NOT EDIT!
/*
Package health is a generated protocol buffer package.
It is generated from these files:
health.proto
It has these top-level messages:
HealthCheckRequest
HealthCheckResponse
*/
package health
import proto "github.com/golang/protobuf/proto"
import (
context "golang.org/x/net/context"
grpc "google.golang.org/grpc"
)
// Reference imports to suppress errors if they are not otherwise used.
var _ context.Context
var _ grpc.ClientConn
// Reference imports to suppress errors if they are not otherwise used.
var _ = proto.Marshal
type HealthCheckRequest struct {
}
func (m *HealthCheckRequest) Reset() { *m = HealthCheckRequest{} }
func (m *HealthCheckRequest) String() string { return proto.CompactTextString(m) }
func (*HealthCheckRequest) ProtoMessage() {}
type HealthCheckResponse struct {
}
func (m *HealthCheckResponse) Reset() { *m = HealthCheckResponse{} }
func (m *HealthCheckResponse) String() string { return proto.CompactTextString(m) }
func (*HealthCheckResponse) ProtoMessage() {}
func init() {
}
// Client API for HealthCheck service
type HealthCheckClient interface {
HealthCheck(ctx context.Context, in *HealthCheckRequest, opts ...grpc.CallOption) (*HealthCheckResponse, error)
}
type healthCheckClient struct {
cc *grpc.ClientConn
}
func NewHealthCheckClient(cc *grpc.ClientConn) HealthCheckClient {
return &healthCheckClient{cc}
}
func (c *healthCheckClient) HealthCheck(ctx context.Context, in *HealthCheckRequest, opts ...grpc.CallOption) (*HealthCheckResponse, error) {
out := new(HealthCheckResponse)
err := grpc.Invoke(ctx, "/health.HealthCheck/HealthCheck", in, out, c.cc, opts...)
if err != nil {
return nil, err
}
return out, nil
}
// Server API for HealthCheck service
type HealthCheckServer interface {
HealthCheck(context.Context, *HealthCheckRequest) (*HealthCheckResponse, error)
}
func RegisterHealthCheckServer(s *grpc.Server, srv HealthCheckServer) {
s.RegisterService(&_HealthCheck_serviceDesc, srv)
}
func _HealthCheck_HealthCheck_Handler(srv interface{}, ctx context.Context, codec grpc.Codec, buf []byte) (interface{}, error) {
in := new(HealthCheckRequest)
if err := codec.Unmarshal(buf, in); err != nil {
return nil, err
}
out, err := srv.(HealthCheckServer).HealthCheck(ctx, in)
if err != nil {
return nil, err
}
return out, nil
}
var _HealthCheck_serviceDesc = grpc.ServiceDesc{
ServiceName: "health.HealthCheck",
HandlerType: (*HealthCheckServer)(nil),
Methods: []grpc.MethodDesc{
{
MethodName: "HealthCheck",
Handler: _HealthCheck_HealthCheck_Handler,
},
},
Streams: []grpc.StreamDesc{},
}

13
health/health.proto Normal file
View File

@ -0,0 +1,13 @@
syntax = "proto3";
package health;
message HealthCheckRequest{
}
message HealthCheckResponse{
}
service HealthCheck{
rpc HealthCheck( HealthCheckRequest) returns ( HealthCheckResponse);
}

View File

@ -53,6 +53,7 @@ import (
"google.golang.org/grpc/grpclog"
"google.golang.org/grpc/metadata"
testpb "google.golang.org/grpc/test/grpc_testing"
"google.golang.org/grpc/health"
)
var (
@ -304,7 +305,8 @@ func setUp(healthCheck bool, maxStream uint32, e env) (s *grpc.Server, cc *grpc.
}
s = grpc.NewServer(sopts...)
if healthCheck {
grpc.Enable(s)
health.RegisterHealthCheckServer(s,&health.HealthServer{})
}
testpb.RegisterTestServiceServer(s, &testServer{})
go s.Serve(lis)
@ -367,8 +369,10 @@ func TestHealthCheckOnSucceed(t *testing.T) {
func testHealthCheckOnSucceed(t *testing.T, e env) {
s, cc := setUp(true, math.MaxUint32, e)
defer tearDown(s, cc)
in := new(grpc.HealthCheckRequest)
if err := grpc.Check(1*time.Second, cc, in); err != nil {
in := new(health.HealthCheckRequest)
out := new(health.HealthCheckResponse)
//hc := health.NewHealthCheckMessage()
if err := health.HealthCheck(1*time.Second, cc, in, out); err != nil {
t.Fatalf("HealthCheck(_)=_, %v, want <nil>", err)
}
}
@ -382,8 +386,10 @@ func TestHealthCheckOnFailure(t *testing.T) {
func testHealthCheckOnFailure(t *testing.T, e env) {
s, cc := setUp(true, math.MaxUint32, e)
defer tearDown(s, cc)
in := new(grpc.HealthCheckRequest)
if err := grpc.Check(0*time.Second, cc, in); err != grpc.Errorf(codes.DeadlineExceeded, "context deadline exceeded") {
in := new(health.HealthCheckRequest)
out := new(health.HealthCheckResponse)
//hc := health.NewHealthCheckMessage()
if err := health.HealthCheck(0*time.Second, cc, in, out); err != grpc.Errorf(codes.DeadlineExceeded, "context deadline exceeded") {
t.Fatalf("HealthCheck(_)=_, %v, want <DeadlineExcced>", err)
}
}
@ -397,9 +403,11 @@ func TestHealthCheckOff(t *testing.T) {
func testHealthCheckOff(t *testing.T, e env) {
s, cc := setUp(false, math.MaxUint32, e)
defer tearDown(s, cc)
in := new(grpc.HealthCheckRequest)
err := grpc.Check(1 * time.Second, cc, in)
if err != grpc.Errorf(codes.Unimplemented, "unknown service grpc.HealthCheck") {
in := new(health.HealthCheckRequest)
out := new(health.HealthCheckResponse)
//hc :=health.NewHealthCheckMessage()
err := health.HealthCheck(1 * time.Second, cc, in, out)
if err != grpc.Errorf(codes.Unimplemented, "unknown service health.HealthCheck") {
t.Fatalf("HealthCheck(_)=_, %v, want <unimplemented>", err)
}
}