Skip to content

Commit

Permalink
feat(contrib/registry/etcd/): add DialTimeout and `AutoSyncInterval…
Browse files Browse the repository at this point in the history
…` option (#3698)
  • Loading branch information
fengshunli authored Sep 24, 2024
1 parent c13004e commit a1ce97e
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 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 @@ -38,11 +39,26 @@ type Registry struct {
type Option struct {
Logger glog.ILogger
KeepaliveTTL time.Duration

// DialTimeout is the timeout for failing to establish a connection.
DialTimeout time.Duration

// AutoSyncInterval is the interval to update endpoints with its latest members.
AutoSyncInterval time.Duration

DialOptions []grpc.DialOption
}

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 +96,21 @@ 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
}

client, err := etcd3.New(cfg)
if err != nil {
panic(gerror.Wrap(err, `create etcd client failed`))
Expand Down

0 comments on commit a1ce97e

Please sign in to comment.