Skip to content

Commit

Permalink
Merge pull request #245 from gatewayd-io/fix-client-connection-issue
Browse files Browse the repository at this point in the history
Fix receive issue of the client connection
  • Loading branch information
mostafa authored May 20, 2023
2 parents d88792c + 93364c5 commit b8491db
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 20 deletions.
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

0 comments on commit b8491db

Please sign in to comment.