change the name resolver interface
This commit is contained in:
@ -71,18 +71,6 @@ type dialOptions struct {
|
|||||||
// DialOption configures how we set up the connection.
|
// DialOption configures how we set up the connection.
|
||||||
type DialOption func(*dialOptions)
|
type DialOption func(*dialOptions)
|
||||||
|
|
||||||
// NameResolver dose name resolution and watches for the resolution changes.
|
|
||||||
type NameResolver interface {
|
|
||||||
// Get gets a snapshot of the current name resolution results for target.
|
|
||||||
Get(target string) map[string]string
|
|
||||||
// Watch watches for the name resolution changes on target. It blocks until Stop() is invoked. The watch results are obtained via GetUpdate().
|
|
||||||
Watch(target string)
|
|
||||||
// GetUpdate returns a name resolution change when watch is triggered. It blocks until it observes a change. The caller needs to call it again to get the next change.
|
|
||||||
GetUpdate() (string, string)
|
|
||||||
// Stop shuts down the NameResolver.
|
|
||||||
Stop()
|
|
||||||
}
|
|
||||||
|
|
||||||
// WithCodec returns a DialOption which sets a codec for message marshaling and unmarshaling.
|
// WithCodec returns a DialOption which sets a codec for message marshaling and unmarshaling.
|
||||||
func WithCodec(c Codec) DialOption {
|
func WithCodec(c Codec) DialOption {
|
||||||
return func(o *dialOptions) {
|
return func(o *dialOptions) {
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
package etcdnaming
|
package etcd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
@ -6,6 +6,7 @@ import (
|
|||||||
|
|
||||||
etcdcl "github.com/coreos/etcd/client"
|
etcdcl "github.com/coreos/etcd/client"
|
||||||
"golang.org/x/net/context"
|
"golang.org/x/net/context"
|
||||||
|
"google.golang.org/grpc/naming"
|
||||||
)
|
)
|
||||||
|
|
||||||
type kv struct {
|
type kv struct {
|
||||||
@ -73,7 +74,7 @@ type etcdNR struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// NewETCDNR creates an etcd NameResolver
|
// NewETCDNR creates an etcd NameResolver
|
||||||
func NewETCDNR(cfg etcdcl.Config) *etcdNR {
|
func NewETCDNR(cfg etcdcl.Config) naming.Resolver {
|
||||||
c, err := etcdcl.New(cfg)
|
c, err := etcdcl.New(cfg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
@ -106,6 +107,10 @@ func (nr *etcdNR) Get(target string) map[string]string {
|
|||||||
}
|
}
|
||||||
res := make(map[string]string)
|
res := make(map[string]string)
|
||||||
getNode(resp.Node, res)
|
getNode(resp.Node, res)
|
||||||
|
|
||||||
|
for k,v := range res {
|
||||||
|
fmt.Println("key is :",k,"value is :",v)}
|
||||||
|
|
||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -132,6 +137,9 @@ func (nr *etcdNR) GetUpdate() (string, string) {
|
|||||||
select {
|
select {
|
||||||
case i := <-nr.recv.get():
|
case i := <-nr.recv.get():
|
||||||
nr.recv.load()
|
nr.recv.load()
|
||||||
|
if i == nil {
|
||||||
|
return "",""
|
||||||
|
}
|
||||||
// returns key and the corresponding value of the updated kv
|
// returns key and the corresponding value of the updated kv
|
||||||
return i.key, i.value
|
return i.key, i.value
|
||||||
}
|
}
|
||||||
@ -141,3 +149,5 @@ func (nr *etcdNR) Stop() {
|
|||||||
nr.recv.stop()
|
nr.recv.stop()
|
||||||
nr.cancel()
|
nr.cancel()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
14
naming/naming.go
Normal file
14
naming/naming.go
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
package naming;
|
||||||
|
|
||||||
|
// Resolver dose name resolution and watches for the resolution changes.
|
||||||
|
type Resolver interface {
|
||||||
|
// Get gets a snapshot of the current name resolution results for target.
|
||||||
|
Get(target string) map[string]string
|
||||||
|
// Watch watches for the name resolution changes on target. It blocks until Stop() is invoked. The watch results are obtained via GetUpdate().
|
||||||
|
Watch(target string)
|
||||||
|
// GetUpdate returns a name resolution change when watch is triggered. It blocks until it observes a change. The caller needs to call it again to get the next change.
|
||||||
|
GetUpdate() (string, string)
|
||||||
|
// Stop shuts down the NameResolver.
|
||||||
|
Stop()
|
||||||
|
}
|
||||||
|
|
Reference in New Issue
Block a user