1 Star 0 Fork 0

linxing/youye-core

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
service.go 1.89 KB
一键复制 编辑 原始数据 按行查看 历史
Mark 提交于 2024-01-29 17:39 +08:00 . rename package
/*
* @Author: lwnmengjing
* @Date: 2021/6/8 5:29 下午
* @Last Modified by: lwnmengjing
* @Last Modified time: 2021/6/8 5:29 下午
*/
package grpc
import (
"context"
"fmt"
"time"
log "gitee.com/linxing_3/youye-core/logger"
"gitee.com/linxing_3/youye-core/server/grpc/interceptors/logging"
reqtags "gitee.com/linxing_3/youye-core/server/grpc/interceptors/request_tag"
middleware "github.com/grpc-ecosystem/go-grpc-middleware"
opentracing "github.com/grpc-ecosystem/go-grpc-middleware/tracing/opentracing"
"google.golang.org/grpc"
)
type Service struct {
Connection *grpc.ClientConn
CallTimeout time.Duration
}
func (e *Service) Dial(
endpoint string,
callTimeout time.Duration,
unary ...grpc.UnaryClientInterceptor) (err error) {
log.Infof("configure service with endpoint: %s", endpoint)
ctx, cancel := context.WithTimeout(context.Background(), callTimeout)
defer cancel()
if len(unary) == 0 {
unary = defaultUnaryClientInterceptors()
}
e.Connection, err = grpc.DialContext(ctx,
endpoint,
grpc.WithInsecure(),
grpc.WithStreamInterceptor(middleware.ChainStreamClient(defaultStreamClientInterceptors()...)),
grpc.WithUnaryInterceptor(middleware.ChainUnaryClient(unary...)),
grpc.WithDefaultCallOptions(grpc.WaitForReady(true), grpc.MaxCallRecvMsgSize(defaultMaxMsgSize)),
)
if err != nil {
msg := fmt.Sprintf("connect gRPC service %s failed", endpoint)
log.Errorf(msg, err)
return fmt.Errorf("%w, "+msg, err)
}
return nil
}
func defaultUnaryClientInterceptors() []grpc.UnaryClientInterceptor {
return []grpc.UnaryClientInterceptor{
opentracing.UnaryClientInterceptor(),
logging.UnaryClientInterceptor(),
reqtags.UnaryClientInterceptor(),
}
}
func defaultStreamClientInterceptors() []grpc.StreamClientInterceptor {
return []grpc.StreamClientInterceptor{
opentracing.StreamClientInterceptor(),
logging.StreamClientInterceptor(),
reqtags.StreamClientInterceptor(),
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/linxing_3/youye-core.git
git@gitee.com:linxing_3/youye-core.git
linxing_3
youye-core
youye-core
v0.0.1

搜索帮助