-
Notifications
You must be signed in to change notification settings - Fork 264
/
api_connz.go
358 lines (295 loc) · 10.7 KB
/
api_connz.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
package server
import (
"fmt"
"net"
"net/http"
"sort"
"strconv"
"strings"
"time"
"github.com/WuKongIM/WuKongIM/pkg/wkhttp"
"github.com/WuKongIM/WuKongIM/pkg/wklog"
"github.com/WuKongIM/WuKongIM/pkg/wknet"
"github.com/WuKongIM/WuKongIM/pkg/wkproto"
)
type ConnzAPI struct {
wklog.Log
s *Server
}
func NewConnzAPI(s *Server) *ConnzAPI {
return &ConnzAPI{
Log: wklog.NewWKLog("ConnzAPI"),
s: s,
}
}
func (co *ConnzAPI) Route(r *wkhttp.WKHttp) {
r.GET("/connz", co.HandleConnz)
}
func (co *ConnzAPI) HandleConnz(c *wkhttp.Context) {
conns := co.s.dispatch.engine.GetAllConn()
sortStr := c.Query("sort")
offset64, _ := strconv.ParseInt(c.Query("offset"), 10, 64)
limit64, _ := strconv.ParseInt(c.Query("limit"), 10, 64)
uid := c.Query("uid")
offset := int(offset64)
limit := int(limit64)
if limit <= 0 {
limit = 20
}
sortOpt := ByID
if sortStr != "" {
sortOpt = SortOpt(sortStr)
}
var connInfos []*ConnInfo
if strings.TrimSpace(uid) != "" {
connInfos = make([]*ConnInfo, 0, 4)
if len(conns) > 0 {
for _, conn := range conns {
connInfos = append(connInfos, newConnInfo(conn))
}
}
} else {
resultConns := co.s.GetConnInfos(sortOpt, offset, limit)
connInfos = make([]*ConnInfo, 0, len(resultConns))
for _, resultConn := range resultConns {
if resultConn == nil || !resultConn.IsAuthed() {
continue
}
connInfos = append(connInfos, newConnInfo(resultConn))
}
}
c.JSON(http.StatusOK, Connz{
Connections: connInfos,
Now: time.Now(),
Total: len(conns),
Offset: offset,
Limit: limit,
})
}
func (s *Server) GetConnInfos(sortOpt SortOpt, offset, limit int) []wknet.Conn {
conns := s.dispatch.engine.GetAllConn()
switch sortOpt {
case ByID:
sort.Sort(byID{Conns: conns})
case ByIDDesc:
sort.Sort(byIDDesc{Conns: conns})
case ByInMsg:
sort.Sort(byInMsg{Conns: conns})
case ByInMsgDesc:
sort.Sort(byInMsgDesc{Conns: conns})
case ByOutMsg:
sort.Sort(byOutMsg{Conns: conns})
case ByOutMsgDesc:
sort.Sort(byOutMsgDesc{Conns: conns})
case ByInBytes:
sort.Sort(byInBytes{Conns: conns})
case ByInBytesDesc:
sort.Sort(byInBytesDesc{Conns: conns})
case ByOutBytes:
sort.Sort(byOutBytes{Conns: conns})
case ByOutBytesDesc:
sort.Sort(byOutBytesDesc{Conns: conns})
case ByPendingBytes:
sort.Sort(byPendingBytes{Conns: conns})
case ByPendingBytesDesc:
sort.Sort(byPendingBytesDesc{Conns: conns})
case ByUptime:
sort.Sort(byUptime{Conns: conns})
case ByUptimeDesc:
sort.Sort(byUptimeDesc{Conns: conns})
}
minoff := offset
maxoff := offset + limit
maxIndex := len(conns)
if minoff > maxIndex {
minoff = maxIndex
}
if maxoff > maxIndex {
maxoff = maxIndex
}
resultConns := conns[minoff:maxoff]
return resultConns
}
type Connz struct {
Connections []*ConnInfo `json:"connections"` // 连接数
Now time.Time `json:"now"` // 查询时间
Total int `json:"total"` // 总连接数量
Offset int `json:"offset"` // 偏移位置
Limit int `json:"limit"` // 限制数量
}
type ConnInfo struct {
ID int64 `json:"id"` // 连接ID
UID string `json:"uid"` // 用户uid
IP string `json:"ip"` // 客户端IP
Port int `json:"port"` // 客户端端口
LastActivity time.Time `json:"last_activity"` // 最后一次活动时间
Uptime string `json:"uptime"` // 启动时间
Idle string `json:"idle"` // 客户端闲置时间
PendingBytes int `json:"pending_bytes"` // 等待发送的字节数
InMsgs int64 `json:"in_msgs"` // 流入的消息数
OutMsgs int64 `json:"out_msgs"` // 流出的消息数量
InBytes int64 `json:"in_bytes"` // 流入的字节数量
OutBytes int64 `json:"out_bytes"` // 流出的字节数量
Device string `json:"device"` // 设备
DeviceID string `json:"device_id"` // 设备ID
Version uint8 `json:"version"` // 客户端协议版本
}
func newConnInfo(c wknet.Conn) *ConnInfo {
var (
now = time.Now()
host string
port int
)
if c.RemoteAddr() != nil {
hostStr, portStr, _ := net.SplitHostPort(c.RemoteAddr().String())
port, _ = strconv.Atoi(portStr)
host = hostStr
}
connStats := c.ConnStats()
return &ConnInfo{
ID: c.ID(),
UID: c.UID(),
IP: host,
Port: port,
LastActivity: c.LastActivity(),
Uptime: myUptime(now.Sub(c.Uptime())),
Idle: myUptime(now.Sub(c.LastActivity())),
PendingBytes: c.OutboundBuffer().BoundBufferSize(),
InMsgs: connStats.InMsgs.Load(),
OutMsgs: connStats.OutMsgs.Load(),
InBytes: connStats.InBytes.Load(),
OutBytes: connStats.OutBytes.Load(),
Device: device(c),
DeviceID: c.DeviceID(),
Version: uint8(c.ProtoVersion()),
}
}
func device(conn wknet.Conn) string {
d := "未知"
level := "主"
switch wkproto.DeviceFlag(conn.DeviceFlag()) {
case wkproto.APP:
d = "App"
case wkproto.PC:
d = "PC"
case wkproto.WEB:
d = "Web"
}
if wkproto.DeviceLevel(conn.DeviceLevel()) == wkproto.DeviceLevelSlave {
level = "从"
}
return fmt.Sprintf("%s(%s)", d, level)
}
type SortOpt string
const (
ByID SortOpt = "id" // 通过连接id排序
ByIDDesc SortOpt = "idDesc" // 通过连接id排序
ByInMsg SortOpt = "inMsg" // 通过收到消息排序
ByInMsgDesc SortOpt = "inMsgDesc" // 通过收到消息排序
ByOutMsg SortOpt = "outMsg" // 通过发送消息排序
ByOutMsgDesc SortOpt = "outMsgDesc" // 通过发送消息排序
ByInBytes SortOpt = "inBytes" // 通过收到字节数排序
ByInBytesDesc SortOpt = "inBytesDesc" // 通过收到字节数排序
ByOutBytes SortOpt = "outBytes" // 通过发送字节数排序
ByOutBytesDesc SortOpt = "outBytesDesc" // 通过发送字节数排序
ByPendingBytes SortOpt = "pendingBytes" // 通过等待发送字节数排序
ByPendingBytesDesc SortOpt = "pendingBytesDesc" // 通过等待发送字节数排序
ByUptime SortOpt = "uptime" // 通过启动时间排序
ByUptimeDesc SortOpt = "uptimeDesc" // 通过启动时间排序
)
// byID
type byID struct{ Conns []wknet.Conn }
func (l byID) Less(i, j int) bool {
return l.Conns[i].ID() < l.Conns[j].ID()
}
func (l byID) Len() int {
return len(l.Conns)
}
func (l byID) Swap(i, j int) { l.Conns[i], l.Conns[j] = l.Conns[j], l.Conns[i] }
// byIDDesc
type byIDDesc struct{ Conns []wknet.Conn }
func (l byIDDesc) Less(i, j int) bool {
return l.Conns[i].ID() > l.Conns[j].ID()
}
func (l byIDDesc) Len() int {
return len(l.Conns)
}
func (l byIDDesc) Swap(i, j int) { l.Conns[i], l.Conns[j] = l.Conns[j], l.Conns[i] }
// inMsg
type byInMsg struct{ Conns []wknet.Conn }
func (l byInMsg) Less(i, j int) bool {
return l.Conns[i].ConnStats().InMsgs.Load() < l.Conns[j].ConnStats().InMsgs.Load()
}
func (l byInMsg) Len() int { return len(l.Conns) }
func (l byInMsg) Swap(i, j int) { l.Conns[i], l.Conns[j] = l.Conns[j], l.Conns[i] }
type byInMsgDesc struct{ Conns []wknet.Conn }
func (l byInMsgDesc) Less(i, j int) bool {
return l.Conns[i].ConnStats().InMsgs.Load() > l.Conns[j].ConnStats().InMsgs.Load()
}
func (l byInMsgDesc) Len() int { return len(l.Conns) }
func (l byInMsgDesc) Swap(i, j int) { l.Conns[i], l.Conns[j] = l.Conns[j], l.Conns[i] }
// byOutMsg
type byOutMsg struct{ Conns []wknet.Conn }
func (l byOutMsg) Less(i, j int) bool {
return l.Conns[i].ConnStats().OutMsgs.Load() < l.Conns[j].ConnStats().OutMsgs.Load()
}
func (l byOutMsg) Len() int { return len(l.Conns) }
func (l byOutMsg) Swap(i, j int) { l.Conns[i], l.Conns[j] = l.Conns[j], l.Conns[i] }
type byOutMsgDesc struct{ Conns []wknet.Conn }
func (l byOutMsgDesc) Less(i, j int) bool {
return l.Conns[i].ConnStats().OutMsgs.Load() > l.Conns[j].ConnStats().OutMsgs.Load()
}
func (l byOutMsgDesc) Len() int { return len(l.Conns) }
func (l byOutMsgDesc) Swap(i, j int) { l.Conns[i], l.Conns[j] = l.Conns[j], l.Conns[i] }
// byInBytes
type byInBytes struct{ Conns []wknet.Conn }
func (l byInBytes) Less(i, j int) bool {
return l.Conns[i].ConnStats().InBytes.Load() < l.Conns[j].ConnStats().InBytes.Load()
}
func (l byInBytes) Len() int { return len(l.Conns) }
func (l byInBytes) Swap(i, j int) { l.Conns[i], l.Conns[j] = l.Conns[j], l.Conns[i] }
type byInBytesDesc struct{ Conns []wknet.Conn }
func (l byInBytesDesc) Less(i, j int) bool {
return l.Conns[i].ConnStats().InBytes.Load() > l.Conns[j].ConnStats().InBytes.Load()
}
func (l byInBytesDesc) Len() int { return len(l.Conns) }
func (l byInBytesDesc) Swap(i, j int) { l.Conns[i], l.Conns[j] = l.Conns[j], l.Conns[i] }
// outBytes
type byOutBytes struct{ Conns []wknet.Conn }
func (l byOutBytes) Less(i, j int) bool {
return l.Conns[i].ConnStats().OutBytes.Load() < l.Conns[j].ConnStats().OutBytes.Load()
}
func (l byOutBytes) Len() int { return len(l.Conns) }
func (l byOutBytes) Swap(i, j int) { l.Conns[i], l.Conns[j] = l.Conns[j], l.Conns[i] }
type byOutBytesDesc struct{ Conns []wknet.Conn }
func (l byOutBytesDesc) Less(i, j int) bool {
return l.Conns[i].ConnStats().OutBytes.Load() > l.Conns[j].ConnStats().OutBytes.Load()
}
func (l byOutBytesDesc) Len() int { return len(l.Conns) }
func (l byOutBytesDesc) Swap(i, j int) { l.Conns[i], l.Conns[j] = l.Conns[j], l.Conns[i] }
// PendingBytes
type byPendingBytes struct{ Conns []wknet.Conn }
func (l byPendingBytes) Less(i, j int) bool {
return l.Conns[i].OutboundBuffer().BoundBufferSize() < l.Conns[j].OutboundBuffer().BoundBufferSize()
}
func (l byPendingBytes) Len() int { return len(l.Conns) }
func (l byPendingBytes) Swap(i, j int) { l.Conns[i], l.Conns[j] = l.Conns[j], l.Conns[i] }
type byPendingBytesDesc struct{ Conns []wknet.Conn }
func (l byPendingBytesDesc) Less(i, j int) bool {
return l.Conns[i].OutboundBuffer().BoundBufferSize() > l.Conns[j].OutboundBuffer().BoundBufferSize()
}
func (l byPendingBytesDesc) Len() int { return len(l.Conns) }
func (l byPendingBytesDesc) Swap(i, j int) { l.Conns[i], l.Conns[j] = l.Conns[j], l.Conns[i] }
// uptime
type byUptime struct{ Conns []wknet.Conn }
func (l byUptime) Less(i, j int) bool {
return l.Conns[i].Uptime().Before(l.Conns[j].Uptime())
}
func (l byUptime) Len() int { return len(l.Conns) }
func (l byUptime) Swap(i, j int) { l.Conns[i], l.Conns[j] = l.Conns[j], l.Conns[i] }
type byUptimeDesc struct{ Conns []wknet.Conn }
func (l byUptimeDesc) Less(i, j int) bool {
return l.Conns[i].Uptime().After(l.Conns[j].Uptime())
}
func (l byUptimeDesc) Len() int { return len(l.Conns) }
func (l byUptimeDesc) Swap(i, j int) { l.Conns[i], l.Conns[j] = l.Conns[j], l.Conns[i] }