diff --git a/cluster/clusterproviders/etcd/config.go b/cluster/clusterproviders/etcd/config.go index c57eb74e..3c11a4ef 100644 --- a/cluster/clusterproviders/etcd/config.go +++ b/cluster/clusterproviders/etcd/config.go @@ -3,6 +3,12 @@ package etcd import ( clientv3 "go.etcd.io/etcd/client/v3" + "time" +) + +const ( + DefaultEtcdKeepAliveTtl = 3 * time.Second + DefaultRetryInterval = 1 * time.Second ) // RoleChangedListener receives notifications when the node role changes. @@ -34,12 +40,31 @@ func WithRoleChangedListener(l RoleChangedListener) Option { } } +// WithKeepAliveTTL sets the grant TTL for node leases. +func WithKeepAliveTTL(ttl time.Duration) Option { + return func(o *config) { + o.KeepAliveTTL = ttl + } +} + +// WithRetryInterval sets the interval between retries for etcd operations. +func WithRetryInterval(interval time.Duration) Option { + return func(o *config) { + o.RetryInterval = interval + } +} + type config struct { - BaseKey string - cfg clientv3.Config - RoleChanged RoleChangedListener + BaseKey string + cfg clientv3.Config + RoleChanged RoleChangedListener + KeepAliveTTL time.Duration + RetryInterval time.Duration } func defaultConfig() *config { - return &config{} + return &config{ + KeepAliveTTL: DefaultEtcdKeepAliveTtl, + RetryInterval: DefaultRetryInterval, + } } diff --git a/cluster/clusterproviders/etcd/etcd_provider.go b/cluster/clusterproviders/etcd/etcd_provider.go index 30b6017f..d5aab412 100644 --- a/cluster/clusterproviders/etcd/etcd_provider.go +++ b/cluster/clusterproviders/etcd/etcd_provider.go @@ -62,8 +62,8 @@ func NewWithConfig(baseKey string, cfg clientv3.Config, opts ...Option) (*Provid } p := &Provider{ client: client, - keepAliveTTL: 3 * time.Second, - retryInterval: 1 * time.Second, + keepAliveTTL: c.KeepAliveTTL, + retryInterval: c.RetryInterval, baseKey: c.BaseKey, members: map[string]*Node{}, cancelWatchCh: make(chan bool),