From d9d275a7ce128bd3d69099cd7a89d65f6d83d603 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zolt=C3=A1n=20Papp?= Date: Tue, 9 Jul 2024 16:27:20 +0200 Subject: [PATCH] Optimisation for sonar --- relay/client/client.go | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/relay/client/client.go b/relay/client/client.go index 35c338e34..1897e521b 100644 --- a/relay/client/client.go +++ b/relay/client/client.go @@ -320,15 +320,7 @@ func (c *Client) readLoop(relayConn net.Conn) { 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() - _, wErr := c.relayConn.Write(msg) - if wErr != nil { - if c.serviceIsRunning && !internallyStoppedFlag.isSet() { - c.log.Errorf("failed to send heartbeat: %s", wErr) - } - } - hc.Heartbeat() - c.bufPool.Put(bufPtr) + c.handleHealthCheck(buf, bufPtr, hc, internallyStoppedFlag) case messages.MsgTypeTransport: return c.handleTrasnportMsg(buf, bufPtr, internallyStoppedFlag) case messages.MsgTypeClose: @@ -340,6 +332,19 @@ func (c *Client) handleMsg(msgType messages.MsgType, buf []byte, bufPtr *[]byte, return true } +func (c *Client) handleHealthCheck(buf []byte, bufPtr *[]byte, hc *healthcheck.Receiver, internallyStoppedFlag *internalStopFlag) { + c.handleHealthCheck(buf, bufPtr, hc, internallyStoppedFlag) + msg := messages.MarshalHealthcheck() + _, wErr := c.relayConn.Write(msg) + if wErr != nil { + if c.serviceIsRunning && !internallyStoppedFlag.isSet() { + c.log.Errorf("failed to send heartbeat: %s", wErr) + } + } + hc.Heartbeat() + c.bufPool.Put(bufPtr) +} + func (c *Client) handleTrasnportMsg(buf []byte, bufPtr *[]byte, internallyStoppedFlag *internalStopFlag) bool { peerID, payload, err := messages.UnmarshalTransportMsg(buf) if err != nil {