Apply .gitignore rules
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
package client
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/credentials/insecure"
|
||||
)
|
||||
|
||||
var (
|
||||
// 缓存连接 key:rpc地址 value:连接指针对象
|
||||
rpcConMap sync.Map
|
||||
|
||||
// 连接超时时间
|
||||
defaultTimeOut = time.Second * 1
|
||||
)
|
||||
|
||||
// GetClientConn
|
||||
// @description: 获取grpc连接对象
|
||||
// parameter:
|
||||
// @rpcAddres:连接地址
|
||||
// return:
|
||||
// @*grpc.ClientConn:连接对象
|
||||
// @error:错误对象
|
||||
func GetClientConn(rpcAddres string) (*grpc.ClientConn, error) {
|
||||
// 从缓存获取
|
||||
if con1, isok := rpcConMap.Load(rpcAddres); isok {
|
||||
return con1.(*grpc.ClientConn), nil
|
||||
}
|
||||
|
||||
// 构造新对象
|
||||
//ctx, _ := context.WithDeadline(context.Background(), time.Now().Add(time.Second*1))
|
||||
var ctx, _ = context.WithTimeout(context.Background(), defaultTimeOut)
|
||||
con, err := grpc.DialContext(ctx, rpcAddres, grpc.WithTransportCredentials(insecure.NewCredentials()))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 加入缓存
|
||||
rpcConMap.Store(rpcAddres, con)
|
||||
|
||||
return con, nil
|
||||
}
|
||||
|
||||
// ClearClientConn
|
||||
// @description: 根据连接地址,删除缓存的连接对象
|
||||
// parameter:
|
||||
// @rpcAddres:连接地址
|
||||
// return:
|
||||
func ClearClientConn(rpcAddres string) {
|
||||
rpcConMap.Delete(rpcAddres)
|
||||
}
|
||||
|
||||
// SetDefaultTimeOut
|
||||
// @description: 设置连接的默认超时
|
||||
// parameter:
|
||||
// @outTime:超时时间
|
||||
// return:
|
||||
func SetDefaultTimeOut(outTime time.Duration) {
|
||||
defaultTimeOut = outTime
|
||||
}
|
||||
Reference in New Issue
Block a user