-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbalancer.go
266 lines (228 loc) · 5.1 KB
/
balancer.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
package producer
import (
"context"
"errors"
"fmt"
"time"
"github.com/hopingtop/balancer-nsqd-producer/algorithm"
"github.com/nsqio/go-nsq"
)
type (
// Options 负载均衡器配置文件
Options struct {
Addrs map[string]int
Retry int
Mode BalanceMode
PingInterval int // s
PingTimeout int
}
// Conn nsqd 链接
Conn struct {
producer *nsq.Producer
connAddr string
errCount int
weight int
}
// BalanceMode 算法模式
BalanceMode int
algorithmMode interface {
GetAll() (objPool []interface{})
Get() (obj interface{}, index int)
Put(obj interface{}, weight ...int)
Del(int)
}
)
// 支持如下算法
const (
PollingMode BalanceMode = iota
RandomMode
SmoothWeightMode
)
// error 定义
var (
ErrNoAvailableConns = errors.New("no available conns")
)
// Balancer 负载均衡生成器
type Balancer struct {
addrs map[string]int // string nsqd 地址 int 权重 非权重 int = 0
balanceWay algorithmMode
errConns chan *Conn // 存在异常的链接, 等待 retryConn 重试链接
ErrorsChan chan error // nsqd 链接错误
pingInterval int // retryConn 间隔
pingTimeout int // pingTimeout / retryConn 连续 ping 这么多次,则返回 ErrorsChan
retry int //如果连续未成功则返回 error
ctx context.Context
cancel context.CancelFunc
}
// NewBalancer 生成负载均衡器 opt.Addrs 非权重 int = 0 config = nsq.NewConfig()
func NewBalancer(opt Options, config *nsq.Config) (*Balancer, error) {
if err := Validate(opt); err != nil {
return nil, err
}
bl := Balancer{
addrs: opt.Addrs,
pingInterval: opt.PingInterval,
pingTimeout: opt.PingTimeout,
errConns: make(chan *Conn, 100),
ErrorsChan: make(chan error, 100),
retry: opt.Retry,
}
bl.ctx, bl.cancel = context.WithCancel(context.Background())
bl.setAlgorithm(opt.Mode)
if config == nil {
return nil, fmt.Errorf("config nil")
}
for addr, wright := range opt.Addrs {
pd, err := nsq.NewProducer(addr, config)
if err != nil {
return nil, err
}
pdConn := Conn{
producer: pd,
connAddr: addr,
errCount: 0,
weight: 0,
}
bl.balanceWay.Put(&pdConn, wright)
}
bl.retryConns()
return &bl, nil
}
func (bl *Balancer) setAlgorithm(mode BalanceMode) {
switch mode {
case PollingMode:
bl.balanceWay = algorithm.NewPolling()
case RandomMode:
bl.balanceWay = algorithm.NewRandom()
case SmoothWeightMode:
bl.balanceWay = algorithm.NewSmoothWeight()
default:
bl.balanceWay = algorithm.NewPolling()
}
}
// Validate Options 参数验证
func Validate(opt Options) error {
if len(opt.Addrs) == 0 {
return errors.New("invalid addr")
}
for addr, weight := range opt.Addrs {
if weight < 0 {
return errors.New(addr + " invalid weight")
}
}
if opt.Retry < 1 || opt.Retry > 50 {
return errors.New("invalid retry")
}
if opt.PingInterval > 5 || opt.PingInterval < 1 {
return errors.New("invalid pingInterval")
}
if opt.PingTimeout/opt.PingInterval <= 0 {
return errors.New("invalid pingTimeout")
}
return nil
}
// Publish 生产消息写入 nsqd
func (bl *Balancer) Publish(topic string, body []byte) error {
retry := bl.retry
var err error
get:
retry--
pd, index := bl.balanceWay.Get()
if index <= -1 {
return ErrNoAvailableConns
}
if pd == nil {
if retry < 0 {
return ErrNoAvailableConns
}
goto get
}
conn := pd.(*Conn)
if conn == nil {
if retry < 0 {
return ErrNoAvailableConns
}
bl.errConns <- conn
bl.balanceWay.Del(index)
goto get
}
err = conn.producer.Publish(topic, body)
if err != nil {
bl.errConns <- conn
bl.balanceWay.Del(index)
if retry < 0 {
return err
}
}
return nil
}
// MultiPublish 生产多条消息一次写入 nsqd
func (bl *Balancer) MultiPublish(topic string, body [][]byte) error {
retry := bl.retry
var err error
get:
retry--
pd, index := bl.balanceWay.Get()
if index <= -1 {
return ErrNoAvailableConns
}
if pd == nil {
if retry < 0 {
return ErrNoAvailableConns
}
goto get
}
conn := pd.(*Conn)
if conn == nil {
if retry < 0 {
return ErrNoAvailableConns
}
bl.errConns <- conn
bl.balanceWay.Del(index)
goto get
}
err = conn.producer.MultiPublish(topic, body)
if err != nil {
bl.errConns <- conn
bl.balanceWay.Del(index)
if retry < 0 {
return err
}
}
return nil
}
func (bl *Balancer) retryConns() {
go func(ctx context.Context) {
for {
select {
case conn := <-bl.errConns:
if err := conn.producer.Ping(); err != nil {
conn.errCount++
if conn.errCount >= (bl.pingTimeout / bl.pingInterval) {
bl.ErrorsChan <- err
conn.errCount = 0
}
time.Sleep(time.Second * time.Duration(bl.pingInterval))
bl.errConns <- conn
} else {
bl.balanceWay.Put(conn, conn.weight)
}
case <-ctx.Done():
fmt.Println("retry conns goroutine exiting")
return
}
}
}(bl.ctx)
}
// CloseAll 关闭所有链接
func (bl *Balancer) CloseAll() {
// 退出 retryConns goroutine
bl.cancel()
objs := bl.balanceWay.GetAll()
for _, obj := range objs {
conn := obj.(*Conn)
if conn != nil {
conn.producer.Stop()
}
}
}