Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 3 additions & 9 deletions roundrobin_weighted.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,8 @@ func (w *RRW) Next() interface{} {
}

func gcd(x, y int) int {
var t int
for {
t = (x % y)
if t > 0 {
x = y
y = t
} else {
return y
}
for y != 0 {
x, y = y, x%y
}
return x
}
20 changes: 20 additions & 0 deletions roundrobin_weighted_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,23 @@ func TestRRW_Next(t *testing.T) {
t.Error("the algorithm is wrong", results)
}
}

func TestGCB(t *testing.T) {
tests := []struct {
name string
args [2]int
want int
}{
{"0,0", [2]int{0, 0}, 0},
{"1997,615", [2]int{1997, 6150}, 1},
{"481,221", [2]int{481, 221}, 13},
{"12,18", [2]int{12, 18}, 6},
}
for _, tt := range tests {
n1 := tt.args[0]
n2 := tt.args[1]
if got := gcd(n1, n2); got != tt.want {
t.Errorf("gcb(%v, %v) = %v ; want = %v", n1, n2, got, tt.want)
}
}
}
25 changes: 6 additions & 19 deletions smooth_weighted.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,23 +66,14 @@ func (w *SW) All() map[interface{}]int {

// Next returns next selected server.
func (w *SW) Next() interface{} {
i := w.nextWeighted()
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if w.n == 0; just return; No additional method calls are required.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why removed this line?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

because can be return nextSmoothWeighted(w.items).Item
look blow:

// Next returns next selected server.
func (w *SW) Next() interface{} {
	switch w.n {
	case 0:
		return nil
	case 1:
		return w.items[0].Item
	default:
		return nextSmoothWeighted(w.items).Item
	}
}

if i == nil {
switch w.n {
case 0:
return nil
case 1:
return w.items[0].Item
default:
return nextSmoothWeighted(w.items).Item
}
return i.Item
}

// nextWeighted returns next selected weighted object.
func (w *SW) nextWeighted() *smoothWeighted {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be more concise to make these judgments in next.

if w.n == 0 {
return nil
}
if w.n == 1 {
return w.items[0]
}

return nextSmoothWeighted(w.items)
}

//https://github.com/phusion/nginx/commit/27e94984486058d73157038f7950a0a36ecc6e35
Expand All @@ -92,10 +83,6 @@ func nextSmoothWeighted(items []*smoothWeighted) (best *smoothWeighted) {
for i := 0; i < len(items); i++ {
w := items[i]

if w == nil {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because the parameters call into the method nextSmoothWeighted() always is greater than 1. Another way, whether it is len(items) =0; i !< 0; len(items) > 1 , items[i] != nil; So, those code never gets executed.

continue
}

w.CurrentWeight += w.EffectiveWeight
total += w.EffectiveWeight
if w.EffectiveWeight < w.Weight {
Expand Down