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

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