Skip to content

Commit

Permalink
add internal pkg
Browse files Browse the repository at this point in the history
  • Loading branch information
sysulq committed Aug 19, 2020
1 parent 22bda0a commit beda079
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 6 deletions.
8 changes: 8 additions & 0 deletions internal/internal.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package internal

import "google.golang.org/grpc/balancer"

var (
// EmptyDoneFunc is a empty done function
EmptyDoneFunc = func(balancer.DoneInfo) {}
)
3 changes: 2 additions & 1 deletion p2c/least_loaded.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"time"

"github.com/hnlq715/go-loadbalance"
"github.com/hnlq715/go-loadbalance/internal"
"google.golang.org/grpc/balancer"
)

Expand Down Expand Up @@ -42,7 +43,7 @@ func (p *leastLoaded) Next() (interface{}, func(balancer.DoneInfo)) {

switch len(p.items) {
case 0:
return nil, func(balancer.DoneInfo) {}
return nil, internal.EmptyDoneFunc
case 1:
sc = p.items[0]
default:
Expand Down
3 changes: 2 additions & 1 deletion p2c/pewma.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"time"

"github.com/hnlq715/go-loadbalance"
"github.com/hnlq715/go-loadbalance/internal"
"google.golang.org/grpc/balancer"
)

Expand Down Expand Up @@ -88,7 +89,7 @@ func (p *pewma) Next() (interface{}, func(balancer.DoneInfo)) {

switch len(p.items) {
case 0:
return nil, func(balancer.DoneInfo) {}
return nil, internal.EmptyDoneFunc
case 1:
sc = p.items[0]
default:
Expand Down
7 changes: 4 additions & 3 deletions roundrobin/smooth_weighted.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"math"

"github.com/hnlq715/go-loadbalance"
"github.com/hnlq715/go-loadbalance/internal"
"google.golang.org/grpc/balancer"
)

Expand Down Expand Up @@ -51,14 +52,14 @@ func (w *smoothRoundrobin) Reset() {
// Next returns next selected server.
func (w *smoothRoundrobin) Next() (interface{}, func(balancer.DoneInfo)) {
if w.n == 0 {
return nil, func(balancer.DoneInfo) {}
return nil, internal.EmptyDoneFunc
}

if w.n == 1 {
return w.items[0].Item, func(balancer.DoneInfo) {}
return w.items[0].Item, internal.EmptyDoneFunc
}

return nextSmoothWeighted(w.items).Item, func(balancer.DoneInfo) {}
return nextSmoothWeighted(w.items).Item, internal.EmptyDoneFunc
}

// nextSmoothWeighted selects the best node through the smooth weighted roundrobin .
Expand Down
4 changes: 3 additions & 1 deletion roundrobin/smooth_weighted_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"google.golang.org/grpc/balancer"
)

func TestSW_Next(t *testing.T) {
w := NewSmoothRoundrobin()

s, _ := w.Next()
s, done := w.Next()
assert.Nil(t, s)
done(balancer.DoneInfo{})

w.Add("server1", 5)
s, _ = w.Next()
Expand Down

0 comments on commit beda079

Please sign in to comment.