/
broker.go
192 lines (167 loc) · 3.91 KB
/
broker.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
package server
import (
"context"
pb "github.com/zhihanii/im-pusher/api/broker"
"github.com/zhihanii/im-pusher/api/protocol"
"github.com/zhihanii/im-pusher/internal/dispatcher/conf"
"github.com/zhihanii/registry"
"github.com/zhihanii/zlog"
clientv3 "go.etcd.io/etcd/client/v3"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"sync/atomic"
"time"
)
type Broker struct {
c *conf.Config
serverId string
cli pb.BrokerClient
pushCh []chan *pb.PushMsgReq
roomCh []chan *pb.BroadcastRoomReq
broadcastCh chan *pb.BroadcastReq
pushChNum uint64
roomChNum uint64
routineSize uint64
ctx context.Context
cancel context.CancelFunc
input chan *protocol.TransMessage
output chan *messageSet
buffer *messageSet
timer *time.Timer
timerFired bool
}
func newBroker(config *conf.Config, serverId string, etcdClient *clientv3.Client) *Broker {
b := &Broker{
c: config,
serverId: serverId,
}
var err error
etcdResolver, err := registry.NewGRPCEtcdResolverBuilder(etcdClient)
if err != nil {
zlog.Errorf("resolver new builder:%v", err)
}
conn, err := grpc.Dial("etcd:///broker-service", grpc.WithResolvers(etcdResolver),
grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
panic(err)
}
zlog.Infoln("成功与broker建立grpc连接")
b.cli = pb.NewBrokerClient(conn)
b.pushCh = make([]chan *pb.PushMsgReq, 1)
b.pushChNum = 1
b.ctx, b.cancel = context.WithCancel(context.Background())
b.input = make(chan *protocol.TransMessage)
b.output = make(chan *messageSet)
b.buffer = newMessageSet(b.c)
go b.run()
go b.sender()
return b
}
func (b *Broker) Push(msg *protocol.TransMessage) {
b.input <- msg
}
func (b *Broker) run() {
var output chan<- *messageSet
var timerChan <-chan time.Time
for {
select {
case msg, ok := <-b.input:
if !ok {
zlog.Infoln("input chan closed")
return
}
if msg == nil {
continue
}
//todo if needs retry
if err := b.buffer.add(msg); err != nil {
continue
}
if b.c.ServerOptions.Flush.Frequency > 0 && b.timer == nil {
b.timer = time.NewTimer(b.c.ServerOptions.Flush.Frequency)
timerChan = b.timer.C
}
case <-timerChan:
b.timerFired = true
case output <- b.buffer:
b.rollOver()
timerChan = nil
}
if b.timerFired || b.buffer.readyToFlush() {
output = b.output
} else {
output = nil
}
}
}
func (b *Broker) sender() {
for set := range b.output {
if !set.empty() {
req := set.buildRequest()
_, err := b.cli.Push(b.ctx, req)
if err != nil {
zlog.Errorf("broker client push error:%v", err)
}
}
}
}
func (b *Broker) rollOver() {
if b.timer != nil {
b.timer.Stop()
}
b.timer = nil
b.timerFired = false
b.buffer = newMessageSet(b.c)
}
//func (c *Connect) Push(req *pb.PushMsgReq) (err error) {
// idx := atomic.AddUint64(&c.pushChNum, 1) % c.routineSize
// c.pushCh[idx] <- req
// return
//}
func (b *Broker) BroadcastRoom(req *pb.BroadcastRoomReq) (err error) {
idx := atomic.AddUint64(&b.roomChNum, 1) % b.routineSize
b.roomCh[idx] <- req
return
}
func (b *Broker) Broadcast(req *pb.BroadcastReq) (err error) {
b.broadcastCh <- req
return
}
func (b *Broker) process(
pushCh chan *pb.PushMsgReq,
roomCh chan *pb.BroadcastRoomReq,
broadcastCh chan *pb.BroadcastReq,
) {
for {
select {
case req := <-pushCh:
_, err := b.cli.PushMsg(context.Background(), &pb.PushMsgReq{
ProtoOp: req.ProtoOp,
Keys: req.Keys,
Msg: req.Msg,
})
if err != nil {
//log
}
case req := <-roomCh:
_, err := b.cli.BroadcastRoom(context.Background(), &pb.BroadcastRoomReq{
Msg: req.Msg,
RoomId: req.RoomId,
})
if err != nil {
//log
}
case req := <-broadcastCh:
_, err := b.cli.Broadcast(context.Background(), &pb.BroadcastReq{
ProtoOp: req.ProtoOp,
Msg: req.Msg,
Speed: req.Speed,
})
if err != nil {
//log
}
case <-b.ctx.Done():
return
}
}
}