|
|
@ -144,33 +144,42 @@ func (r *Registry) Watch(ctx context.Context, name string) (registry.Watcher, er |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if !ok { |
|
|
|
if !ok { |
|
|
|
go r.resolve(set) |
|
|
|
err := r.resolve(set) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
return nil, err |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
return w, nil |
|
|
|
return w, nil |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (r *Registry) resolve(ss *serviceSet) { |
|
|
|
func (r *Registry) resolve(ss *serviceSet) error { |
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10) |
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10) |
|
|
|
services, idx, err := r.cli.Service(ctx, ss.serviceName, 0, true) |
|
|
|
services, idx, err := r.cli.Service(ctx, ss.serviceName, 0, true) |
|
|
|
cancel() |
|
|
|
cancel() |
|
|
|
if err == nil && len(services) > 0 { |
|
|
|
if err != nil { |
|
|
|
|
|
|
|
return err |
|
|
|
|
|
|
|
} else if len(services) > 0 { |
|
|
|
ss.broadcast(services) |
|
|
|
ss.broadcast(services) |
|
|
|
} |
|
|
|
} |
|
|
|
ticker := time.NewTicker(time.Second) |
|
|
|
go func() { |
|
|
|
defer ticker.Stop() |
|
|
|
ticker := time.NewTicker(time.Second) |
|
|
|
for { |
|
|
|
defer ticker.Stop() |
|
|
|
<-ticker.C |
|
|
|
for { |
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Second*120) |
|
|
|
<-ticker.C |
|
|
|
tmpService, tmpIdx, err := r.cli.Service(ctx, ss.serviceName, idx, true) |
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Second*120) |
|
|
|
cancel() |
|
|
|
tmpService, tmpIdx, err := r.cli.Service(ctx, ss.serviceName, idx, true) |
|
|
|
if err != nil { |
|
|
|
cancel() |
|
|
|
time.Sleep(time.Second) |
|
|
|
if err != nil { |
|
|
|
continue |
|
|
|
time.Sleep(time.Second) |
|
|
|
|
|
|
|
continue |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if len(tmpService) != 0 && tmpIdx != idx { |
|
|
|
|
|
|
|
services = tmpService |
|
|
|
|
|
|
|
ss.broadcast(services) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
idx = tmpIdx |
|
|
|
} |
|
|
|
} |
|
|
|
if len(tmpService) != 0 && tmpIdx != idx { |
|
|
|
}() |
|
|
|
services = tmpService |
|
|
|
|
|
|
|
ss.broadcast(services) |
|
|
|
return nil |
|
|
|
} |
|
|
|
|
|
|
|
idx = tmpIdx |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|