This commit is contained in:
yangzhouhan 2015-08-13 15:16:59 -07:00
Родитель 20386cb7c5
Коммит 5a78be3dbb
4 изменённых файлов: 10 добавлений и 14 удалений

Просмотреть файл

@ -21,10 +21,6 @@ import (
grpc "google.golang.org/grpc" 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. // Reference imports to suppress errors if they are not otherwise used.
var _ = proto.Marshal var _ = proto.Marshal
@ -52,7 +48,6 @@ func (x HealthCheckResponse_ServingStatus) String() string {
} }
type HealthCheckRequest struct { type HealthCheckRequest struct {
Host string `protobuf:"bytes,1,opt,name=host" json:"host,omitempty"`
Service string `protobuf:"bytes,2,opt,name=service" json:"service,omitempty"` Service string `protobuf:"bytes,2,opt,name=service" json:"service,omitempty"`
} }
@ -72,6 +67,10 @@ func init() {
proto.RegisterEnum("grpc.health.v1alpha.HealthCheckResponse_ServingStatus", HealthCheckResponse_ServingStatus_name, HealthCheckResponse_ServingStatus_value) proto.RegisterEnum("grpc.health.v1alpha.HealthCheckResponse_ServingStatus", HealthCheckResponse_ServingStatus_name, HealthCheckResponse_ServingStatus_value)
} }
// Reference imports to suppress errors if they are not otherwise used.
var _ context.Context
var _ grpc.ClientConn
// Client API for HealthCheck service // Client API for HealthCheck service
type HealthCheckClient interface { type HealthCheckClient interface {

Просмотреть файл

@ -3,7 +3,6 @@ syntax = "proto3";
package grpc.health.v1alpha; package grpc.health.v1alpha;
message HealthCheckRequest { message HealthCheckRequest {
string host = 1;
string service = 2; string service = 2;
} }

Просмотреть файл

@ -24,7 +24,7 @@ func NewHealthServer() *HealthServer {
} }
func (s *HealthServer) Check(ctx context.Context, in *healthpb.HealthCheckRequest) (*healthpb.HealthCheckResponse, error) { func (s *HealthServer) Check(ctx context.Context, in *healthpb.HealthCheckRequest) (*healthpb.HealthCheckResponse, error) {
service := in.Host + ":" + in.Service service := in.Service
s.mu.Lock() s.mu.Lock()
defer s.mu.Unlock() defer s.mu.Unlock()
if status, ok := s.statusMap[service]; ok { if status, ok := s.statusMap[service]; ok {
@ -37,8 +37,7 @@ func (s *HealthServer) Check(ctx context.Context, in *healthpb.HealthCheckReques
// SetServingStatus is called when need to reset the serving status of a service // SetServingStatus is called when need to reset the serving status of a service
// or insert a new service entry into the statusMap. // or insert a new service entry into the statusMap.
func (s *HealthServer) SetServingStatus(host string, service string, status healthpb.HealthCheckResponse_ServingStatus) { func (s *HealthServer) SetServingStatus(service string, status healthpb.HealthCheckResponse_ServingStatus) {
service = host + ":" + service
s.mu.Lock() s.mu.Lock()
s.statusMap[service] = status s.statusMap[service] = status
s.mu.Unlock() s.mu.Unlock()

Просмотреть файл

@ -392,7 +392,6 @@ func healthCheck(t time.Duration, cc *grpc.ClientConn, serviceName string) (*hea
ctx, _ := context.WithTimeout(context.Background(), t) ctx, _ := context.WithTimeout(context.Background(), t)
hc := healthpb.NewHealthCheckClient(cc) hc := healthpb.NewHealthCheckClient(cc)
req := &healthpb.HealthCheckRequest{ req := &healthpb.HealthCheckRequest{
Host: "",
Service: serviceName, Service: serviceName,
} }
return hc.Check(ctx, req) return hc.Check(ctx, req)
@ -406,7 +405,7 @@ func TestHealthCheckOnSuccess(t *testing.T) {
func testHealthCheckOnSuccess(t *testing.T, e env) { func testHealthCheckOnSuccess(t *testing.T, e env) {
hs := health.NewHealthServer() hs := health.NewHealthServer()
hs.SetServingStatus("", "grpc.health.v1alpha.HealthCheck", 1) hs.SetServingStatus("grpc.health.v1alpha.HealthCheck", 1)
s, cc := setUp(hs, math.MaxUint32, "", e) s, cc := setUp(hs, math.MaxUint32, "", e)
defer tearDown(s, cc) defer tearDown(s, cc)
if _, err := healthCheck(1*time.Second, cc, "grpc.health.v1alpha.HealthCheck"); err != nil { if _, err := healthCheck(1*time.Second, cc, "grpc.health.v1alpha.HealthCheck"); err != nil {
@ -422,7 +421,7 @@ func TestHealthCheckOnFailure(t *testing.T) {
func testHealthCheckOnFailure(t *testing.T, e env) { func testHealthCheckOnFailure(t *testing.T, e env) {
hs := health.NewHealthServer() hs := health.NewHealthServer()
hs.SetServingStatus("", "grpc.health.v1alpha.HealthCheck", 1) hs.SetServingStatus("grpc.health.v1alpha.HealthCheck", 1)
s, cc := setUp(hs, math.MaxUint32, "", e) s, cc := setUp(hs, math.MaxUint32, "", e)
defer tearDown(s, cc) defer tearDown(s, cc)
if _, err := healthCheck(0*time.Second, cc, "grpc.health.v1alpha.HealthCheck"); err != grpc.Errorf(codes.DeadlineExceeded, "context deadline exceeded") { if _, err := healthCheck(0*time.Second, cc, "grpc.health.v1alpha.HealthCheck"); err != grpc.Errorf(codes.DeadlineExceeded, "context deadline exceeded") {
@ -457,7 +456,7 @@ func testHealthCheckServingStatus(t *testing.T, e env) {
if _, err := healthCheck(1*time.Second, cc, "grpc.health.v1alpha.HealthCheck"); err != grpc.Errorf(codes.NotFound, "unknown service") { if _, err := healthCheck(1*time.Second, cc, "grpc.health.v1alpha.HealthCheck"); err != grpc.Errorf(codes.NotFound, "unknown service") {
t.Fatalf("HealthCheck/Check(_, _) = _, %v, want _, error code %d", err, codes.NotFound) t.Fatalf("HealthCheck/Check(_, _) = _, %v, want _, error code %d", err, codes.NotFound)
} }
hs.SetServingStatus("", "grpc.health.v1alpha.HealthCheck", healthpb.HealthCheckResponse_SERVING) hs.SetServingStatus("grpc.health.v1alpha.HealthCheck", healthpb.HealthCheckResponse_SERVING)
out, err := healthCheck(1*time.Second, cc, "grpc.health.v1alpha.HealthCheck") out, err := healthCheck(1*time.Second, cc, "grpc.health.v1alpha.HealthCheck")
if err != nil { if err != nil {
t.Fatalf("HealthCheck/Check(_, _) = _, %v, want _, <nil>", err) t.Fatalf("HealthCheck/Check(_, _) = _, %v, want _, <nil>", err)
@ -465,7 +464,7 @@ func testHealthCheckServingStatus(t *testing.T, e env) {
if out.Status != healthpb.HealthCheckResponse_SERVING { if out.Status != healthpb.HealthCheckResponse_SERVING {
t.Fatalf("Got the serving status %v, want SERVING", out.Status) t.Fatalf("Got the serving status %v, want SERVING", out.Status)
} }
hs.SetServingStatus("", "grpc.health.v1alpha.HealthCheck", healthpb.HealthCheckResponse_NOT_SERVING) hs.SetServingStatus("grpc.health.v1alpha.HealthCheck", healthpb.HealthCheckResponse_NOT_SERVING)
out, err = healthCheck(1*time.Second, cc, "grpc.health.v1alpha.HealthCheck") out, err = healthCheck(1*time.Second, cc, "grpc.health.v1alpha.HealthCheck")
if err != nil { if err != nil {
t.Fatalf("HealthCheck/Check(_, _) = _, %v, want _, <nil>", err) t.Fatalf("HealthCheck/Check(_, _) = _, %v, want _, <nil>", err)