Ai
1 Star 0 Fork 0

monobytes/gcore

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
options.go 3.05 KB
一键复制 编辑 原始数据 按行查看 历史
null 提交于 2025-01-22 18:29 +08:00 . first commit
package etcd
import (
"context"
"gitee.com/monobytes/gcore/getc"
clientv3 "go.etcd.io/etcd/client/v3"
"time"
)
const (
defaultAddr = "127.0.0.1:2379"
defaultDialTimeout = "5s"
defaultNamespace = "services"
defaultTimeout = "3s"
defaultRetryTimes = 3
defaultRetryInterval = "10s"
)
const (
defaultAddrsKey = "etc.registry.etcd.addrs"
defaultDialTimeoutKey = "etc.registry.etcd.dialTimeout"
defaultNamespaceKey = "etc.registry.etcd.namespace"
defaultTimeoutKey = "etc.registry.etcd.timeout"
defaultRetryTimesKey = "etc.registry.etcd.retryTimes"
defaultRetryIntervalKey = "etc.registry.etcd.retryInterval"
)
type Option func(o *options)
type options struct {
// 客户端连接地址
// 内建客户端配置,默认为[]string{"localhost:2379"}
addrs []string
// 客户端拨号超时时间
// 内建客户端配置,默认为5秒
dialTimeout time.Duration
// 外部客户端
// 外部客户端配置,存在外部客户端时,优先使用外部客户端,默认为nil
client *clientv3.Client
// 上下文
// 默认context.Background
ctx context.Context
// 命名空间
// 默认为services
namespace string
// 上下文超时时间
// 默认为3秒
timeout time.Duration
// 心跳重试次数
// 默认为3次
retryTimes int
// 心跳重试间隔
// 默认为10秒
retryInterval time.Duration
}
func defaultOptions() *options {
return &options{
ctx: context.Background(),
addrs: getc.Get(defaultAddrsKey, []string{defaultAddr}).Strings(),
dialTimeout: getc.Get(defaultDialTimeoutKey, defaultDialTimeout).Duration(),
namespace: getc.Get(defaultNamespaceKey, defaultNamespace).String(),
timeout: getc.Get(defaultTimeoutKey, defaultTimeout).Duration(),
retryTimes: getc.Get(defaultRetryTimesKey, defaultRetryTimes).Int(),
retryInterval: getc.Get(defaultRetryIntervalKey, defaultRetryInterval).Duration(),
}
}
// WithAddrs 设置客户端连接地址
func WithAddrs(addrs ...string) Option {
return func(o *options) { o.addrs = addrs }
}
// WithDialTimeout 设置客户端拨号超时时间
func WithDialTimeout(dialTimeout time.Duration) Option {
return func(o *options) { o.dialTimeout = dialTimeout }
}
// WithClient 设置外部客户端
func WithClient(client *clientv3.Client) Option {
return func(o *options) { o.client = client }
}
// WithContext 设置上下文
func WithContext(ctx context.Context) Option {
return func(o *options) { o.ctx = ctx }
}
// WithNamespace 设置命名空间
func WithNamespace(namespace string) Option {
return func(o *options) { o.namespace = namespace }
}
// WithTimeout 设置上下文超时时间
func WithTimeout(timeout time.Duration) Option {
return func(o *options) { o.timeout = timeout }
}
// WithRetryTimes 设置心跳重试次数
func WithRetryTimes(retryTimes int) Option {
return func(o *options) { o.retryTimes = retryTimes }
}
// WithRetryInterval 设置心跳重试间隔时间
func WithRetryInterval(retryInterval time.Duration) Option {
return func(o *options) { o.retryInterval = retryInterval }
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/monobytes/gcore.git
git@gitee.com:monobytes/gcore.git
monobytes
gcore
gcore
v1.0.1

搜索帮助