From cb792005981240cd4a857789a284e374340e60ac Mon Sep 17 00:00:00 2001 From: Mostafa Moradian Date: Sat, 20 May 2023 21:12:35 +0200 Subject: [PATCH 1/4] Simplify the code --- network/client.go | 15 +++------------ 1 file changed, 3 insertions(+), 12 deletions(-) diff --git a/network/client.go b/network/client.go index 82dfcc1a..c3fd5f00 100644 --- a/network/client.go +++ b/network/client.go @@ -184,23 +184,14 @@ func (c *Client) Receive() (int, []byte, *gerr.GatewayDError) { for { chunk := make([]byte, c.ReceiveChunkSize) read, err := c.Conn.Read(chunk) - switch { - case read > 0 && err != nil: - received += read - buffer.Write(chunk[:read]) + if err != nil { c.logger.Error().Err(err).Msg("Couldn't receive data from the server") - metrics.BytesReceivedFromServer.Observe(float64(received)) span.RecordError(err) - return received, buffer.Bytes(), gerr.ErrClientReceiveFailed.Wrap(err) - case err != nil: - c.logger.Error().Err(err).Msg("Couldn't receive data from the server") metrics.BytesReceivedFromServer.Observe(float64(received)) - span.RecordError(err) return received, buffer.Bytes(), gerr.ErrClientReceiveFailed.Wrap(err) - default: - received += read - buffer.Write(chunk[:read]) } + received += read + buffer.Write(chunk[:read]) if read == 0 || read < c.ReceiveChunkSize { break From 9951078df4f23305ba7218d935821e900fef6121 Mon Sep 17 00:00:00 2001 From: Mostafa Moradian Date: Sat, 20 May 2023 23:06:08 +0200 Subject: [PATCH 2/4] Add a new target for running with Go race detector --- Makefile | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Makefile b/Makefile index 9993e094..bf79e6b3 100644 --- a/Makefile +++ b/Makefile @@ -52,6 +52,9 @@ build-release: tidy run: tidy @go run -tags embed_swagger main.go run --dev +run-race: tidy + @go run -race -tags embed_swagger main.go run --dev + run-tracing: tidy @go run -tags embed_swagger main.go run --tracing --dev From d70dbe1a3ace4f24e7a3e55a65a562d929cbb8a8 Mon Sep 17 00:00:00 2001 From: Mostafa Moradian Date: Sat, 20 May 2023 23:23:34 +0200 Subject: [PATCH 3/4] Use the magic select to sync the blocking call --- network/client.go | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/network/client.go b/network/client.go index c3fd5f00..194855b9 100644 --- a/network/client.go +++ b/network/client.go @@ -181,20 +181,23 @@ func (c *Client) Receive() (int, []byte, *gerr.GatewayDError) { var received int buffer := bytes.NewBuffer(nil) // Read the data in chunks. - for { - chunk := make([]byte, c.ReceiveChunkSize) - read, err := c.Conn.Read(chunk) - if err != nil { - c.logger.Error().Err(err).Msg("Couldn't receive data from the server") - span.RecordError(err) - metrics.BytesReceivedFromServer.Observe(float64(received)) - return received, buffer.Bytes(), gerr.ErrClientReceiveFailed.Wrap(err) - } - received += read - buffer.Write(chunk[:read]) + select { + case <-time.After(time.Millisecond): + for { + chunk := make([]byte, c.ReceiveChunkSize) + read, err := c.Conn.Read(chunk) + if err != nil { + c.logger.Error().Err(err).Msg("Couldn't receive data from the server") + span.RecordError(err) + metrics.BytesReceivedFromServer.Observe(float64(received)) + return received, buffer.Bytes(), gerr.ErrClientReceiveFailed.Wrap(err) + } + received += read + buffer.Write(chunk[:read]) - if read == 0 || read < c.ReceiveChunkSize { - break + if read == 0 || read < c.ReceiveChunkSize { + break + } } } metrics.BytesReceivedFromServer.Observe(float64(received)) From 93364c555d1a071d891f65daa560b96b33058a27 Mon Sep 17 00:00:00 2001 From: Mostafa Moradian Date: Sat, 20 May 2023 23:30:04 +0200 Subject: [PATCH 4/4] Ignore linter --- network/client.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/network/client.go b/network/client.go index 194855b9..d6b6f403 100644 --- a/network/client.go +++ b/network/client.go @@ -181,7 +181,7 @@ func (c *Client) Receive() (int, []byte, *gerr.GatewayDError) { var received int buffer := bytes.NewBuffer(nil) // Read the data in chunks. - select { + select { //nolint:gosimple case <-time.After(time.Millisecond): for { chunk := make([]byte, c.ReceiveChunkSize)