Skip to content

Commit

Permalink
feature: simplify find next service algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
miun173 committed Feb 16, 2020
1 parent 3ee8357 commit ddcb085
Showing 1 changed file with 9 additions and 12 deletions.
21 changes: 9 additions & 12 deletions proxy/service_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,6 @@ func NewServiceProxy() *ServiceProxy {
}
}

// NextIndex :nodoc:
func (sp *ServiceProxy) NextIndex() int {
return sp.currentService % len(sp.services)
}

// Start round robin server :nodoc:
func (sp *ServiceProxy) Start() {
m := &http.ServeMux{}
Expand Down Expand Up @@ -145,16 +140,18 @@ func (sp *ServiceProxy) FindNextService() *Service {
return nil
}

next := sp.NextIndex()
n := len(sp.services) + next
next := sp.currentService % len(sp.services)
nservice := len(sp.services)

for i := next; i < n; i++ {
// make itter to nservice+1 so when it reach `idx = nservice-1`,
// but the serivce of `idx` is not alive, it will go back to `idx = 0`
for i := next; i < nservice+1; i++ {
idx := i % nservice
if sp.services[idx].IsAlive() {
sp.currentService = (sp.currentService + 1) % nservice
return sp.services[next]
if !sp.services[idx].IsAlive() {
continue
}

sp.currentService = (idx + 1) % nservice
return sp.services[idx]
}

return nil
Expand Down

0 comments on commit ddcb085

Please sign in to comment.