Skip to content

Commit

Permalink
Merge pull request #3 from gptankit/master_algo
Browse files Browse the repository at this point in the history
Bug fix: findCeilIn
  • Loading branch information
gptankit authored May 26, 2018
2 parents e0b1def + 3e145a3 commit 959dd02
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 7 deletions.
12 changes: 12 additions & 0 deletions algorithm/randomize.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,15 @@ func randomize(init int, set int) int {

return choice
}

func randomize64(init int64, set int64) int64 {

if set <= init {
return int64(0)
}

rand.Seed(time.Now().UTC().UnixNano())
choice := rand.Int63n(set - init) + init

return choice
}
35 changes: 28 additions & 7 deletions algorithm/select_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,36 @@ func ChooseServiceIndex(sqp *model.ServiceQProperties, initialChoice int, retry
prefixes[i] = weights[i] + prefixes[i-1]
}
}
randx := randomize(1, int(prefixes[len(prefixes) - 1]))
for i, n := range prefixes {
if int(n) >= randx {
return i
}
}
prLen := len(prefixes) - 1
randx := randomize64(1, int64(prefixes[prLen]))
ceil := findCeilIn(randx, prefixes, 0, prLen)
if ceil >= 0 {
return ceil
}
return randomize(0, noOfServices)
}
return randomize(0, noOfServices)
} else {
return roundrobin(noOfServices, initialChoice)
}
}

func findCeilIn(randx int64, prefixes []float64, start int, end int) int {

var mid int
for {
if start >= end {
break
}
mid = start + ((end - start) >> 1)
if randx > int64(prefixes[mid]) {
start = mid + 1
} else {
end = mid
}
}

if randx <= int64(prefixes[start]) {
return start
}
return -1
}

0 comments on commit 959dd02

Please sign in to comment.