-
Notifications
You must be signed in to change notification settings - Fork 278
/
webbhook.go
408 lines (370 loc) · 12.5 KB
/
webbhook.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
package server
import (
"bytes"
"compress/gzip"
"context"
"encoding/json"
"errors"
"fmt"
"net"
"net/http"
"strconv"
"sync"
"time"
"github.com/WuKongIM/WuKongIM/pkg/grpcpool"
"github.com/WuKongIM/WuKongIM/pkg/wkhook"
"github.com/WuKongIM/WuKongIM/pkg/wklog"
"github.com/WuKongIM/WuKongIM/pkg/wkutil"
wkproto "github.com/WuKongIM/WuKongIMGoProto"
"github.com/panjf2000/ants/v2"
"go.uber.org/zap"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/keepalive"
)
type Webhook struct {
s *Server
wklog.Log
eventPool *ants.Pool
httpClient *http.Client
webhookGRPCPool *grpcpool.Pool // webhook grpc客户端
stoped chan struct{}
onlinestatusLock sync.RWMutex
onlinestatusList []string
}
func NewWebhook(s *Server) *Webhook {
options := ants.Options{ExpiryDuration: 10 * time.Second, Nonblocking: false}
eventPool, err := ants.NewPool(s.opts.EventPoolSize, ants.WithOptions(options), ants.WithPanicHandler(func(err interface{}) {
fmt.Println("事件池panic->", err)
}))
if err != nil {
panic(err)
}
var (
webhookGRPCPool *grpcpool.Pool
)
if s.opts.WebhookGRPCOn() {
webhookGRPCPool, err = grpcpool.New(func() (*grpc.ClientConn, error) {
return grpc.Dial(s.opts.Webhook.GRPCAddr, grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithKeepaliveParams(keepalive.ClientParameters{
Time: 5 * time.Minute, // send pings every 5 minute if there is no activity
Timeout: 2 * time.Second, // wait 1 second for ping ack before considering the connection dead
}))
}, 2, 20, time.Minute*5) // 初始化2个连接 最多20个连接
if err != nil {
panic(err)
}
}
return &Webhook{
s: s,
Log: wklog.NewWKLog("Webhook"),
eventPool: eventPool,
webhookGRPCPool: webhookGRPCPool,
onlinestatusList: make([]string, 0),
stoped: make(chan struct{}),
httpClient: &http.Client{
Transport: &http.Transport{
DialContext: (&net.Dialer{
Timeout: 5 * time.Second,
KeepAlive: 5 * time.Second,
}).DialContext,
ForceAttemptHTTP2: true,
MaxIdleConns: 200,
MaxIdleConnsPerHost: 200,
IdleConnTimeout: 300 * time.Second,
TLSHandshakeTimeout: time.Second * 5,
ResponseHeaderTimeout: 5 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
},
},
}
}
func (w *Webhook) Start() {
go w.notifyQueueLoop()
go w.loopOnlineStatus()
}
func (w *Webhook) Stop() {
close(w.stoped)
}
// TriggerEvent 触发事件
func (w *Webhook) TriggerEvent(event *Event) {
if !w.s.opts.WebhookOn() { // 没设置webhook直接忽略
return
}
err := w.eventPool.Submit(func() {
jsonData, err := json.Marshal(event.Data)
if err != nil {
w.Error("webhook的event数据不能json化!", zap.Error(err))
return
}
if w.s.opts.WebhookGRPCOn() {
err = w.sendWebhookForGRPC(event.Event, jsonData)
} else {
err = w.sendWebhookForHttp(event.Event, jsonData)
}
if err != nil {
w.Error("请求webhook失败!", zap.Error(err), zap.String("event", event.Event))
return
}
})
if err != nil {
w.Error("提交事件失败", zap.Error(err))
}
}
// 通知离线消息
func (w *Webhook) notifyOfflineMsg(msg *Message, large bool, subscribers []string) {
compress := ""
toUIDs := subscribers
var compresssToUIDs []byte
if w.s.opts.Channel.SubscriberCompressOfCount > 0 && len(subscribers) > w.s.opts.Channel.SubscriberCompressOfCount {
buff := new(bytes.Buffer)
gWriter := gzip.NewWriter(buff)
defer gWriter.Close()
_, err := gWriter.Write([]byte(wkutil.ToJSON(subscribers)))
if err != nil {
w.Error("压缩订阅者失败!", zap.Error(err))
} else {
toUIDs = make([]string, 0)
compress = "gzip"
compresssToUIDs = buff.Bytes()
}
}
// 推送离线到上层应用
w.TriggerEvent(&Event{
Event: EventMsgOffline,
Data: MessageOfflineNotify{
MessageResp: MessageResp{
Header: MessageHeader{
RedDot: wkutil.BoolToInt(msg.RedDot),
SyncOnce: wkutil.BoolToInt(msg.SyncOnce),
NoPersist: wkutil.BoolToInt(msg.NoPersist),
},
Setting: msg.Setting.Uint8(),
ClientMsgNo: msg.ClientMsgNo,
MessageID: msg.MessageID,
MessageIDStr: strconv.FormatInt(msg.MessageID, 10),
MessageSeq: msg.MessageSeq,
FromUID: msg.FromUID,
ChannelID: msg.ChannelID,
ChannelType: msg.ChannelType,
Topic: msg.Topic,
Expire: msg.Expire,
Timestamp: msg.Timestamp,
Payload: msg.Payload,
},
ToUIDs: toUIDs,
Compress: compress,
CompresssToUIDs: compresssToUIDs,
SourceID: int64(w.s.opts.ID),
},
})
}
// Online 用户在线
func (w *Webhook) Online(uid string, deviceFlag wkproto.DeviceFlag, id int64, onlineCount int, totalOnlineCount int) {
w.onlinestatusLock.Lock()
defer w.onlinestatusLock.Unlock()
online := 1
w.onlinestatusList = append(w.onlinestatusList, fmt.Sprintf("%s-%d-%d-%d-%d-%d", uid, deviceFlag, online, id, onlineCount, totalOnlineCount))
w.Debug("User online", zap.String("uid", uid), zap.String("deviceFlag", deviceFlag.String()), zap.Int64("id", id))
}
// Offline 用户离线
// id 为用户在当前系统中的socket id
// left 为剩余在线数量
func (w *Webhook) Offline(uid string, deviceFlag wkproto.DeviceFlag, id int64, onlineCount int, totalOnlineCount int) {
w.onlinestatusLock.Lock()
defer w.onlinestatusLock.Unlock()
online := 0
// 用户ID-用户设备标记-在线状态-socket ID-当前设备标记下的设备在线数量-当前用户下的所有设备在线数量
w.onlinestatusList = append(w.onlinestatusList, fmt.Sprintf("%s-%d-%d-%d-%d-%d", uid, deviceFlag, online, id, onlineCount, totalOnlineCount))
w.Debug("User offline", zap.String("uid", uid), zap.String("deviceFlag", deviceFlag.String()))
}
// 通知上层应用 TODO: 此初报错可以做一个邮件报警处理类的东西,
func (w *Webhook) notifyQueueLoop() {
errorSleepTime := time.Second * 1 // 发生错误后sleep时间
ticker := time.NewTicker(w.s.opts.Webhook.MsgNotifyEventPushInterval)
errMessageIDMap := make(map[int64]int) // 记录错误的消息ID value为错误次数
if w.s.opts.WebhookOn() {
for {
messages, err := w.s.store.GetMessagesOfNotifyQueue(w.s.opts.Webhook.MsgNotifyEventCountPerPush)
if err != nil {
w.Error("获取通知队列内的消息失败!", zap.Error(err))
time.Sleep(errorSleepTime) // 如果报错就休息下
continue
}
if len(messages) > 0 {
messageResps := make([]*MessageResp, 0, len(messages))
for _, msg := range messages {
resp := &MessageResp{}
resp.from(msg.(*Message), nil)
messageResps = append(messageResps, resp)
}
messageData, err := json.Marshal(messageResps)
if err != nil {
w.Error("第三方消息通知的event数据不能json化!", zap.Error(err))
time.Sleep(errorSleepTime) // 如果报错就休息下
continue
}
if w.s.opts.WebhookGRPCOn() {
err = w.sendWebhookForGRPC(EventMsgNotify, messageData)
} else {
err = w.sendWebhookForHttp(EventMsgNotify, messageData)
}
if err != nil {
w.Error("请求所有消息通知webhook失败!", zap.Error(err))
errMessageIDs := make([]int64, 0, len(messages))
for _, message := range messages {
errCount := errMessageIDMap[message.GetMessageID()]
errCount++
errMessageIDMap[message.GetMessageID()] = errCount
if errCount >= w.s.opts.Webhook.MsgNotifyEventRetryMaxCount {
errMessageIDs = append(errMessageIDs, message.GetMessageID())
}
}
if len(errMessageIDs) > 0 {
w.Error("消息通知失败超过最大次数!", zap.Int64s("messageIDs", errMessageIDs))
err = w.s.store.RemoveMessagesOfNotifyQueue(errMessageIDs)
if err != nil {
w.Warn("从通知队列里移除消息失败!", zap.Error(err), zap.Int64s("messageIDs", errMessageIDs))
}
for _, errMessageID := range errMessageIDs {
delete(errMessageIDMap, errMessageID)
}
}
time.Sleep(errorSleepTime) // 如果报错就休息下
continue
}
messageIDs := make([]int64, 0, len(messages))
for _, message := range messages {
messageID := message.(*Message).MessageID
messageIDs = append(messageIDs, messageID)
delete(errMessageIDMap, messageID)
}
err = w.s.store.RemoveMessagesOfNotifyQueue(messageIDs)
if err != nil {
w.Warn("从通知队列里移除消息失败!", zap.Error(err), zap.Int64s("messageIDs", messageIDs), zap.String("Webhook", w.s.opts.Webhook.HTTPAddr))
time.Sleep(errorSleepTime) // 如果报错就休息下
continue
}
}
select {
case <-ticker.C:
case <-w.stoped:
return
}
}
}
}
func (w *Webhook) sendWebhookForHttp(event string, data []byte) error {
eventURL := fmt.Sprintf("%s?event=%s", w.s.opts.Webhook.HTTPAddr, event)
startTime := time.Now().UnixNano() / 1000 / 1000
w.Debug("webhook开始请求", zap.String("eventURL", eventURL))
resp, err := w.httpClient.Post(eventURL, "application/json", bytes.NewBuffer(data))
w.Debug("webhook请求结束 耗时", zap.Int64("mill", time.Now().UnixNano()/1000/1000-startTime))
if err != nil {
w.Warn("调用第三方消息通知失败!", zap.String("Webhook", w.s.opts.Webhook.HTTPAddr), zap.Error(err))
return err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
w.Warn("第三方消息通知接口返回状态错误!", zap.Int("status", resp.StatusCode), zap.String("Webhook", w.s.opts.Webhook.HTTPAddr))
return errors.New("第三方消息通知接口返回状态错误!")
}
return nil
}
func (w *Webhook) sendWebhookForGRPC(event string, data []byte) error {
startNow := time.Now()
startTime := startNow.UnixNano() / 1000 / 1000
w.Debug("webhook grpc 开始请求", zap.String("event", event))
ctx, cancel := context.WithTimeout(context.Background(), time.Second*2)
defer cancel()
clientConn, err := w.webhookGRPCPool.Get(ctx)
if err != nil {
return err
}
defer clientConn.Close()
// cliConn, err := grpc.Dial(l.opts.WebhookGRPC, grpc.WithInsecure())
// if err != nil {
// return err
// }
cli := wkhook.NewWebhookServiceClient(clientConn)
sendCtx, sendCancel := context.WithTimeout(context.Background(), time.Second*10)
defer sendCancel()
resp, err := cli.SendWebhook(sendCtx, &wkhook.EventReq{
Event: event,
Data: data,
})
w.Debug("webhook grpc 请求结束 耗时", zap.Int64("mill", time.Now().UnixNano()/1000/1000-startTime))
w.s.monitor.WebhookObserve(event, time.Since(startNow))
if err != nil {
return err
}
if resp.Status != wkhook.EventStatus_Success {
return errors.New("grpc返回状态错误!")
}
return nil
}
func (w *Webhook) loopOnlineStatus() {
if !w.s.opts.WebhookOn() {
return
}
opLen := 0 // 最后一次操作在线状态数组的长度
errCount := 0 // webhook请求失败重试次数
for {
if opLen == 0 {
w.onlinestatusLock.Lock()
opLen = len(w.onlinestatusList)
w.onlinestatusLock.Unlock()
}
if opLen == 0 {
time.Sleep(time.Second * 2) // 没有数据就休息2秒
continue
}
w.onlinestatusLock.Lock()
data := w.onlinestatusList[:opLen]
w.onlinestatusLock.Unlock()
jsonData, err := json.Marshal(data)
if err != nil {
w.Error("webhook的event数据不能json化!", zap.Error(err))
time.Sleep(time.Second * 1)
continue
}
if w.s.opts.WebhookGRPCOn() {
err = w.sendWebhookForGRPC(EventOnlineStatus, jsonData)
} else {
err = w.sendWebhookForHttp(EventOnlineStatus, jsonData)
}
if err != nil {
errCount++
w.Error("请求在线状态webhook失败!", zap.Error(err))
if errCount >= w.s.opts.Webhook.MsgNotifyEventRetryMaxCount {
w.Error("请求在线状态webhook失败通知超过最大次数!", zap.Int("MsgNotifyEventRetryMaxCount", w.s.opts.Webhook.MsgNotifyEventRetryMaxCount))
w.onlinestatusLock.Lock()
w.onlinestatusList = w.onlinestatusList[opLen:]
opLen = 0
w.onlinestatusLock.Unlock()
errCount = 0
}
time.Sleep(time.Second * 1) // 如果报错就休息下
continue
}
w.onlinestatusLock.Lock()
w.onlinestatusList = w.onlinestatusList[opLen:]
opLen = 0
w.onlinestatusLock.Unlock()
}
}
const (
// EventMsgOffline 离线消息
EventMsgOffline = "msg.offline"
// EventMsgNotify 消息通知(将所有消息通知到第三方程序)
EventMsgNotify = "msg.notify"
// EventOnlineStatus 用户在线状态
EventOnlineStatus = "user.onlinestatus"
)
// Event Event
type Event struct {
Event string `json:"event"` // 事件标示
Data interface{} `json:"data"` // 事件数据
}
func (e *Event) String() string {
return fmt.Sprintf("Event:%s Data:%v", e.Event, e.Data)
}