This commit is contained in:
esrrhs 2018-12-22 17:49:45 +08:00
parent 9f8e88b863
commit aa606c2ef1
2 changed files with 19 additions and 8 deletions

View File

@ -64,6 +64,8 @@ type Client struct {
recvPacket uint64 recvPacket uint64
sendPacketSize uint64 sendPacketSize uint64
recvPacketSize uint64 recvPacketSize uint64
sendHBPacket uint64
} }
type ClientConn struct { type ClientConn struct {
@ -260,12 +262,13 @@ func (p *Client) ping() {
} }
func (p *Client) showNet() { func (p *Client) showNet() {
fmt.Printf("send %dPacket/s %dKB/s recv %dPacket/s %dKB/s\n", fmt.Printf("send %dPacket/s %dKB/s recv %dPacket/s %dKB/s HB %d/s\n",
p.sendPacket, p.sendPacketSize/1024, p.recvPacket, p.recvPacketSize/1024) p.sendPacket, p.sendPacketSize/1024, p.recvPacket, p.recvPacketSize/1024,p.sendHBPacket)
p.sendPacket = 0 p.sendPacket = 0
p.recvPacket = 0 p.recvPacket = 0
p.sendPacketSize = 0 p.sendPacketSize = 0
p.recvPacketSize = 0 p.recvPacketSize = 0
p.sendHBPacket = 0
} }
func (p *Client) heartbeat() { func (p *Client) heartbeat() {
@ -282,6 +285,7 @@ func (p *Client) heartbeat() {
conn.sendHBPacket++ conn.sendHBPacket++
p.sendHBPacket++
} }
} }
} }
@ -292,16 +296,20 @@ func (p *Client) calcHB() {
if p.hb > 0 { if p.hb > 0 {
for _, conn := range p.localIdToConnMap { for _, conn := range p.localIdToConnMap {
conn.avgRecvPacket = (conn.recvPacket + conn.avgRecvPacket*conn.avgRecvNum) / conn.avgRecvNum if conn.avgRecvNum > 0 {
conn.avgRecvPacket = (conn.recvPacket + conn.avgRecvPacket*conn.avgRecvNum) / conn.avgRecvNum
} else {
conn.avgRecvPacket = conn.recvPacket
}
conn.avgRecvNum++ conn.avgRecvNum++
if conn.avgRecvNum > 10 { if conn.avgRecvNum > 10 {
conn.avgRecvNum = 0 conn.avgRecvNum = 0
} }
conn.recvPacket = 0 conn.recvPacket = 0
conn.hbPacket = conn.avgRecvPacket * 2 / 3 conn.hbPacket = conn.avgRecvPacket + 10
if conn.hbPacket > 0 { if conn.avgRecvPacket > 0 {
fmt.Printf("calcHB %s %s %d %d\n", conn.id, conn.ipaddr.String(), conn.hbPacket, conn.sendHBPacket) fmt.Printf("calcHB %s %s %d %d\n", conn.id, conn.ipaddr.String(), conn.hbPacket, conn.sendHBPacket)
} }

View File

@ -24,6 +24,8 @@ type Server struct {
recvPacket uint64 recvPacket uint64
sendPacketSize uint64 sendPacketSize uint64
recvPacketSize uint64 recvPacketSize uint64
recvHBPacket uint64
} }
type ServerConn struct { type ServerConn struct {
@ -105,8 +107,7 @@ func (p *Server) processPacket(packet *Packet) {
udpConn.echoSeq = packet.echoSeq udpConn.echoSeq = packet.echoSeq
if packet.msgType == HB { if packet.msgType == HB {
udpConn.echoId = packet.echoId p.recvHBPacket++
udpConn.echoSeq = packet.echoSeq
return return
} }
@ -179,9 +180,11 @@ func (p *Server) checkTimeoutConn() {
} }
func (p *Server) showNet() { func (p *Server) showNet() {
fmt.Printf("send %dPacket/s %dKB/s recv %dPacket/s %dKB/s\n", p.sendPacket, p.sendPacketSize/1024, p.recvPacket, p.recvPacketSize/1024) fmt.Printf("send %dPacket/s %dKB/s recv %dPacket/s %dKB/s HB %d/s\n",
p.sendPacket, p.sendPacketSize/1024, p.recvPacket, p.recvPacketSize/1024, p.recvHBPacket)
p.sendPacket = 0 p.sendPacket = 0
p.recvPacket = 0 p.recvPacket = 0
p.sendPacketSize = 0 p.sendPacketSize = 0
p.recvPacketSize = 0 p.recvPacketSize = 0
p.recvHBPacket = 0
} }