Files
unitech-golib/gfetcd/config.go
2020-04-06 19:57:19 +08:00

40 lines
961 B
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package gfetcd
import (
"path"
"time"
"github.com/coreos/etcd/clientv3"
)
const (
DEFAULT_DIAL_TIMEOUT = time.Second * 5
DEFAULT_LEASE_TTL = 5
)
// 基础配置,公共字段
type BasicConfig struct {
DialTimeout int `toml:"dial_timeout"` // 连接超时时间
EndPoints []string `toml:"endpoints"` // etcd的地址
Prefix string `toml:"prefix"` // etcd服务的路径前缀区分环境用
Debug bool `toml:"debug"` // 使用debug模式
}
func etcdDial(cfg *BasicConfig) (*clientv3.Client, error) {
dialTimeout := DEFAULT_DIAL_TIMEOUT
if cfg.DialTimeout > 0 {
dialTimeout = time.Duration(cfg.DialTimeout) * time.Second
}
return clientv3.New(clientv3.Config{
Endpoints: cfg.EndPoints,
DialTimeout: dialTimeout,
DialKeepAliveTime: 2 * time.Second,
DialKeepAliveTimeout: 2 * time.Second,
})
}
func ServiceKey(opts ...string) string {
return path.Join(opts...)
}