Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix receive issue of the client connection #245

Merged
merged 4 commits into from
May 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
34 changes: 14 additions & 20 deletions network/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,29 +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)
switch {
case read > 0 && err != nil:
received += read
buffer.Write(chunk[:read])
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:
select { //nolint:gosimple
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))
Expand Down