gopkg/middleware/etcd/init.go

60 lines
1.2 KiB
Go
Raw Normal View History

2021-11-23 11:58:13 +08:00
// Package etcd ...
//
// Description : etcd ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2021-11-23 10:53 上午
package etcd
import (
"github.com/pkg/errors"
"go.etcd.io/etcd/clientv3"
)
var (
// Client 客户端
Client *clientv3.Client
)
// InitEtcdClient 初始化etcd连接
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 10:53 上午 2021/11/23
func InitEtcdClient(cfg clientv3.Config) error {
if nil == cfg.Endpoints || len(cfg.Endpoints) == 0 {
return errors.New("endpoints is empty")
}
// 连接超时
if cfg.DialTimeout == 0 {
cfg.DialTimeout = DefaultDialTimeout
}
// ping 时间间隔
if cfg.DialKeepAliveTime == 0 {
cfg.DialKeepAliveTime = DefaultDialKeepAliveTime
}
// ping 响应超时时间
if cfg.DialKeepAliveTimeout == 0 {
cfg.DialKeepAliveTimeout = DefaultDialKeepAliveTimeout
}
// 客户端发送的最大请求体大小
if cfg.MaxCallSendMsgSize <= 0 {
cfg.MaxCallSendMsgSize = DefaultMaxCallSendMsgSize
}
// 客户端接受的最大响应体大小
if cfg.MaxCallRecvMsgSize <= 0 {
cfg.MaxCallRecvMsgSize = DefaultMaxCallRecvMsgSize
}
var err error
if Client, err = clientv3.New(cfg); nil != err {
return errors.New("etcd连接失败 : " + err.Error())
}
return nil
}