forked from Terry-Mao/gopush-cluster
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rand_lb.go
157 lines (144 loc) · 4.09 KB
/
rand_lb.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// Copyright © 2014 Terry Mao, LiuDing All rights reserved.
// This file is part of gopush-cluster.
// gopush-cluster is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// gopush-cluster is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with gopush-cluster. If not, see <http://www.gnu.org/licenses/>.
package rpc
import (
"errors"
"fmt"
"github.com/golang/glog"
"math/rand"
"net/rpc"
"time"
)
const (
randLBRetryCHLength = 10
)
var (
ErrRandLBLength = errors.New("clients and addrs length not match")
ErrRandLBAddr = errors.New("clients map no addr key")
)
// random load balancing object
type RandLB struct {
Clients map[string]*rpc.Client
addrs []string
length int
exitCH chan int
}
// NewRandLB new a random load balancing object.
func NewRandLB(clients map[string]*rpc.Client, addrs []string, service string, retry, ping time.Duration, check bool) (*RandLB, error) {
length := len(clients)
if length != len(addrs) {
glog.Errorf("clients: %d, addrs: %d not equal", length, len(addrs))
return nil, ErrRandLBLength
}
for _, addr := range addrs {
if _, ok := clients[addr]; !ok {
glog.Errorf("addr: \"%s\" not exist in clients map", addr)
return nil, ErrRandLBAddr
}
}
r := &RandLB{Clients: clients, addrs: addrs, length: length}
if check && length > 0 {
glog.Info("rpc ping start")
r.ping(service, retry, ping)
}
return r, nil
}
// Get get a rpc client randomly.
func (r *RandLB) Get() *rpc.Client {
if len(r.addrs) == 0 {
return nil
}
addr := r.addrs[rand.Intn(r.length)]
glog.V(1).Infof("rand hit rpc node: \"%s\"", addr)
client, _ := r.Clients[addr]
return client
}
// Stop stop the retry connect goroutine and ping goroutines.
func (r *RandLB) Stop() {
if r.exitCH != nil {
close(r.exitCH)
}
glog.Info("stop the randlb retry connect goroutine and ping goroutines")
}
// Destroy release the rpc.Client resource.
func (r *RandLB) Destroy() {
r.Stop()
for _, client := range r.Clients {
if client != nil {
if err := client.Close(); err != nil {
glog.Errorf("client.Close() error(%v)", err)
}
}
}
}
// ping do a ping, if failed then retry.
func (r *RandLB) ping(service string, retry, ping time.Duration) {
method := fmt.Sprintf("%s.Ping", service)
retryCH := make(chan string, randLBRetryCHLength)
r.exitCH = make(chan int, 1)
for _, addr := range r.addrs {
// warn: closures problem
go func(addr string) {
glog.Infof("\"%s\" rpc ping goroutine start", addr)
ret := 0
for {
select {
case <-r.exitCH:
glog.Infof("\"%s\" rpc ping goroutine exit", addr)
return
default:
}
// get client for ping
client, _ := r.Clients[addr]
if err := client.Call(method, 0, &ret); err != nil {
// if failed send to chan reconnect, sleep
client.Close()
retryCH <- addr
glog.Errorf("client.Call(\"%s\", 0, &ret) error(%v), retry", method, err)
time.Sleep(retry)
continue
}
// if ok, sleep
glog.V(2).Infof("\"%s\": rpc ping ok", addr)
time.Sleep(ping)
}
}(addr)
}
// rpc retry connect
go func() {
var retryAddr string
glog.Info("rpc retry connect goroutine start")
for {
select {
case retryAddr = <-retryCH:
case <-r.exitCH:
glog.Info("rpc retry connect goroutine exit")
return
}
rpcTmp, err := rpc.Dial("tcp", retryAddr)
if err != nil {
glog.Errorf("rpc.Dial(\"tcp\", %s) error(%s)", retryAddr, err)
continue
}
glog.Infof("rpc.Dial(\"tcp\", %s) retry succeed", retryAddr)
// copy-on-write
tmpClients := make(map[string]*rpc.Client, r.length)
for addr, client := range r.Clients {
tmpClients[addr] = client
}
tmpClients[retryAddr] = rpcTmp
// atomic update clients
r.Clients = tmpClients
}
}()
}