fix concurrent map read and write (#446)

* fix concurrent map read and write

* add test for fetch after build opt

* fix typo
pull/462/head
Tanghui Lin 5 years ago committed by Tony
parent 43a13f6aae
commit 99eb3efa02
  1. 13
      pkg/naming/discovery/discovery.go

@ -276,9 +276,18 @@ func (r *Resolve) Fetch(ctx context.Context) (ins *naming.InstancesInfo, ok bool
app, ok := r.d.apps[r.id] app, ok := r.d.apps[r.id]
r.d.mutex.RUnlock() r.d.mutex.RUnlock()
if ok { if ok {
ins, ok = app.zoneIns.Load().(*naming.InstancesInfo) var appIns *naming.InstancesInfo
appIns, ok = app.zoneIns.Load().(*naming.InstancesInfo)
ins = new(naming.InstancesInfo)
ins.LastTs = appIns.LastTs
ins.Scheduler = appIns.Scheduler
if r.opt.Filter != nil { if r.opt.Filter != nil {
ins.Instances = r.opt.Filter(ins.Instances) ins.Instances = r.opt.Filter(appIns.Instances)
} else {
ins.Instances = make(map[string][]*naming.Instance)
for zone, in := range appIns.Instances {
ins.Instances[zone] = in
}
} }
if r.opt.Scheduler != nil { if r.opt.Scheduler != nil {
ins.Instances[r.opt.ClientZone] = r.opt.Scheduler(ins) ins.Instances[r.opt.ClientZone] = r.opt.Scheduler(ins)

Loading…
Cancel
Save