Skip to content

Commit

Permalink
add etcd DialTimeout and AutoSyncInterval
Browse files Browse the repository at this point in the history
Signed-off-by: fsl <[email protected]>
  • Loading branch information
fengshunli committed Jul 24, 2024
1 parent ba322ba commit 7214290
Showing 1 changed file with 32 additions and 10 deletions.
42 changes: 32 additions & 10 deletions contrib/registry/etcd/etcd.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"time"

etcd3 "go.etcd.io/etcd/client/v3"
"google.golang.org/grpc"

"github.com/gogf/gf/v2/errors/gcode"
"github.com/gogf/gf/v2/errors/gerror"
Expand All @@ -36,13 +37,22 @@ type Registry struct {

// Option is the option for the etcd registry.
type Option struct {
Logger glog.ILogger
KeepaliveTTL time.Duration
Logger glog.ILogger
KeepaliveTTL time.Duration
DialTimeout time.Duration
AutoSyncInterval time.Duration
}

const (
// DefaultKeepAliveTTL is the default keepalive TTL.
DefaultKeepAliveTTL = 10 * time.Second

// DefaultDialTimeout is the timeout for failing to establish a connection.
DefaultDialTimeout = time.Second * 5

// DefaultAutoSyncInterval is the interval to update endpoints with its latest members.
// 0 disables auto-sync. By default auto-sync is disabled.
DefaultAutoSyncInterval = time.Second
)

// New creates and returns a new etcd registry.
Expand Down Expand Up @@ -80,6 +90,22 @@ func New(address string, option ...Option) gsvc.Registry {
if password != "" {
cfg.Password = password
}

cfg.DialTimeout = DefaultDialTimeout
cfg.AutoSyncInterval = DefaultAutoSyncInterval

var usedOption Option
if len(option) > 0 {
usedOption = option[0]
}
if usedOption.DialTimeout > 0 {
cfg.DialTimeout = usedOption.DialTimeout
}
if usedOption.AutoSyncInterval > 0 {
cfg.AutoSyncInterval = usedOption.AutoSyncInterval
}

cfg.DialOptions = []grpc.DialOption{grpc.WithBlock()}
client, err := etcd3.New(cfg)
if err != nil {
panic(gerror.Wrap(err, `create etcd client failed`))
Expand All @@ -90,19 +116,15 @@ func New(address string, option ...Option) gsvc.Registry {
// NewWithClient creates and returns a new etcd registry with the given client.
func NewWithClient(client *etcd3.Client, option ...Option) *Registry {
r := &Registry{
client: client,
kv: etcd3.NewKV(client),
client: client,
kv: etcd3.NewKV(client),
logger: g.Log(),
keepaliveTTL: DefaultKeepAliveTTL,
}
if len(option) > 0 {
r.logger = option[0].Logger
r.keepaliveTTL = option[0].KeepaliveTTL
}
if r.logger == nil {
r.logger = g.Log()
}
if r.keepaliveTTL == 0 {
r.keepaliveTTL = DefaultKeepAliveTTL
}
return r
}

Expand Down

0 comments on commit 7214290

Please sign in to comment.