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 sending large batch of events over TLS #51

Merged
merged 1 commit into from
Jul 30, 2023

Conversation

smortex
Copy link
Member

@smortex smortex commented Jul 29, 2023

When writing data over an OpenSSL::SSL::SSLSocket, we have two buffers that can fill-in: the TCPSocket and the SSLSocket.

  • When the TCPSocket buffer is full, the TcpClient#write method wait for the socket to be writable again, and retry the operation;
  • When the SSLSocket buffer is full, the SSLClient#write method wait for the socket to be writable again, and retry the operation.

However, SSLClient#write is a wrapper around TcpClient#write, and when it retry after caching a OpenSSL::SSL::SSLErrorWaitWritable it has no idea of the amount of data that got send and restart a full transfer of the data with TcpClient#write. When this happen, the new transfer can fail in a similar fashion any number of time and will eventually come to completion after sending multiple partial copies of the message followed by a complete copy, which is just garbage for Riemann on the other side. Riemann will discard the message and return an error that will be passed to the calling code.

In order to fix this, make TcpClient#write aware of IO::WaitWritable (a base class of OpenSSL::SSL::SSLErrorWaitWritable) and remove the SSLClient#write method so that the parent class method is used directly instead.

While here, do the same for TcpClient#read / SSLClient#read for consistency.

While here, also handle IO::WaitReadable exception in TcpClient#write to cope with TLS renegociation as recommended in the IO#select documentation.

When writing data over an `OpenSSL::SSL::SSLSocket`, we have two buffers
that can fill-in: the `TCPSocket` and the `SSLSocket`.

  * When the `TCPSocket` buffer is full, the `TcpClient#write` method
    wait for the socket to be writable again, and `retry` the operation;
  * When the `SSLSocket` buffer is full, the `SSLClient#write` method
    wait for the socket to be writable again, and `retry` the operation.

However, `SSLClient#write` is a wrapper around `TcpClient#write`, and
when it `retry` after caching a `OpenSSL::SSL::SSLErrorWaitWritable` it
has no idea of the amount of data that got send and restart a full
transfer of the data with `TcpClient#write`.  When this happen, the new
transfer can fail in a similar fashion any number of time and will
eventually come to completion after sending multiple partial copies of
the message followed by a complete copy, which is just garbage for
Riemann on the other side.  Riemann will discard the message and return
an error that will be passed to the calling code.

In order to fix this, make `TcpClient#write` aware of `IO::WaitWritable`
(a base class of `OpenSSL::SSL::SSLErrorWaitWritable`) and remove the
`SSLClient#write` method so that the parent class method is used
directly instead.

While here, do the same for `TcpClient#read` / `SSLClient#read` for
consistency.

While here, also handle `IO::WaitReadable` exception in
`TcpClient#write` to cope with TLS renegociation as recommended in the
`IO#select` documentation.
@smortex smortex force-pushed the fix-sending-large-batch-of-events-over-tls branch from 32afb61 to b13c73c Compare July 29, 2023 23:14
@smortex smortex marked this pull request as ready for review July 30, 2023 04:58
@smortex smortex requested a review from jamtur01 July 30, 2023 04:58
Copy link
Member

@jamtur01 jamtur01 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@jamtur01 jamtur01 merged commit 05b0856 into main Jul 30, 2023
6 checks passed
@smortex smortex deleted the fix-sending-large-batch-of-events-over-tls branch July 30, 2023 16:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants