-
Notifications
You must be signed in to change notification settings - Fork 265
/
api_connz.go
175 lines (146 loc) · 4.29 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
package server
import (
"fmt"
"net"
"net/http"
"sort"
"strconv"
"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)
offset := int(offset64)
limit := int(limit64)
if limit <= 0 {
limit = 20
}
sortOpt := ByID
if sortStr != "" {
sortOpt = SortOpt(sortOpt)
}
switch sortOpt {
case ByID:
sort.Sort(byID{Conns: conns})
}
minoff := offset
maxoff := offset + limit
maxIndex := len(conns)
if minoff > maxIndex {
minoff = maxIndex
}
if maxoff > maxIndex {
maxoff = maxIndex
}
resultConns := conns[minoff:maxoff]
connInfos := make([]*ConnInfo, 0, len(resultConns))
for _, resultConn := range resultConns {
if !resultConn.IsAuthed() {
continue
}
connInfos = append(connInfos, newConnInfo(resultConn))
}
c.JSON(http.StatusOK, Connz{
Connections: connInfos,
Now: time.Now(),
Total: len(conns),
Offset: offset,
})
}
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
}
connCtx := c.Context().(*connContext)
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: connCtx.inMsgs.Load(),
OutMsgs: connCtx.outMsgs.Load(),
InBytes: connCtx.inBytes.Load(),
OutBytes: connCtx.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排序
)
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] }