From 2e3fbc4f8f3789057f412ef7df071d9ee73307fc Mon Sep 17 00:00:00 2001 From: yangzhouhan Date: Fri, 7 Aug 2015 17:12:15 -0700 Subject: [PATCH] change the name resolver interface --- clientconn.go | 12 ------------ etcdnaming/etcdnaming.go => naming/etcd/etcd.go | 14 ++++++++++++-- naming/naming.go | 14 ++++++++++++++ 3 files changed, 26 insertions(+), 14 deletions(-) rename etcdnaming/etcdnaming.go => naming/etcd/etcd.go (92%) create mode 100644 naming/naming.go diff --git a/clientconn.go b/clientconn.go index 8a45185b..52d47a57 100644 --- a/clientconn.go +++ b/clientconn.go @@ -71,18 +71,6 @@ type dialOptions struct { // DialOption configures how we set up the connection. 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. func WithCodec(c Codec) DialOption { return func(o *dialOptions) { diff --git a/etcdnaming/etcdnaming.go b/naming/etcd/etcd.go similarity index 92% rename from etcdnaming/etcdnaming.go rename to naming/etcd/etcd.go index 59e01c13..838c2377 100644 --- a/etcdnaming/etcdnaming.go +++ b/naming/etcd/etcd.go @@ -1,4 +1,4 @@ -package etcdnaming +package etcd import ( "fmt" @@ -6,6 +6,7 @@ import ( etcdcl "github.com/coreos/etcd/client" "golang.org/x/net/context" + "google.golang.org/grpc/naming" ) type kv struct { @@ -73,7 +74,7 @@ type etcdNR struct { } // NewETCDNR creates an etcd NameResolver -func NewETCDNR(cfg etcdcl.Config) *etcdNR { +func NewETCDNR(cfg etcdcl.Config) naming.Resolver { c, err := etcdcl.New(cfg) if err != nil { panic(err) @@ -106,6 +107,10 @@ func (nr *etcdNR) Get(target string) map[string]string { } res := make(map[string]string) getNode(resp.Node, res) + + for k,v := range res { + fmt.Println("key is :",k,"value is :",v)} + return res } @@ -132,6 +137,9 @@ func (nr *etcdNR) GetUpdate() (string, string) { select { case i := <-nr.recv.get(): nr.recv.load() + if i == nil { + return "","" + } // returns key and the corresponding value of the updated kv return i.key, i.value } @@ -141,3 +149,5 @@ func (nr *etcdNR) Stop() { nr.recv.stop() nr.cancel() } + + diff --git a/naming/naming.go b/naming/naming.go new file mode 100644 index 00000000..bba0f52a --- /dev/null +++ b/naming/naming.go @@ -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() +} +