移除调试日志
This commit is contained in:
parent
a8bb1d01b4
commit
679728c6bc
46
ws/wsConn.go
46
ws/wsConn.go
@ -55,7 +55,7 @@ func (c *wsConnect) SendMsg(data []byte) error {
|
|||||||
// 关闭链接
|
// 关闭链接
|
||||||
func (c *wsConnect) Close() {
|
func (c *wsConnect) Close() {
|
||||||
c.once.Do(func() {
|
c.once.Do(func() {
|
||||||
log.Debug(c.Log("关闭链接"))
|
//log.Debug(c.Log("关闭链接"))
|
||||||
_ = c.wsConn.WriteMessage(websocket.CloseMessage, []byte{})
|
_ = c.wsConn.WriteMessage(websocket.CloseMessage, []byte{})
|
||||||
c.inChan.Close()
|
c.inChan.Close()
|
||||||
c.outChan.Close()
|
c.outChan.Close()
|
||||||
@ -69,10 +69,15 @@ func (c *wsConnect) Close() {
|
|||||||
|
|
||||||
// 循环从websocket中读取消息放入到读队列中
|
// 循环从websocket中读取消息放入到读队列中
|
||||||
func (c *wsConnect) readWsLoop() {
|
func (c *wsConnect) readWsLoop() {
|
||||||
|
defer func() {
|
||||||
|
//log.Debug(c.Log("readWsLoop协程退出"))
|
||||||
|
c.Close()
|
||||||
|
}()
|
||||||
|
|
||||||
c.wsConn.SetReadLimit(maxMessageSize)
|
c.wsConn.SetReadLimit(maxMessageSize)
|
||||||
_ = c.wsConn.SetReadDeadline(time.Now().Add(pongWait))
|
_ = c.wsConn.SetReadDeadline(time.Now().Add(pongWait))
|
||||||
c.wsConn.SetPongHandler(func(string) error {
|
c.wsConn.SetPongHandler(func(string) error {
|
||||||
log.Debug(c.Log("received pong. from client"))
|
//log.Debug(c.Log("received pong. from client"))
|
||||||
_ = c.wsConn.SetReadDeadline(time.Now().Add(pongWait))
|
_ = c.wsConn.SetReadDeadline(time.Now().Add(pongWait))
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
@ -84,8 +89,7 @@ func (c *wsConnect) readWsLoop() {
|
|||||||
if websocket.IsUnexpectedCloseError(err, websocket.CloseGoingAway, websocket.CloseAbnormalClosure, websocket.CloseNormalClosure) {
|
if websocket.IsUnexpectedCloseError(err, websocket.CloseGoingAway, websocket.CloseAbnormalClosure, websocket.CloseNormalClosure) {
|
||||||
log.Error(c.Log("消息读取出现错误:%v", err))
|
log.Error(c.Log("消息读取出现错误:%v", err))
|
||||||
}
|
}
|
||||||
log.Debug(c.Log("关闭连接:%v", err))
|
//log.Debug(c.Log("关闭连接:%v", err))
|
||||||
c.Close()
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
switch msgType {
|
switch msgType {
|
||||||
@ -105,53 +109,63 @@ func (c *wsConnect) readWsLoop() {
|
|||||||
// 发送响应关闭帧(必须回传相同状态码)
|
// 发送响应关闭帧(必须回传相同状态码)
|
||||||
rspMsg := websocket.FormatCloseMessage(code, reason)
|
rspMsg := websocket.FormatCloseMessage(code, reason)
|
||||||
_ = c.wsConn.WriteControl(websocket.CloseMessage, rspMsg, time.Now().Add(5*time.Second))
|
_ = c.wsConn.WriteControl(websocket.CloseMessage, rspMsg, time.Now().Add(5*time.Second))
|
||||||
c.Close()
|
return
|
||||||
default:
|
default:
|
||||||
if msgType != wsMsgType {
|
if msgType != wsMsgType {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
msg := &wsMessage{messageType: msgType, data: data}
|
msg := &wsMessage{messageType: msgType, data: data}
|
||||||
_ = c.inChan.Write(msg)
|
if err = c.inChan.Write(msg); err != nil {
|
||||||
|
log.Error(c.Log("读队列关闭:%v", err))
|
||||||
|
return
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *wsConnect) writeWsLoop() {
|
func (c *wsConnect) writeWsLoop() {
|
||||||
ticker := time.NewTicker(pingPeriod)
|
ticker := time.NewTicker(pingPeriod)
|
||||||
defer ticker.Stop()
|
defer func() {
|
||||||
|
//log.Debug(c.Log("writeWsLoop协程退出"))
|
||||||
|
ticker.Stop()
|
||||||
|
// 关闭连接
|
||||||
|
c.Close()
|
||||||
|
}()
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
// 取一个消息发送给客户端
|
// 取一个消息发送给客户端
|
||||||
case msg := <-c.outChan.Reader():
|
case msg, ok := <-c.outChan.Reader():
|
||||||
|
if !ok {
|
||||||
|
return
|
||||||
|
}
|
||||||
if err := c.wsConn.WriteMessage(msg.messageType, msg.data); err != nil {
|
if err := c.wsConn.WriteMessage(msg.messageType, msg.data); err != nil {
|
||||||
iMsg := &ipb.InternalMsg{}
|
iMsg := &ipb.InternalMsg{}
|
||||||
_ = proto.Unmarshal(msg.data, iMsg)
|
_ = proto.Unmarshal(msg.data, iMsg)
|
||||||
log.Error(c.Log("发送消息错误:%v 消息长度:%v 消息内容:%v", err, len(msg.data), iMsg))
|
log.Error(c.Log("发送消息错误:%v 消息长度:%v 消息内容:%v", err, len(msg.data), iMsg))
|
||||||
// 关闭连接
|
|
||||||
c.Close()
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
case <-ticker.C:
|
case <-ticker.C:
|
||||||
if err := c.wsConn.WriteMessage(websocket.PingMessage, []byte("ping")); err != nil {
|
if err := c.wsConn.WriteMessage(websocket.PingMessage, []byte("ping")); err != nil {
|
||||||
log.Error(c.Log("发送心跳失败:%v", err))
|
log.Error(c.Log("发送心跳失败:%v", err))
|
||||||
c.Close()
|
|
||||||
return
|
return
|
||||||
} else {
|
} else {
|
||||||
log.Debug(c.Log("发送心跳"))
|
//log.Debug(c.Log("发送心跳"))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *wsConnect) handle(process func(IConn, []byte)) {
|
func (c *wsConnect) handle(process func(IConn, []byte)) {
|
||||||
|
defer c.Close()
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case msg, ok := <-c.inChan.Reader():
|
case msg, ok := <-c.inChan.Reader():
|
||||||
if ok {
|
if !ok {
|
||||||
process(c, msg.data)
|
//log.Debug(c.Log("handle协程退出"))
|
||||||
} else {
|
return
|
||||||
c.Close()
|
|
||||||
}
|
}
|
||||||
|
process(c, msg.data)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -11,7 +11,7 @@ import (
|
|||||||
|
|
||||||
const (
|
const (
|
||||||
// 允许等待的写入时间
|
// 允许等待的写入时间
|
||||||
writeWait = 10 * time.Second
|
//writeWait = 10 * time.Second
|
||||||
// pong间隔时间
|
// pong间隔时间
|
||||||
pongWait = 60 * time.Second
|
pongWait = 60 * time.Second
|
||||||
// ping间隔时间
|
// ping间隔时间
|
||||||
@ -64,7 +64,7 @@ func (s *WsServer) wsHandle(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *WsServer) debugGoroutineNum() {
|
func (s *WsServer) debugGoroutineNum() {
|
||||||
ch := time.Tick(5 * time.Minute)
|
ch := time.Tick(2 * time.Minute)
|
||||||
go func() {
|
go func() {
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user