Skip to content
This repository has been archived by the owner on Oct 30, 2023. It is now read-only.

Commit

Permalink
One more update (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
kirillbilchenko authored May 25, 2020
1 parent d1a5c55 commit 049b1df
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@ func findFirstNodeByQueue(hosts *Hosts, mutex *sync.RWMutex) (host *Host, err er
return &(*hosts)[0], nil
}
hostMap := map[string]*Host{}
for _, host := range *hosts {
hostMap[fmt.Sprintf("%s%d%s", host.Name, host.Port, host.Region)] = &host
for i, v := range *hosts {
hostMap[fmt.Sprintf("%s%d%s", v.Name, v.Port, v.Region)] = &(*hosts)[i]
}
mutex.Lock()
resultMap := make(map[*Host]*capacity)
Expand All @@ -145,15 +145,14 @@ func findFirstNodeByQueue(hosts *Hosts, mutex *sync.RWMutex) (host *Host, err er
}
netClient.CloseIdleConnections()
if len(resultMap) < 1 {
for k := range resultMap {
return k, nil
}
}
targetHost, err := mostFreeHost(resultMap)
if err != nil {
return nil, errors.New("no valid host found")
}
return targetHost, nil
var target, _ = mostFreeHost(resultMap)
if v, ok := hostMap[fmt.Sprintf("%s%d%s", target.Name, target.Port, target.Region)]; ok {
return v, nil
}
return nil, errors.New("no valid host found")

}

func mostFreeHost(target map[*Host]*capacity) (*Host, error) {
Expand Down

0 comments on commit 049b1df

Please sign in to comment.