Skip to content

Commit

Permalink
Fix API loader race (#2082)
Browse files Browse the repository at this point in the history
Remove using goroutine when processing specs.
API loader gets called very rare, and does not needs to be parallelized.
  • Loading branch information
buger authored Jan 28, 2019
1 parent dc5124d commit 288924a
Showing 1 changed file with 13 additions and 24 deletions.
37 changes: 13 additions & 24 deletions api_loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -546,8 +546,6 @@ func loadApps(specs []*APISpec, muxer *mux.Router) {
return len(specs[i].Proxy.ListenPath) > len(specs[j].Proxy.ListenPath)
})

chainChannel := make(chan *ChainObject)

// Create a new handler for each API spec
loadList := make([]*ChainObject, len(specs))
apisByListen := countApisByListenHash(specs)
Expand Down Expand Up @@ -584,32 +582,23 @@ func loadApps(specs []*APISpec, muxer *mux.Router) {
}

for i, spec := range specs {
go func(spec *APISpec, i int) {
subrouter := hostRouters[spec.Domain]
if subrouter == nil {
mainLog.WithFields(logrus.Fields{
"domain": spec.Domain,
"api_id": spec.APIID,
}).Warning("Trying to load API with Domain when custom domains are disabled.")
subrouter = muxer
}

chainObj := processSpec(spec, apisByListen, redisStore, redisOrgStore, healthStore, rpcAuthStore, rpcOrgStore, subrouter, logrus.NewEntry(log))
subrouter := hostRouters[spec.Domain]
if subrouter == nil {
mainLog.WithFields(logrus.Fields{
"domain": spec.Domain,
"api_id": spec.APIID,
}).Warning("Trying to load API with Domain when custom domains are disabled.")
subrouter = muxer
}

chainObj.Index = i
chainChannel <- chainObj
apisMu.Lock()
spec.middlewareChain = chainObj
apisMu.Unlock()
}(spec, i)
chainObj := processSpec(spec, apisByListen, redisStore, redisOrgStore, healthStore, rpcAuthStore, rpcOrgStore, subrouter, logrus.NewEntry(log))
apisMu.Lock()
spec.middlewareChain = chainObj
apisMu.Unlock()

// TODO: This will not deal with skipped APis well
tmpSpecRegister[spec.APIID] = spec
}

for range specs {
chObj := <-chainChannel
loadList[chObj.Index] = chObj
loadList[i] = chainObj
}

for _, chainObj := range loadList {
Expand Down

0 comments on commit 288924a

Please sign in to comment.