watch supports path

This commit is contained in:
shu xian 2020-01-20 18:31:18 +08:00
parent 8d6f82707a
commit 94bb0f4c08
5 changed files with 78 additions and 32 deletions

View File

@ -7,12 +7,32 @@ import (
)
type serviceNameKey struct{}
type keyKey struct{}
type pathKey struct{}
func ServiceName(a string) source.Option {
func ServiceName(name string) source.Option {
return func(o *source.Options) {
if o.Context == nil {
o.Context = context.Background()
}
o.Context = context.WithValue(o.Context, serviceNameKey{}, a)
o.Context = context.WithValue(o.Context, serviceNameKey{}, name)
}
}
func Key(key string) source.Option {
return func(o *source.Options) {
if o.Context == nil {
o.Context = context.Background()
}
o.Context = context.WithValue(o.Context, keyKey{}, key)
}
}
func Path(path string) source.Option {
return func(o *source.Options) {
if o.Context == nil {
o.Context = context.Background()
}
o.Context = context.WithValue(o.Context, pathKey{}, path)
}
}

View File

@ -514,6 +514,7 @@ func (m *ReadResponse) GetChange() *Change {
type WatchRequest struct {
Key string `protobuf:"bytes,1,opt,name=key,proto3" json:"key,omitempty"`
Path string `protobuf:"bytes,2,opt,name=path,proto3" json:"path,omitempty"`
XXX_NoUnkeyedLiteral struct{} `json:"-"`
XXX_unrecognized []byte `json:"-"`
XXX_sizecache int32 `json:"-"`
@ -551,6 +552,13 @@ func (m *WatchRequest) GetKey() string {
return ""
}
func (m *WatchRequest) GetPath() string {
if m != nil {
return m.Path
}
return ""
}
type WatchResponse struct {
Key string `protobuf:"bytes,1,opt,name=key,proto3" json:"key,omitempty"`
ChangeSet *ChangeSet `protobuf:"bytes,2,opt,name=changeSet,proto3" json:"changeSet,omitempty"`
@ -618,32 +626,32 @@ func init() {
func init() { proto.RegisterFile("proto/service.proto", fileDescriptor_c33392ef2c1961ba) }
var fileDescriptor_c33392ef2c1961ba = []byte{
// 425 bytes of a gzipped FileDescriptorProto
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x94, 0x93, 0xdb, 0x6a, 0xdb, 0x40,
0x10, 0x86, 0x2d, 0xdb,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e, 0x36, 0xc3, 0x77, 0x70, 0x62, 0xaf, 0x85, 0x09, 0x1f, 0xdf, 0x35, 0x4f,
0xf9, 0xe4, 0x88, 0x6c, 0x76, 0x11, 0x7c, 0x0b, 0xed, 0xa7, 0x7d, 0xf9, 0x37, 0x00, 0x00, 0xff,
0xff, 0x85, 0xa3, 0x8e, 0x20, 0xf1, 0x03, 0x00, 0x00,
// 427 bytes of a gzipped FileDescriptorProto
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x94, 0x93, 0xcf, 0x6a, 0xdb, 0x40,
0x10, 0xc6, 0x2d, 0xdb, 0x51, 0xaa, 0xb1, 0x24, 0x87, 0x29, 0x14, 0x21, 0x0a, 0x35, 0x0b, 0x05,
0xd3, 0xc2, 0x26, 0x38, 0x7d, 0x83, 0xf4, 0xd6, 0x9e, 0x36, 0x94, 0xf6, 0xba, 0x95, 0xa7, 0xb5,
0x49, 0x14, 0xa9, 0xda, 0x55, 0xa0, 0x8f, 0xd0, 0x3e, 0x75, 0xd9, 0x3f, 0x8a, 0x24, 0x17, 0x4c,
0x7c, 0xdb, 0x99, 0xd9, 0xf9, 0xe6, 0xd3, 0xfc, 0x56, 0xf0, 0xb2, 0x6e, 0x2a, 0x5d, 0x5d, 0x2a,
0x6a, 0x1e, 0xf7, 0x05, 0x71, 0x1b, 0xb1, 0x3f, 0x01, 0x44, 0x37, 0x3b, 0xf9, 0xf0, 0x93, 0x6e,
0x49, 0x23, 0xc2, 0x7c, 0x2b, 0xb5, 0xcc, 0x82, 0x55, 0xb0, 0x8e, 0x85, 0x3d, 0x63, 0x0e, 0x2f,
0x8a, 0x1d, 0x15, 0x77, 0xaa, 0x2d, 0xb3, 0xe9, 0x2a, 0x58, 0x47, 0xe2, 0x29, 0xc6, 0x57, 0x10,
0xfe, 0xa8, 0x9a, 0x52, 0xea, 0x6c, 0x66, 0x2b, 0x3e, 0x32, 0x79, 0x55, 0xb5, 0x4d, 0x41, 0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1e, 0x42, 0x87, 0x13, 0x53, 0x3e, 0x7a, 0x09, 0xf9,
0x92, 0x1f, 0x70, 0x9e, 0x98, 0xcb, 0x8e, 0x1b, 0xa6, 0x7c, 0x84, 0x3c, 0x5f, 0xf2, 0x03, 0xa0,
0xf6, 0xb2, 0x03, 0x84, 0x29, 0x1f, 0xb1, 0xcd, 0x97, 0xfc, 0x80, 0xdc, 0x04, 0xdf, 0xc2, 0xdc,
0xc0, 0xc2, 0x98, 0x0f, 0x10, 0xe6, 0x09, 0x1f, 0x12, 0x74, 0xd7, 0xcc, 0xb6, 0x31, 0xe6, 0x03,
0x52, 0x79, 0xc2, 0x87, 0x08, 0xd8, 0x04, 0xdf, 0xc1, 0x99, 0xdd, 0x16, 0x26, 0x7c, 0xb8, 0xeb,
0x3c, 0xe5, 0xa3, 0x25, 0xb2, 0xc9, 0x55, 0xf0, 0x3d, 0xb4, 0xbf, 0xfb, 0xf5, 0xbf, 0x00, 0x00,
0x00, 0xff, 0xff, 0xc3, 0x45, 0xac, 0x57, 0x05, 0x04, 0x00, 0x00,
}

View File

@ -58,6 +58,7 @@ message ReadResponse {
message WatchRequest {
string key = 1;
string path = 2;
}
message WatchResponse {

View File

@ -11,18 +11,21 @@ import (
var (
DefaultName = "go.micro.config"
DefaultKey = "NAMESPACE:CONFIG"
DefaultPath = ""
DefaultClient = client.DefaultClient
)
type service struct {
serviceName string
key string
path string
opts source.Options
client proto.Service
}
func (m *service) Read() (set *source.ChangeSet, err error) {
req, err := m.client.Read(context.Background(), &proto.ReadRequest{Path: m.key})
req, err := m.client.Read(context.Background(), &proto.ReadRequest{Key: m.key, Path: m.path})
if err != nil {
return nil, err
}
@ -31,7 +34,7 @@ func (m *service) Read() (set *source.ChangeSet, err error) {
}
func (m *service) Watch() (w source.Watcher, err error) {
stream, err := m.client.Watch(context.Background(), &proto.WatchRequest{Key: m.key})
stream, err := m.client.Watch(context.Background(), &proto.WatchRequest{Key: m.key, Path: m.path})
if err != nil {
log.Error("watch err: ", err)
return
@ -55,17 +58,31 @@ func NewSource(opts ...source.Option) source.Source {
}
addr := DefaultName
key := DefaultKey
path := DefaultPath
if options.Context != nil {
a, ok := options.Context.Value(serviceNameKey{}).(string)
if ok {
addr = a
}
k, ok := options.Context.Value(keyKey{}).(string)
if ok {
key = k
}
p, ok := options.Context.Value(pathKey{}).(string)
if ok {
path = p
}
}
s := &service{
serviceName: addr,
opts: options,
key: key,
path: path,
client: proto.NewService(addr, DefaultClient),
}