change the name resolver interface

This commit is contained in:
yangzhouhan
2015-08-07 17:12:15 -07:00
parent 813dca6ff7
commit 2e3fbc4f8f
3 changed files with 26 additions and 14 deletions

View File

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

View File

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

14
naming/naming.go Normal file
View 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()
}