feat 初始化
This commit is contained in:
@@ -1,11 +1,14 @@
|
||||
package grpcconn
|
||||
|
||||
import (
|
||||
"context"
|
||||
"git.hlsq.asia/alex/service-common/log"
|
||||
"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/credentials/insecure"
|
||||
"google.golang.org/grpc/keepalive"
|
||||
"google.golang.org/grpc/metadata"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
@@ -22,6 +25,7 @@ func NewGrpcConnection(sid int64, address string) (*GrpcConnection, error) {
|
||||
address,
|
||||
grpc.WithTransportCredentials(insecure.NewCredentials()),
|
||||
grpc.WithStatsHandler(otelgrpc.NewClientHandler()),
|
||||
grpc.WithUnaryInterceptor(sidInterceptor(sid)),
|
||||
grpc.WithKeepaliveParams(
|
||||
keepalive.ClientParameters{
|
||||
Time: 30 * time.Second, // 保活探测包发送的时间间隔
|
||||
@@ -41,3 +45,13 @@ func NewGrpcConnection(sid int64, address string) (*GrpcConnection, error) {
|
||||
func (g *GrpcConnection) GetConnection() *grpc.ClientConn {
|
||||
return g.conn
|
||||
}
|
||||
|
||||
func sidInterceptor(sid int64) grpc.UnaryClientInterceptor {
|
||||
sidStr := strconv.Itoa(int(sid))
|
||||
return func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
|
||||
ctx = metadata.AppendToOutgoingContext(ctx,
|
||||
"X-Sid", sidStr,
|
||||
)
|
||||
return invoker(ctx, method, req, reply, cc, opts...)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -60,3 +60,11 @@ func (p *GrpcConnectionMgr) LoadAll() map[int64]*grpc.ClientConn {
|
||||
}
|
||||
return sidM
|
||||
}
|
||||
|
||||
func (p *GrpcConnectionMgr) LoadAllSid() []int64 {
|
||||
sidArr := make([]int64, 0)
|
||||
for sid := range p.poolM {
|
||||
sidArr = append(sidArr, sid)
|
||||
}
|
||||
return sidArr
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user