From d67c1ba111cbe4352fd4cb36b189d09b8ef8aa34 Mon Sep 17 00:00:00 2001 From: Asim Date: Thu, 21 May 2015 23:06:01 +0100 Subject: [PATCH] Fix health checker --- cmd/cmd.go | 3 +++ proto/health/health.pb.go | 38 ++++++++++++++++++++++++++++++++++++++ proto/health/health.proto | 8 ++++++++ server/health_checker.go | 26 +++++++++++--------------- server/rpc_server.go | 3 +-- 5 files changed, 61 insertions(+), 17 deletions(-) create mode 100644 proto/health/health.pb.go create mode 100644 proto/health/health.proto diff --git a/cmd/cmd.go b/cmd/cmd.go index 72b80c7c..17acd2ca 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -9,6 +9,7 @@ import ( "github.com/codegangsta/cli" "github.com/myodc/go-micro/broker" + "github.com/myodc/go-micro/client" "github.com/myodc/go-micro/registry" "github.com/myodc/go-micro/server" "github.com/myodc/go-micro/store" @@ -113,6 +114,8 @@ func Setup(c *cli.Context) error { transport.DefaultTransport = transport.NewNatsTransport(tAddrs) } + client.DefaultClient = client.NewRpcClient() + return nil } diff --git a/proto/health/health.pb.go b/proto/health/health.pb.go new file mode 100644 index 00000000..c5c6b415 --- /dev/null +++ b/proto/health/health.pb.go @@ -0,0 +1,38 @@ +// Code generated by protoc-gen-go. +// source: github.com/myodc/go-micro/proto/health/health.proto +// DO NOT EDIT! + +/* +Package health is a generated protocol buffer package. + +It is generated from these files: + github.com/myodc/go-micro/proto/health/health.proto + +It has these top-level messages: + Request + Response +*/ +package health + +import proto "github.com/golang/protobuf/proto" + +// Reference imports to suppress errors if they are not otherwise used. +var _ = proto.Marshal + +type Request struct { +} + +func (m *Request) Reset() { *m = Request{} } +func (m *Request) String() string { return proto.CompactTextString(m) } +func (*Request) ProtoMessage() {} + +type Response struct { + Status string `protobuf:"bytes,1,opt,name=status" json:"status,omitempty"` +} + +func (m *Response) Reset() { *m = Response{} } +func (m *Response) String() string { return proto.CompactTextString(m) } +func (*Response) ProtoMessage() {} + +func init() { +} diff --git a/proto/health/health.proto b/proto/health/health.proto new file mode 100644 index 00000000..34dcaf6e --- /dev/null +++ b/proto/health/health.proto @@ -0,0 +1,8 @@ +syntax = "proto3"; + +message Request { +} + +message Response { + string status = 1; +} diff --git a/server/health_checker.go b/server/health_checker.go index 612aaa58..1c1462ab 100644 --- a/server/health_checker.go +++ b/server/health_checker.go @@ -1,21 +1,17 @@ package server import ( - "io" - "net/http" - "net/url" + "github.com/myodc/go-micro/proto/health" + "golang.org/x/net/context" ) -func registerHealthChecker(mux *http.ServeMux) { - req := &http.Request{ - Method: "GET", - URL: &url.URL{ - Path: HealthPath, - }, - } - if _, path := mux.Handler(req); path != HealthPath { - mux.HandleFunc(HealthPath, func(w http.ResponseWriter, r *http.Request) { - io.WriteString(w, "ok") - }) - } +type Debug struct{} + +func (d *Debug) Health(ctx context.Context, req *health.Request, rsp *health.Response) error { + rsp.Status = "ok" + return nil +} + +func registerHealthChecker(r Server) { + r.Register(r.NewReceiver(&Debug{})) } diff --git a/server/rpc_server.go b/server/rpc_server.go index 9955c8ea..b5184b83 100644 --- a/server/rpc_server.go +++ b/server/rpc_server.go @@ -2,7 +2,6 @@ package server import ( "bytes" - "net/http" "sync" log "github.com/golang/glog" @@ -97,7 +96,7 @@ func (s *RpcServer) Register(r Receiver) error { } func (s *RpcServer) Start() error { - registerHealthChecker(http.DefaultServeMux) + registerHealthChecker(s) ts, err := s.opts.transport.Listen(s.address) if err != nil {