From 326bdfc7e4e29e98dd043ad3b12c9c46aaa9165b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E5=BE=B7=E6=BB=A1?= Date: Thu, 1 Apr 2021 20:20:04 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E8=BD=AE=E8=AF=A2=E8=B4=9F?= =?UTF-8?q?=E8=BD=BD=E7=AD=96=E7=95=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- balance_test.go | 67 +++++++++++++++++++++++++++++++++++++++++ dispatch/round_robin.go | 55 +++++++++++++++++++++++++++++++++ go.mod | 5 ++- go.sum | 7 ++--- rand_test.go | 41 ------------------------- server.go | 4 +++ 6 files changed, 133 insertions(+), 46 deletions(-) create mode 100644 balance_test.go create mode 100644 dispatch/round_robin.go delete mode 100644 rand_test.go diff --git a/balance_test.go b/balance_test.go new file mode 100644 index 0000000..94186d1 --- /dev/null +++ b/balance_test.go @@ -0,0 +1,67 @@ +// Package dispatch... +// +// Description : dispatch... +// +// Author : go_developer@163.com<张德满> +// +// Date : 2021-04-01 6:48 下午 +package balance + +import ( + "fmt" + "testing" + + "github.com/go-playground/assert/v2" + + "github.com/go-developer/balance/dispatch" + + "github.com/go-developer/balance/define" +) + +var list = []*define.SeverNode{ + {"127.0.0.1:8080", "127.0.0.1", 8080, 0, 1}, + {"127.0.0.1:8081", "127.0.0.1", 8081, 0, 1}, + {"127.0.0.1:8082", "127.0.0.1", 8082, 0, 1}, + {"127.0.0.1:8083", "127.0.0.1", 8083, 0, 1}, +} + +// TestRand 测试随机选节点 +// +// Author : go_developer@163.com<张德满> +// +// Date : 6:48 下午 2021/4/1 +func TestRand(t *testing.T) { + result := make(map[string]int) + + s := NewServer(list, dispatch.NewRand()) + for i := 0; i < 10000; i++ { + node, _ := s.Get() + if _, exist := result[node]; !exist { + result[node] = 0 + } + result[node]++ + } + fmt.Printf("%+v", result) +} + +// TestRoundRobin 轮询测试 +// +// Author : go_developer@163.com<张德满> +// +// Date : 8:12 下午 2021/4/1 +func TestRoundRobin(t *testing.T) { + result := make(map[string]int) + + s := NewServer(list, dispatch.NewRoundRobin()) + for i := 0; i < 10000; i++ { + node, _ := s.Get() + if _, exist := result[node]; !exist { + result[node] = 0 + } + result[node]++ + } + assert.Equal(t, 2500, result["127.0.0.1:8080"]) + assert.Equal(t, 2500, result["127.0.0.1:8081"]) + assert.Equal(t, 2500, result["127.0.0.1:8082"]) + assert.Equal(t, 2500, result["127.0.0.1:8083"]) +} diff --git a/dispatch/round_robin.go b/dispatch/round_robin.go new file mode 100644 index 0000000..2cda071 --- /dev/null +++ b/dispatch/round_robin.go @@ -0,0 +1,55 @@ +// Package dispatch ... +// +// Description : Round Robin 轮询选择服务器 +// +// Author : go_developer@163.com<张德满> +// +// Date : 2021-04-01 8:04 下午 +package dispatch + +import ( + "fmt" + + "github.com/go-developer/balance/define" + "github.com/go-developer/gopkg/easylock" +) + +// NewRoundRobin 轮询调度 +// +// Author : go_developer@163.com<张德满> +// +// Date : 8:07 下午 2021/4/1 +func NewRoundRobin() IDispatch { + return &RoundRobin{ + lock: easylock.NewLock(), + nextNodeIndex: 0, + } +} + +// RoundRobin 轮询选择机器 +// +// Author : go_developer@163.com<张德满> +// +// Date : 8:06 下午 2021/4/1 +type RoundRobin struct { + lock easylock.EasyLock + nextNodeIndex int +} + +// Get ... +// +// Author : go_developer@163.com<张德满> +// +// Date : 8:05 下午 2021/4/1 +func (r *RoundRobin) Get(nodeList []*define.SeverNode) (string, *define.Error) { + if len(nodeList) == 0 { + return "", define.NewError(define.ErrorTypeNodeListEmpty, "服务器可用节点为空") + } + _ = r.lock.Lock() + defer func() { + _ = r.lock.Unlock() + }() + node := fmt.Sprintf("%s:%d", nodeList[r.nextNodeIndex].Host, nodeList[r.nextNodeIndex].Port) + r.nextNodeIndex = (r.nextNodeIndex + 1) % len(nodeList) + return node, nil +} diff --git a/go.mod b/go.mod index 74cf717..5cbcfe2 100644 --- a/go.mod +++ b/go.mod @@ -2,4 +2,7 @@ module github.com/go-developer/balance go 1.16 -require github.com/go-developer/gopkg v0.0.0-20210401085834-5312092ad68b +require ( + github.com/go-developer/gopkg v0.0.0-20210401121730-91894a6f9659 + github.com/go-playground/assert/v2 v2.0.1 +) diff --git a/go.sum b/go.sum index 0ecc239..785a4d8 100644 --- a/go.sum +++ b/go.sum @@ -8,10 +8,9 @@ github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMo github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ= github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI= github.com/gin-gonic/gin v1.6.3/go.mod h1:75u5sXoLsGZoRN5Sgbi1eraJ4GU3++wFwWzhwvtwp4M= -github.com/go-developer/gopkg v0.0.0-20210326100134-08a8a8015cc5 h1:Id0MV9wihSMCncZ7OshI2ZNouijF9lrbIy+6vI6MoLM= -github.com/go-developer/gopkg v0.0.0-20210326100134-08a8a8015cc5/go.mod h1:DZcG3JkuXhqmHwZMrSJefYWNPns7nYBhA9q4ocmpG5o= -github.com/go-developer/gopkg v0.0.0-20210401085834-5312092ad68b h1:PduruFtrPw7BoxesthPBbYazz6jueMhBEFHxRA3Rd1I= -github.com/go-developer/gopkg v0.0.0-20210401085834-5312092ad68b/go.mod h1:DZcG3JkuXhqmHwZMrSJefYWNPns7nYBhA9q4ocmpG5o= +github.com/go-developer/gopkg v0.0.0-20210401121730-91894a6f9659 h1:wHI+HmfpLeG9B6EL47jLxNl4zB5dSW3ihtlWxvRrDzk= +github.com/go-developer/gopkg v0.0.0-20210401121730-91894a6f9659/go.mod h1:DZcG3JkuXhqmHwZMrSJefYWNPns7nYBhA9q4ocmpG5o= +github.com/go-playground/assert/v2 v2.0.1 h1:MsBgLAaY856+nPRTKrp3/OZK38U/wa0CcBYNjji3q3A= github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4= github.com/go-playground/locales v0.13.0/go.mod h1:taPMhCMXrRLJO55olJkUXHZBHCxTMfnGwq/HNwmWNS8= github.com/go-playground/universal-translator v0.17.0/go.mod h1:UkSxE5sNxxRwHyU+Scu5vgOQjsIJAF8j9muTVoKLVtA= diff --git a/rand_test.go b/rand_test.go deleted file mode 100644 index e02c798..0000000 --- a/rand_test.go +++ /dev/null @@ -1,41 +0,0 @@ -// Package dispatch... -// -// Description : dispatch... -// -// Author : go_developer@163.com<张德满> -// -// Date : 2021-04-01 6:48 下午 -package balance - -import ( - "fmt" - "testing" - - "github.com/go-developer/balance/dispatch" - - "github.com/go-developer/balance/define" -) - -// TestRand 测试随机选节点 -// -// Author : go_developer@163.com<张德满> -// -// Date : 6:48 下午 2021/4/1 -func TestRand(t *testing.T) { - result := make(map[string]int) - list := []*define.SeverNode{ - {"127.0.0.1:8080", "127.0.0.1", 8080, 0, 1}, - {"127.0.0.1:8081", "127.0.0.1", 8081, 0, 1}, - {"127.0.0.1:8082", "127.0.0.1", 8082, 0, 1}, - {"127.0.0.1:8083", "127.0.0.1", 8083, 0, 1}, - } - s := NewServer(list, dispatch.NewRand()) - for i := 0; i < 10000; i++ { - node, _ := s.Get() - if _, exist := result[node]; !exist { - result[node] = 0 - } - result[node]++ - } - fmt.Printf("%+v", result) -} diff --git a/server.go b/server.go index 3580bc3..bb7a3b3 100644 --- a/server.go +++ b/server.go @@ -74,5 +74,9 @@ func (s *Server) Remove(nodeID string) { // // Date : 5:17 下午 2021/4/1 func (s *Server) Get() (string, *define.Error) { + _ = s.lock.RLock() + defer func() { + _ = s.lock.RUnlock() + }() return s.Balance.Get(s.NodeList) }