Add grpc check example. (#306)
* Add grpc check example. Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com> * add missing package. Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
This commit is contained in:
@@ -2,7 +2,6 @@ package rpc
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/appleboy/gorush/rpc/proto"
|
||||
|
||||
@@ -13,8 +12,6 @@ import (
|
||||
// generate protobuffs
|
||||
// protoc --go_out=plugins=grpc,import_path=proto:. *.proto
|
||||
|
||||
var backoff = time.Second
|
||||
|
||||
type healthClient struct {
|
||||
client proto.HealthClient
|
||||
conn *grpc.ClientConn
|
||||
@@ -37,25 +34,24 @@ func (c *healthClient) Check(ctx context.Context) (bool, error) {
|
||||
var err error
|
||||
req := new(proto.HealthCheckRequest)
|
||||
|
||||
for {
|
||||
res, err = c.client.Check(ctx, req)
|
||||
if err == nil {
|
||||
if res.GetStatus() == proto.HealthCheckResponse_SERVING {
|
||||
return true, nil
|
||||
}
|
||||
return false, nil
|
||||
res, err = c.client.Check(ctx, req)
|
||||
if err == nil {
|
||||
if res.GetStatus() == proto.HealthCheckResponse_SERVING {
|
||||
return true, nil
|
||||
}
|
||||
switch grpc.Code(err) {
|
||||
case
|
||||
codes.Aborted,
|
||||
codes.DataLoss,
|
||||
codes.DeadlineExceeded,
|
||||
codes.Internal,
|
||||
codes.Unavailable:
|
||||
// non-fatal errors
|
||||
default:
|
||||
return false, err
|
||||
}
|
||||
<-time.After(backoff)
|
||||
return false, nil
|
||||
}
|
||||
switch grpc.Code(err) {
|
||||
case
|
||||
codes.Aborted,
|
||||
codes.DataLoss,
|
||||
codes.DeadlineExceeded,
|
||||
codes.Internal,
|
||||
codes.Unavailable:
|
||||
// non-fatal errors
|
||||
default:
|
||||
return false, err
|
||||
}
|
||||
|
||||
return false, err
|
||||
}
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"github.com/appleboy/gorush/rpc/proto"
|
||||
"github.com/appleboy/gorush/rpc"
|
||||
|
||||
"golang.org/x/net/context"
|
||||
"google.golang.org/grpc"
|
||||
)
|
||||
|
||||
@@ -21,10 +22,16 @@ func main() {
|
||||
}
|
||||
defer conn.Close()
|
||||
|
||||
c := proto.NewHealthClient(conn)
|
||||
r, err := c.Check(context.Background(), &proto.HealthCheckRequest{})
|
||||
if err != nil {
|
||||
log.Fatalf("could not greet: %v", err)
|
||||
client := rpc.NewGrpcHealthClient(conn)
|
||||
|
||||
for {
|
||||
ok, err := client.Check(context.Background())
|
||||
if !ok || err != nil {
|
||||
log.Printf("can't connect grpc server: %v, code: %v\n", err, grpc.Code(err))
|
||||
} else {
|
||||
log.Println("connect the grpc server successfully")
|
||||
}
|
||||
|
||||
<-time.After(time.Second)
|
||||
}
|
||||
log.Printf("Health: %d\n", r.GetStatus())
|
||||
}
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
|
||||
"github.com/appleboy/gorush/rpc/proto"
|
||||
|
||||
"golang.org/x/net/context"
|
||||
"google.golang.org/grpc"
|
||||
)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user