1. use github.com/coreos instead of go.etcd.io in etcd related import path; 2. add dialtimeout to etcd client

This commit is contained in:
outshow 2019-06-11 16:18:37 +08:00
parent 46de3ae9a9
commit 90a9df9b8c
5 changed files with 27 additions and 8 deletions

View File

@ -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)

View File

@ -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)
}
}

View File

@ -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{} {

View File

@ -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 {

2
go.mod
View File

@ -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