2017-11-12 14:44:33 +00:00
|
|
|
package rpc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2022-11-09 06:07:51 +00:00
|
|
|
"github.com/appleboy/gorush/core"
|
2017-11-12 14:44:33 +00:00
|
|
|
"github.com/appleboy/gorush/rpc/proto"
|
|
|
|
|
|
|
|
"google.golang.org/grpc"
|
|
|
|
"google.golang.org/grpc/codes"
|
2021-08-03 06:44:00 +00:00
|
|
|
"google.golang.org/grpc/status"
|
2017-11-12 14:44:33 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// generate protobuffs
|
|
|
|
// protoc --go_out=plugins=grpc,import_path=proto:. *.proto
|
|
|
|
|
|
|
|
type healthClient struct {
|
|
|
|
client proto.HealthClient
|
|
|
|
conn *grpc.ClientConn
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewGrpcHealthClient returns a new grpc Client.
|
2022-11-09 06:07:51 +00:00
|
|
|
func NewGrpcHealthClient(conn *grpc.ClientConn) core.Health {
|
2017-11-12 14:44:33 +00:00
|
|
|
client := new(healthClient)
|
|
|
|
client.client = proto.NewHealthClient(conn)
|
|
|
|
client.conn = conn
|
|
|
|
return client
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *healthClient) Close() error {
|
|
|
|
return c.conn.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *healthClient) Check(ctx context.Context) (bool, error) {
|
|
|
|
var res *proto.HealthCheckResponse
|
|
|
|
var err error
|
|
|
|
req := new(proto.HealthCheckRequest)
|
|
|
|
|
2017-11-14 06:18:48 +00:00
|
|
|
res, err = c.client.Check(ctx, req)
|
|
|
|
if err == nil {
|
|
|
|
if res.GetStatus() == proto.HealthCheckResponse_SERVING {
|
|
|
|
return true, nil
|
2017-11-12 14:44:33 +00:00
|
|
|
}
|
2017-11-14 06:18:48 +00:00
|
|
|
return false, nil
|
2017-11-12 14:44:33 +00:00
|
|
|
}
|
2022-12-20 14:03:29 +00:00
|
|
|
//nolint:exhaustive
|
2021-08-03 06:44:00 +00:00
|
|
|
switch status.Code(err) {
|
2017-11-14 06:18:48 +00:00
|
|
|
case
|
|
|
|
codes.Aborted,
|
|
|
|
codes.DataLoss,
|
|
|
|
codes.DeadlineExceeded,
|
|
|
|
codes.Internal,
|
|
|
|
codes.Unavailable:
|
|
|
|
// non-fatal errors
|
|
|
|
default:
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return false, err
|
2017-11-12 14:44:33 +00:00
|
|
|
}
|