Optimisation for sonar

This commit is contained in:
Zoltán Papp 2024-07-09 16:15:25 +02:00
parent 7ef191903e
commit 57b85f4f8d

View File

@ -297,7 +297,7 @@ func (c *Client) readLoop(relayConn net.Conn) {
c.log.Debugf("failed to read message from relay server: %s", errExit)
}
c.mu.Unlock()
goto Exit
break
}
msgType, err := messages.DetermineServerMsgType(buf[:n])
@ -306,6 +306,18 @@ func (c *Client) readLoop(relayConn net.Conn) {
continue
}
if !c.handleMsg(msgType, buf[:n], bufPtr, hc, internallyStoppedFlag) {
break
}
}
hc.Stop()
c.notifyDisconnected()
c.wgReadLoop.Done()
_ = c.close(false)
}
func (c *Client) handleMsg(msgType messages.MsgType, buf []byte, bufPtr *[]byte, hc *healthcheck.Receiver, internallyStoppedFlag *internalStopFlag) (continueLoop bool) {
switch msgType {
case messages.MsgTypeHealthCheck:
msg := messages.MarshalHealthcheck()
@ -316,43 +328,50 @@ func (c *Client) readLoop(relayConn net.Conn) {
}
}
hc.Heartbeat()
c.bufPool.Put(bufPtr)
case messages.MsgTypeTransport:
peerID, payload, err := messages.UnmarshalTransportMsg(buf[:n])
return c.handleTrasnportMsg(buf, bufPtr, internallyStoppedFlag)
case messages.MsgTypeClose:
log.Debugf("relay connection close by server")
c.bufPool.Put(bufPtr)
return false
}
return true
}
func (c *Client) handleTrasnportMsg(buf []byte, bufPtr *[]byte, internallyStoppedFlag *internalStopFlag) bool {
peerID, payload, err := messages.UnmarshalTransportMsg(buf)
if err != nil {
if c.serviceIsRunning && !internallyStoppedFlag.isSet() {
c.log.Errorf("failed to parse transport message: %v", err)
}
continue
c.bufPool.Put(bufPtr)
return true
}
stringID := messages.HashIDToString(peerID)
c.mu.Lock()
if !c.serviceIsRunning {
c.mu.Unlock()
goto Exit
c.bufPool.Put(bufPtr)
return false
}
container, ok := c.conns[stringID]
c.mu.Unlock()
if !ok {
c.log.Errorf("peer not found: %s", stringID)
continue
c.bufPool.Put(bufPtr)
return true
}
container.writeMsg(Msg{
msg := Msg{
bufPool: c.bufPool,
bufPtr: bufPtr,
Payload: payload})
case messages.MsgTypeClose:
log.Debugf("relay connection close by server")
goto Exit
Payload: payload,
}
}
Exit:
hc.Stop()
c.notifyDisconnected()
c.wgReadLoop.Done()
_ = c.close(false)
container.writeMsg(msg)
return true
}
// todo check by reference too, the id is not enought because the id come from the outer conn