From 90a9df9b8cc346cf56021bfdf40b0a2be5823698 Mon Sep 17 00:00:00 2001 From: outshow Date: Tue, 11 Jun 2019 16:18:37 +0800 Subject: [PATCH] 1. use github.com/coreos instead of go.etcd.io in etcd related import path; 2. add dialtimeout to etcd client --- config/source/etcd/etcd.go | 13 ++++++++++--- config/source/etcd/options.go | 14 +++++++++++++- config/source/etcd/util.go | 4 ++-- config/source/etcd/watcher.go | 2 +- go.mod | 2 +- 5 files changed, 27 insertions(+), 8 deletions(-) diff --git a/config/source/etcd/etcd.go b/config/source/etcd/etcd.go index 873c2de7..5cbe1286 100644 --- a/config/source/etcd/etcd.go +++ b/config/source/etcd/etcd.go @@ -6,9 +6,9 @@ import ( "net" "time" + cetcd "github.com/coreos/etcd/clientv3" + "github.com/coreos/etcd/mvcc/mvccpb" "github.com/micro/go-micro/config/source" - cetcd "go.etcd.io/etcd/clientv3" - "go.etcd.io/etcd/mvcc/mvccpb" ) // Currently a single etcd reader @@ -100,8 +100,15 @@ func NewSource(opts ...source.Option) source.Source { endpoints = []string{"localhost:2379"} } + // check dial timeout option + dialTimeout, ok := options.Context.Value(dialTimeoutKey{}).(time.Duration) + if !ok { + dialTimeout = 3 * time.Second // default dial timeout + } + config := cetcd.Config{ - Endpoints: endpoints, + Endpoints: endpoints, + DialTimeout: dialTimeout, } u, ok := options.Context.Value(authKey{}).(*authCreds) diff --git a/config/source/etcd/options.go b/config/source/etcd/options.go index 87eff8ef..325d2f46 100644 --- a/config/source/etcd/options.go +++ b/config/source/etcd/options.go @@ -2,6 +2,7 @@ package etcd import ( "context" + "time" "github.com/micro/go-micro/config/source" ) @@ -10,13 +11,14 @@ type addressKey struct{} type prefixKey struct{} type stripPrefixKey struct{} type authKey struct{} +type dialTimeoutKey struct{} type authCreds struct { Username string Password string } -// WithAddress sets the consul address +// WithAddress sets the etcd address func WithAddress(a ...string) source.Option { return func(o *source.Options) { if o.Context == nil { @@ -56,3 +58,13 @@ func Auth(username, password string) source.Option { o.Context = context.WithValue(o.Context, authKey{}, &authCreds{Username: username, Password: password}) } } + +// WithDialTimeout set the time out for dialing to etcd +func WithDialTimeout(timeout time.Duration) source.Option { + return func(o *source.Options) { + if o.Context == nil { + o.Context = context.Background() + } + o.Context = context.WithValue(o.Context, dialTimeoutKey{}, timeout) + } +} diff --git a/config/source/etcd/util.go b/config/source/etcd/util.go index e57475e4..31887fc9 100644 --- a/config/source/etcd/util.go +++ b/config/source/etcd/util.go @@ -3,9 +3,9 @@ package etcd import ( "strings" + "github.com/coreos/etcd/clientv3" + "github.com/coreos/etcd/mvcc/mvccpb" "github.com/micro/go-micro/config/encoder" - "go.etcd.io/etcd/clientv3" - "go.etcd.io/etcd/mvcc/mvccpb" ) func makeEvMap(e encoder.Encoder, data map[string]interface{}, kv []*clientv3.Event, stripPrefix string) map[string]interface{} { diff --git a/config/source/etcd/watcher.go b/config/source/etcd/watcher.go index 1066c899..2f9b3189 100644 --- a/config/source/etcd/watcher.go +++ b/config/source/etcd/watcher.go @@ -6,8 +6,8 @@ import ( "sync" "time" + cetcd "github.com/coreos/etcd/clientv3" "github.com/micro/go-micro/config/source" - cetcd "go.etcd.io/etcd/clientv3" ) type watcher struct { diff --git a/go.mod b/go.mod index b2cb5e95..def78418 100644 --- a/go.mod +++ b/go.mod @@ -7,7 +7,7 @@ require ( github.com/beevik/ntp v0.2.0 github.com/bitly/go-simplejson v0.5.0 github.com/bwmarrin/discordgo v0.19.0 - github.com/coreos/etcd v3.3.13+incompatible // indirect + github.com/coreos/etcd v3.3.13+incompatible github.com/forestgiant/sliceutil v0.0.0-20160425183142-94783f95db6c github.com/fsnotify/fsnotify v1.4.7 github.com/fsouza/go-dockerclient v1.4.1