Skip to content
Snippets Groups Projects
Commit 4f1db893 authored by Jake Taylor's avatar Jake Taylor
Browse files

fix mutex issues

parent f4adc5c0
Branches
Tags
No related merge requests found
......@@ -139,11 +139,13 @@ func (h *HostPool) UpdateNdf(ndf *ndf.NetworkDefinition) {
// Obtain a random, unique list of Hosts of the given length from the HostPool
func (h *HostPool) getAny(length uint32, excluded []*id.ID) []*connect.Host {
checked := make(map[uint32]interface{}) // Keep track of Hosts already selected to avoid duplicates
if length > h.poolParams.PoolSize {
length = h.poolParams.PoolSize
}
checked := make(map[uint32]interface{}) // Keep track of Hosts already selected to avoid duplicates
if excluded != nil {
// Add excluded Hosts to already-checked list
for i := range excluded {
gwId := excluded[i]
if idx, ok := h.hostMap[*gwId]; ok {
......@@ -152,13 +154,19 @@ func (h *HostPool) getAny(length uint32, excluded []*id.ID) []*connect.Host {
}
}
result := make([]*connect.Host, length)
result := make([]*connect.Host, 0, length)
rng := h.rng.GetStream()
h.hostMux.RLock()
for i := uint32(0); i < length; {
// If we've checked the entire HostPool, bail
if uint32(len(checked)) >= h.poolParams.PoolSize {
break
}
// Check the next HostPool index
gwIdx := readRangeUint32(0, h.poolParams.PoolSize, rng)
if _, ok := checked[gwIdx]; !ok {
result[i] = h.hostList[gwIdx]
result = append(result, h.hostList[gwIdx])
checked[gwIdx] = nil
i++
}
......@@ -208,10 +216,21 @@ func (h *HostPool) getPreferred(targets []*id.ID) []*connect.Host {
// Replaces the given hostId in the HostPool if the given hostErr is in errorList
func (h *HostPool) checkReplace(hostId *id.ID, hostErr error) error {
// Check if Host should be replaced
doReplace := false
if hostErr != nil {
for _, errString := range errorsList {
if strings.Contains(hostErr.Error(), errString) {
// Host needs replaced, flag and continue
doReplace = true
break
}
}
}
if doReplace {
h.hostMux.Lock()
defer h.hostMux.Unlock()
// If the Host is still in the pool
if oldPoolIndex, ok := h.hostMap[*hostId]; ok {
......@@ -219,15 +238,8 @@ func (h *HostPool) checkReplace(hostId *id.ID, hostErr error) error {
h.ndfMux.RLock()
err := h.forceReplace(oldPoolIndex)
h.ndfMux.RUnlock()
h.hostMux.Unlock()
return err
}
// If the Host is NOT still in the pool, return
h.hostMux.Unlock()
return nil
}
}
}
return nil
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment