Skip to content

Commit

Permalink
Update: fix typos
Browse files Browse the repository at this point in the history
  • Loading branch information
fufuok committed Oct 16, 2021
1 parent 6d9a37e commit 0dcd609
Show file tree
Hide file tree
Showing 13 changed files with 57 additions and 45 deletions.
17 changes: 9 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ func main() {
for i := 0; i < 9; i++ {
fmt.Print(balancer.Select(), " ")
}
fmt.Println()
}
```

Expand Down Expand Up @@ -84,7 +85,7 @@ nodes := []string{"A", "B", "C"}
2. use WeightedRoundRobin (WRR)

```go
var lb Balancer
var lb balancer.Balancer
lb = balancer.New(balancer.WeightedRoundRobin, wNodes, nil)
// or
Expand All @@ -102,7 +103,7 @@ nodes := []string{"A", "B", "C"}
3. use SmoothWeightedRoundRobin (SWRR)

```go
var lb Balancer
var lb balancer.Balancer
lb = balancer.New(balancer.SmoothWeightedRoundRobin, wNodes, nil)
// or
Expand All @@ -120,7 +121,7 @@ nodes := []string{"A", "B", "C"}
4. use WeightedRand (WR)

```go
var lb Balancer
var lb balancer.Balancer
lb = balancer.New(balancer.WeightedRand, wNodes, nil)
// or
Expand All @@ -138,7 +139,7 @@ nodes := []string{"A", "B", "C"}
5. use ConsistentHash

```go
var lb Balancer
var lb balancer.Balancer
lb = balancer.New(balancer.ConsistentHash, nil, nodes)
// or
Expand All @@ -156,7 +157,7 @@ nodes := []string{"A", "B", "C"}
6. use RoundRobin (RR)

```go
var lb Balancer
var lb balancer.Balancer
lb = balancer.New(balancer.RoundRobin, nil, nodes)
// or
Expand All @@ -174,7 +175,7 @@ nodes := []string{"A", "B", "C"}
7. use Random

```go
var lb Balancer
var lb balancer.Balancer
lb = balancer.New(balancer.Random, nil, nodes)
// or
Expand Down Expand Up @@ -255,14 +256,14 @@ BenchmarkBalancer/RoundRobin/10-4 53076963
BenchmarkBalancer/Random/10-4 64582524 18.02 ns/op 0 B/op 0 allocs/op
BenchmarkBalancer/WRR#01/100-4 32221255 37.31 ns/op 0 B/op 0 allocs/op
BenchmarkBalancer/SWRR#01/100-4 7337542 165.6 ns/op 0 B/op 0 allocs/op
BenchmarkBalancer/SWRR#01/100-4 7337542 165.6 ns/op 0 B/op 0 allocs/op
BenchmarkBalancer/WR#01/100-4 21253034 53.29 ns/op 0 B/op 0 allocs/op
BenchmarkBalancer/Hash#01/100-4 25851721 46.58 ns/op 0 B/op 0 allocs/op
BenchmarkBalancer/RoundRobin#01/100-4 51670482 22.59 ns/op 0 B/op 0 allocs/op
BenchmarkBalancer/Random#01/100-4 66175606 18.14 ns/op 0 B/op 0 allocs/op
BenchmarkBalancer/WRR#02/1000-4 28502208 42.09 ns/op 0 B/op 0 allocs/op
BenchmarkBalancer/SWRR#02/1000-4 872499 1391 ns/op 0 B/op 0 allocs/op
BenchmarkBalancer/SWRR#02/1000-4 872499 1391 ns/op 0 B/op 0 allocs/op
BenchmarkBalancer/WR#02/1000-4 16595787 71.57 ns/op 0 B/op 0 allocs/op
BenchmarkBalancer/Hash#02/1000-4 19103568 63.47 ns/op 0 B/op 0 allocs/op
BenchmarkBalancer/RoundRobin#02/1000-4 52725135 23.05 ns/op 0 B/op 0 allocs/op
Expand Down
34 changes: 17 additions & 17 deletions benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,36 +227,36 @@ func genItemsSlice(n int) (items []string) {
// BenchmarkBalancer/Random/10-4 64582524 18.02 ns/op 0 B/op 0 allocs/op
//
// BenchmarkBalancer/WRR#01/100-4 32221255 37.31 ns/op 0 B/op 0 allocs/op
// BenchmarkBalancer/SWRR#01/100-4 7337542 165.6 ns/op 0 B/op 0 allocs/op
// BenchmarkBalancer/SWRR#01/100-4 7337542 165.6 ns/op 0 B/op 0 allocs/op
// BenchmarkBalancer/WR#01/100-4 21253034 53.29 ns/op 0 B/op 0 allocs/op
// BenchmarkBalancer/Hash#01/100-4 25851721 46.58 ns/op 0 B/op 0 allocs/op
// BenchmarkBalancer/RoundRobin#01/100-4 51670482 22.59 ns/op 0 B/op 0 allocs/op
// BenchmarkBalancer/Random#01/100-4 66175606 18.14 ns/op 0 B/op 0 allocs/op
//
// BenchmarkBalancer/WRR#02/1000-4 28502208 42.09 ns/op 0 B/op 0 allocs/op
// BenchmarkBalancer/SWRR#02/1000-4 872499 1391 ns/op 0 B/op 0 allocs/op
// BenchmarkBalancer/SWRR#02/1000-4 872499 1391 ns/op 0 B/op 0 allocs/op
// BenchmarkBalancer/WR#02/1000-4 16595787 71.57 ns/op 0 B/op 0 allocs/op
// BenchmarkBalancer/Hash#02/1000-4 19103568 63.47 ns/op 0 B/op 0 allocs/op
// BenchmarkBalancer/RoundRobin#02/1000-4 52725135 23.05 ns/op 0 B/op 0 allocs/op
// BenchmarkBalancer/Random#02/1000-4 66541184 18.24 ns/op 0 B/op 0 allocs/op
//
// BenchmarkBalancer/WRR#03/10000-4 27912939 42.67 ns/op 0 B/op 0 allocs/op
// BenchmarkBalancer/SWRR#03/10000-4 86983 14019 ns/op 0 B/op 0 allocs/op
// BenchmarkBalancer/SWRR#03/10000-4 86983 14019 ns/op 0 B/op 0 allocs/op
// BenchmarkBalancer/WR#03/10000-4 12691062 92.73 ns/op 0 B/op 0 allocs/op
// BenchmarkBalancer/Hash#03/10000-4 16084016 73.96 ns/op 0 B/op 0 allocs/op
// BenchmarkBalancer/RoundRobin#03/10000-4 52327888 24.05 ns/op 0 B/op 0 allocs/op
// BenchmarkBalancer/Random#03/10000-4 66457050 18.17 ns/op 0 B/op 0 allocs/op
//
// BenchmarkBalancer/WRR#04/100000-4 24896972 43.20 ns/op 0 B/op 0 allocs/op
// BenchmarkBalancer/SWRR#04/100000-4 7046 173884 ns/op 0 B/op 0 allocs/op
// BenchmarkBalancer/WR#04/100000-4 9491140 127.3 ns/op 0 B/op 0 allocs/op
// BenchmarkBalancer/SWRR#04/100000-4 7046 173884 ns/op 0 B/op 0 allocs/op
// BenchmarkBalancer/WR#04/100000-4 9491140 127.3 ns/op 0 B/op 0 allocs/op
// BenchmarkBalancer/Hash#04/100000-4 16090567 76.46 ns/op 0 B/op 0 allocs/op
// BenchmarkBalancer/RoundRobin#04/100000-4 49422337 24.06 ns/op 0 B/op 0 allocs/op
// BenchmarkBalancer/Random#04/100000-4 62700792 18.52 ns/op 0 B/op 0 allocs/op
//
// BenchmarkBalancer/WRR#05/1000000-4 24038544 47.01 ns/op 0 B/op 0 allocs/op
// BenchmarkBalancer/SWRR#05/1000000-4 381 3108476 ns/op 0 B/op 0 allocs/op
// BenchmarkBalancer/WR#05/1000000-4 4863207 259.8 ns/op 0 B/op 0 allocs/op
// BenchmarkBalancer/SWRR#05/1000000-4 381 3108476 ns/op 0 B/op 0 allocs/op
// BenchmarkBalancer/WR#05/1000000-4 4863207 259.8 ns/op 0 B/op 0 allocs/op
// BenchmarkBalancer/Hash#05/1000000-4 16194163 74.15 ns/op 0 B/op 0 allocs/op
// BenchmarkBalancer/RoundRobin#05/1000000-4 52971156 22.70 ns/op 0 B/op 0 allocs/op
// BenchmarkBalancer/Random#05/1000000-4 40018782 26.25 ns/op 0 B/op 0 allocs/op
Expand All @@ -269,36 +269,36 @@ func genItemsSlice(n int) (items []string) {
// BenchmarkBalancerParallel/Random/10-4 17698065 67.72 ns/op 0 B/op 0 allocs/op
//
// BenchmarkBalancerParallel/WRR#01/100-4 13133222 88.99 ns/op 0 B/op 0 allocs/op
// BenchmarkBalancerParallel/SWRR#01/100-4 6563328 181.4 ns/op 0 B/op 0 allocs/op
// BenchmarkBalancerParallel/SWRR#01/100-4 6563328 181.4 ns/op 0 B/op 0 allocs/op
// BenchmarkBalancerParallel/WR#01/100-4 26578320 79.94 ns/op 0 B/op 0 allocs/op
// BenchmarkBalancerParallel/Hash#01/100-4 16083211 74.67 ns/op 0 B/op 0 allocs/op
// BenchmarkBalancerParallel/RoundRobin#01/100-4 12263182 91.64 ns/op 0 B/op 0 allocs/op
// BenchmarkBalancerParallel/Random#01/100-4 17741816 67.81 ns/op 0 B/op 0 allocs/op
//
// BenchmarkBalancerParallel/WRR#02/1000-4 11212780 104.0 ns/op 0 B/op 0 allocs/op
// BenchmarkBalancerParallel/SWRR#02/1000-4 805879 1474 ns/op 0 B/op 0 allocs/op
// BenchmarkBalancerParallel/WRR#02/1000-4 11212780 104.0 ns/op 0 B/op 0 allocs/op
// BenchmarkBalancerParallel/SWRR#02/1000-4 805879 1474 ns/op 0 B/op 0 allocs/op
// BenchmarkBalancerParallel/WR#02/1000-4 15821539 72.37 ns/op 0 B/op 0 allocs/op
// BenchmarkBalancerParallel/Hash#02/1000-4 14478384 81.82 ns/op 0 B/op 0 allocs/op
// BenchmarkBalancerParallel/RoundRobin#02/1000-4 12103447 88.15 ns/op 0 B/op 0 allocs/op
// BenchmarkBalancerParallel/Random#02/1000-4 17729145 67.81 ns/op 0 B/op 0 allocs/op
//
// BenchmarkBalancerParallel/WRR#03/10000-4 10567130 105.9 ns/op 0 B/op 0 allocs/op
// BenchmarkBalancerParallel/SWRR#03/10000-4 81170 14685 ns/op 0 B/op 0 allocs/op
// BenchmarkBalancerParallel/WRR#03/10000-4 10567130 105.9 ns/op 0 B/op 0 allocs/op
// BenchmarkBalancerParallel/SWRR#03/10000-4 81170 14685 ns/op 0 B/op 0 allocs/op
// BenchmarkBalancerParallel/WR#03/10000-4 14379578 78.16 ns/op 0 B/op 0 allocs/op
// BenchmarkBalancerParallel/Hash#03/10000-4 14215629 84.50 ns/op 0 B/op 0 allocs/op
// BenchmarkBalancerParallel/RoundRobin#03/10000-4 13372892 90.45 ns/op 0 B/op 0 allocs/op
// BenchmarkBalancerParallel/Random#03/10000-4 17676268 67.92 ns/op 0 B/op 0 allocs/op
//
// BenchmarkBalancerParallel/WRR#04/100000-4 11561236 110.4 ns/op 0 B/op 0 allocs/op
// BenchmarkBalancerParallel/SWRR#04/100000-4 6835 175792 ns/op 0 B/op 0 allocs/op
// BenchmarkBalancerParallel/WRR#04/100000-4 11561236 110.4 ns/op 0 B/op 0 allocs/op
// BenchmarkBalancerParallel/SWRR#04/100000-4 6835 175792 ns/op 0 B/op 0 allocs/op
// BenchmarkBalancerParallel/WR#04/100000-4 22145756 54.60 ns/op 0 B/op 0 allocs/op
// BenchmarkBalancerParallel/Hash#04/100000-4 14285690 84.04 ns/op 0 B/op 0 allocs/op
// BenchmarkBalancerParallel/RoundRobin#04/100000-4 12744205 90.57 ns/op 0 B/op 0 allocs/op
// BenchmarkBalancerParallel/Random#04/100000-4 17859376 67.19 ns/op 0 B/op 0 allocs/op
//
// BenchmarkBalancerParallel/WRR#05/1000000-4 11473530 109.0 ns/op 0 B/op 0 allocs/op
// BenchmarkBalancerParallel/SWRR#05/1000000-4 379 3127391 ns/op 7 B/op 0 allocs/op
// BenchmarkBalancerParallel/WR#05/1000000-4 10868928 100.4 ns/op 0 B/op 0 allocs/op
// BenchmarkBalancerParallel/WRR#05/1000000-4 11473530 109.0 ns/op 0 B/op 0 allocs/op
// BenchmarkBalancerParallel/SWRR#05/1000000-4 379 3127391 ns/op 7 B/op 0 allocs/op
// BenchmarkBalancerParallel/WR#05/1000000-4 10868928 100.4 ns/op 0 B/op 0 allocs/op
// BenchmarkBalancerParallel/Hash#05/1000000-4 14018941 85.23 ns/op 0 B/op 0 allocs/op
// BenchmarkBalancerParallel/RoundRobin#05/1000000-4 11614006 87.05 ns/op 0 B/op 0 allocs/op
// BenchmarkBalancerParallel/Random#05/1000000-4 17565664 68.35 ns/op 0 B/op 0 allocs/op
1 change: 1 addition & 0 deletions default.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func Reset() {
DefaultBalancer.Reset()
}

// Update reinitialize the balancer items.
func Update(items interface{}) bool {
return DefaultBalancer.Update(items)
}
11 changes: 10 additions & 1 deletion examples/new/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
func main() {
var lb balancer.Balancer

// SmoothWeightedRoundRobin and WeightedRoundRobin use map[string]int
// SmoothWeightedRoundRobin / WeightedRoundRobin / WeightedRand use map[string]int
wNodes := map[string]int{
"A": 5,
"B": 1,
Expand All @@ -19,9 +19,18 @@ func main() {
lb = balancer.New(balancer.WeightedRoundRobin, wNodes, nil)
fmt.Println("balancer name:", lb.Name())

lb = balancer.New(balancer.WeightedRand, wNodes, nil)
fmt.Println("balancer name:", lb.Name())

lb = balancer.New(balancer.SmoothWeightedRoundRobin, wNodes, nil)
fmt.Println("balancer name:", lb.Name())

// result of smooth selection is similar to: A A C A B A A
for i := 0; i < 7; i++ {
fmt.Print(lb.Select(), " ")
}
fmt.Println()

// RoundRobin / Random / ConsistentHash use []string
nodes := []string{"A", "B", "C"}
lb = balancer.New(balancer.ConsistentHash, nil, nodes)
Expand Down
2 changes: 1 addition & 1 deletion examples/random/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func main() {
fmt.Println("balancer name:", lb.Name())

// random
for i := 0; i < 5; i++ {
for i := 0; i < 9; i++ {
fmt.Print(lb.Select(), " ")
}
fmt.Println()
Expand Down
3 changes: 2 additions & 1 deletion examples/wrr/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ func main() {
// reinitialize the balancer items
lb.Update(wNodes)

// when the weight difference is large, it is not smooth: E E E E E C D E E E E E C D
// when the weight difference is large, it is not smooth
// the result is similar to: E E E E E C D E E E E E C D
for i := 0; i < 14; i++ {
fmt.Print(lb.Select(), " ")
}
Expand Down
13 changes: 7 additions & 6 deletions hash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@ func TestConsistentHash(t *testing.T) {
}

func TestConsistentHash_C(t *testing.T) {
var a int64
var c int64
item := ""
nodes := []string{"A", "B", "C", "D"}
lb := NewConsistentHash(nodes)

Expand All @@ -119,17 +120,17 @@ func TestConsistentHash_C(t *testing.T) {
go func() {
defer wg.Done()
for j := 0; j < 2000; j++ {
switch lb.Select("192.168.1.100") {
case "A":
atomic.AddInt64(&a, 1)
switch item = lb.Select("192.168.1.7"); item {
case "C":
atomic.AddInt64(&c, 1)
default:
}
}
}()
}
wg.Wait()

if atomic.LoadInt64(&a) != 1000000 {
t.Fatal("hash wrong: a")
if atomic.LoadInt64(&c) != 1000000 {
t.Fatalf("hash expected C == 1000000, actual C == %d, item: %s", atomic.LoadInt64(&c), item)
}
}
1 change: 1 addition & 0 deletions random.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/fufuok/balancer/utils"
)

// Random
type random struct {
items []string
count uint32
Expand Down
7 changes: 3 additions & 4 deletions random_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,20 +40,19 @@ func TestRandom(t *testing.T) {
lb.Add("A")
all := lb.All().([]string)
if strings.Join(all, "") != "ABCDAACA" {
t.Fatalf("hash all() wrong")
t.Fatal("r all() wrong")
}

lb.Remove("C")
all = lb.All().([]string)
if strings.Join(all, "") != "ABDAACA" {
t.Fatalf("hash all() wrong")
t.Fatal("r all() wrong")
}

lb.Remove("A", true)
all = lb.All().([]string)
if strings.Join(all, "") != "BDC" {
t.Log(all)
t.Fatalf("hash all() wrong")
t.Fatal("r all() wrong")
}

lb.RemoveAll()
Expand Down
7 changes: 3 additions & 4 deletions rr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,20 +79,19 @@ func TestRoundRobin(t *testing.T) {
lb.Add("A")
all := lb.All().([]string)
if strings.Join(all, "") != "ABCDAACA" {
t.Fatalf("hash all() wrong")
t.Fatal("rr all() wrong")
}

lb.Remove("C")
all = lb.All().([]string)
if strings.Join(all, "") != "ABDAACA" {
t.Fatalf("hash all() wrong")
t.Fatal("rr all() wrong")
}

lb.Remove("A", true)
all = lb.All().([]string)
if strings.Join(all, "") != "BDC" {
t.Log(all)
t.Fatalf("hash all() wrong")
t.Fatal("rr all() wrong")
}

lb.RemoveAll()
Expand Down
2 changes: 1 addition & 1 deletion swrr.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"sync"
)

// smooth weighted round-robin balancing
// Smooth weighted round-robin balancing
// Ref: https://github.com/phusion/nginx/commit/27e94984486058d73157038f7950a0a36ecc6e35
type swrr struct {
items []*swrrItem
Expand Down
2 changes: 1 addition & 1 deletion wr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func TestWeightedRand(t *testing.T) {
}
all, ok = lb.All().(map[string]int)
if !ok || all["Y"] != 1 {
t.Fatal("swrr all() wrong")
t.Fatal("wr all() wrong")
}
item = lb.Select()
if item != "Y" {
Expand Down
2 changes: 1 addition & 1 deletion wrr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func TestWeightedRoundRobin(t *testing.T) {
}
all, ok = lb.All().(map[string]int)
if !ok || all["Y"] != 1 {
t.Fatal("swrr all() wrong")
t.Fatal("wrr all() wrong")
}
item = lb.Select()
if item != "Y" {
Expand Down

0 comments on commit 0dcd609

Please sign in to comment.