Merge pull request #823 from micro/list-services

Support listing full service info in etcd
This commit is contained in:
Asim Aslam 2019-10-07 16:15:30 +01:00 committed by GitHub
commit e4f8b5de70
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -349,7 +349,7 @@ func (e *etcdRegistry) GetService(name string) ([]*registry.Service, error) {
func (e *etcdRegistry) ListServices() ([]*registry.Service, error) { func (e *etcdRegistry) ListServices() ([]*registry.Service, error) {
var services []*registry.Service var services []*registry.Service
nameSet := make(map[string]struct{}) versions := make(map[string]*registry.Service)
ctx, cancel := context.WithTimeout(context.Background(), e.options.Timeout) ctx, cancel := context.WithTimeout(context.Background(), e.options.Timeout)
defer cancel() defer cancel()
@ -364,13 +364,20 @@ func (e *etcdRegistry) ListServices() ([]*registry.Service, error) {
} }
for _, n := range rsp.Kvs { for _, n := range rsp.Kvs {
if sn := decode(n.Value); sn != nil { sn := decode(n.Value)
nameSet[sn.Name] = struct{}{} if sn == nil {
continue
} }
v, ok := versions[sn.Name+sn.Version]
if !ok {
versions[sn.Name+sn.Version] = sn
continue
}
// append to service:version nodes
v.Nodes = append(v.Nodes, sn.Nodes...)
} }
for k := range nameSet {
service := &registry.Service{} for _, service := range versions {
service.Name = k
services = append(services, service) services = append(services, service)
} }