Skip to content

Commit

Permalink
Fix counters when reading or writing data to the socket
Browse files Browse the repository at this point in the history
  • Loading branch information
mostafa committed Oct 12, 2024
1 parent 3090c58 commit 37fec61
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
11 changes: 7 additions & 4 deletions network/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,9 +187,10 @@ func (c *Client) Send(data []byte) (int, *gerr.GatewayDError) {
}

sent := 0
received := len(data)
dataSize := len(data)
for {
if sent >= received {
// If we've sent all the data, we must break the loop.
if sent >= dataSize {
break
}

Expand Down Expand Up @@ -240,13 +241,15 @@ func (c *Client) Receive() (int, []byte, *gerr.GatewayDError) {
for ctx.Err() == nil {
chunk := make([]byte, c.ReceiveChunkSize)
read, err := c.conn.Read(chunk)
if read > 0 {
total += read
buffer.Write(chunk[:read])
}
if err != nil {
c.logger.Error().Err(err).Msg("Couldn't receive data from the server")
span.RecordError(err)
return total, buffer.Bytes(), gerr.ErrClientReceiveFailed.Wrap(err)
}
total += read
buffer.Write(chunk[:read])

if read < c.ReceiveChunkSize {
break
Expand Down
9 changes: 5 additions & 4 deletions network/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -727,19 +727,20 @@ func (pr *Proxy) receiveTrafficFromClient(conn net.Conn) ([]byte, *gerr.GatewayD
for {
chunk := make([]byte, pr.ClientConfig.ReceiveChunkSize)
read, err := conn.Read(chunk)
if read > 0 {
total += read
buffer.Write(chunk[:read])
}
if read == 0 || err != nil {
pr.Logger.Debug().Err(err).Msg("Error reading from client")
span.RecordError(err)

metrics.BytesReceivedFromClient.WithLabelValues(pr.GetGroupName(), pr.GetBlockName()).Observe(float64(read))
metrics.TotalTrafficBytes.WithLabelValues(pr.GetGroupName(), pr.GetBlockName()).Observe(float64(read))

return chunk[:read], gerr.ErrReadFailed.Wrap(err)
return buffer.Bytes(), gerr.ErrReadFailed.Wrap(err)
}

total += read
buffer.Write(chunk[:read])

if read < pr.ClientConfig.ReceiveChunkSize {
break
}
Expand Down

0 comments on commit 37fec61

Please sign in to comment.