Skip to content

Commit

Permalink
Wrap heavy logging into log level checks.
Browse files Browse the repository at this point in the history
Fix data lock and data access order.
  • Loading branch information
alexeykiselev committed Dec 14, 2024
1 parent c08bace commit abced7f
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions pkg/networking/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,9 @@ func (s *Session) waitForSend(data []byte) error {
timer.Reset(s.config.connectionWriteTimeout)
defer s.tp.Put(timer)

s.logger.Debug("Sending data", "data", base64.StdEncoding.EncodeToString(data))
if s.logger.Enabled(s.ctx, slog.LevelDebug) {
s.logger.Debug("Sending data", "data", base64.StdEncoding.EncodeToString(data))
}
ready := &sendPacket{data: data, err: errCh}
select {
case s.sendCh <- ready:
Expand Down Expand Up @@ -210,9 +212,11 @@ func (s *Session) sendLoop() error {
return s.ctx.Err()

case packet := <-s.sendCh:
s.logger.Debug("Sending data to connection",
"data", base64.StdEncoding.EncodeToString(packet.data))
packet.mu.Lock()
if s.logger.Enabled(s.ctx, slog.LevelDebug) {
s.logger.Debug("Sending data to connection",
"data", base64.StdEncoding.EncodeToString(packet.data))
}
if len(packet.data) != 0 {
// Copy the data into the buffer to avoid holding a mutex lock during the writing.
_, err := dataBuf.Write(packet.data)
Expand Down Expand Up @@ -352,7 +356,10 @@ func (s *Session) readMessagePayload(hdr Header, conn io.Reader) error {
// We lock the buffer from modification on the time of invocation of OnReceive handler.
// The slice of bytes passed into the handler is only valid for the duration of the handler invocation.
// So inside the handler better deserialize message or make a copy of the bytes.
s.logger.Debug("Invoking OnReceive handler", "message", base64.StdEncoding.EncodeToString(s.receiveBuffer.Bytes()))
if s.logger.Enabled(s.ctx, slog.LevelDebug) {
s.logger.Debug("Invoking OnReceive handler", "message",
base64.StdEncoding.EncodeToString(s.receiveBuffer.Bytes()))
}
s.config.handler.OnReceive(s, s.receiveBuffer.Bytes()) // Invoke OnReceive handler.
s.receiveBuffer.Reset() // Reset the buffer for the next message.
return nil
Expand Down

0 comments on commit abced7f

Please sign in to comment.