代码拉取完成,页面将自动刷新
package rpc
import (
"context"
"fmt"
"gitee.com/h79/goutils/alarm"
"gitee.com/h79/goutils/common/logger"
"gitee.com/h79/goutils/common/result"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/health/grpc_health_v1"
"sync"
"time"
)
func NewClient(addr string, opts ...grpc.DialOption) (*grpc.ClientConn, error) {
logger.D("rpc", "NewClient, addr= %s", addr)
var dialOpts []grpc.DialOption
if len(opts) <= 0 {
dialOpts = append(dialOpts, grpc.WithTransportCredentials(insecure.NewCredentials()))
} else {
dialOpts = append(dialOpts, opts...)
}
conn, err := grpc.Dial(addr, dialOpts...)
if err != nil {
return nil, result.Errorf(result.ErrClientConnectInternal, "did not connect failure, err= %v", err).Log()
}
return conn, nil
}
func Close(client *grpc.ClientConn) {
if client != nil {
_ = client.Close()
}
}
type Client struct {
Config
health HealthClient
conn *grpc.ClientConn
rm sync.Mutex
}
func (cli *Client) CreateConnect(opts ...grpc.DialOption) (*grpc.ClientConn, error) {
cli.rm.Lock()
defer cli.rm.Unlock()
err := cli.connect(opts...)
return cli.conn, err
}
func (cli *Client) connect(opts ...grpc.DialOption) error {
if cli.conn != nil {
return nil
}
conn, err := NewClient(cli.Server, opts...)
if err != nil {
return err
}
cli.conn = conn
return nil
}
func (cli *Client) Close() {
cli.close(cli.conn)
}
func (cli *Client) close(conn *grpc.ClientConn) {
var c = conn
cli.rm.Lock()
if conn == cli.conn {
c = cli.conn
cli.conn = nil
}
cli.rm.Unlock()
Close(c)
}
func (cli *Client) CheckHealth() (int, error) {
start := time.Now()
conn, err := cli.CreateConnect()
if err != nil {
return 0, err
}
// Set up a connection to the server.
if conn == nil {
return int(grpc_health_v1.HealthCheckResponse_UNKNOWN), result.RErrNil
}
rsp, err := cli.health.Check(conn, cli.Service, cli.Timeout)
if err != nil {
const method = "Check"
return rsp, cli.HandlerError(conn, err, start, method)
}
return rsp, nil
}
func (cli *Client) WatchHealth() (grpc_health_v1.Health_WatchClient, error) {
start := time.Now()
conn, err := cli.CreateConnect()
if err != nil {
return nil, err
}
// Set up a connection to the server.
if conn == nil {
return nil, result.RErrNil
}
rsp, err := cli.health.Watch(conn, cli.Service, cli.Timeout)
if err != nil {
const method = "Watch"
return nil, cli.HandlerError(conn, err, start, method)
}
return rsp, nil
}
// HandlerError "State":"IDLE","Status":"rpc error: code = Unknown desc = rpc error: code = DeadlineExceeded desc = context deadline exceeded"}
func (cli *Client) HandlerError(conn *grpc.ClientConn, err error, start time.Time, method string) error {
st := WithStatus(err)
if st != nil {
logger.E("rpc", "HandlerError connState= %s, status= %s", conn.GetState().String(), st.String())
if IsNeedAlarm(st) {
cli.close(conn)
cli.Alarm(method, time.Now().Sub(start), err)
}
if IsDeadlineExceeded(st) {
return result.ErrCode(result.ErrDeadlineExceeded).WithError(err)
}
}
return err
}
func (cli *Client) Alarm(method string, latency time.Duration, err error) {
alarm.Fatal(context.Background(), -1, "rpc", cli.Title, fmt.Sprintf("method= '%s',url= '%s',latency= '%v'", method, cli.Server, latency), err)
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。