Use a new buffer to avoid race conditions #112
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Here we do 2 things:
Actually, this trick is according to people able to improve the client performances by reusing persistent connections, but it's not documented in the official golang documentation. Moreover, we had those unwanted logs by doing this. So my point here is we should not use this unsafe trick in such an important part of the trace and rather consider using a grpc library like this one that will natively use HTTP2.
I also considered using a pool of buffers to avoid reallocating memory, but it was still leading to race conditions. Indeed, once the function
client.Do(req)
has been called, thepersitentConn.writeLoop
goroutine will be spawned with a reference on the buffer, and will try to read it later, even when we will put the buffer back to the pool. That's why to be safe, it's preferable to create a new buffer each time we callclient.Do
(again, we don't really create a new buffer, since we reuse the underlying slice of bytes).