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

Improve efficiency of network reads in TLSEngine #1898

Merged
merged 1 commit into from
Jun 12, 2020

Conversation

mpilquist
Copy link
Member

@mpilquist mpilquist commented Jun 11, 2020

I was working with @tpolecat on testing Postgres TLS integration in skunk (#1897). While it's working now, Rob noticed lots of buffer underrun events during unwrapping as a result of requesting a single character from the TLSSocket. This turned in to a call to socket.read(1). Consequently, the TLSSocket#read1 implementation would read a single byte at a time from the underlying raw socket. This would continue until enough individual bytes had been read and queued that an unwrap operation could succeed.

In this PR, we guard against very small maxBytes by requesting up to session.getPacketBufferSize bytes from underlying socket when maxBytes is less than that value.

The result of the Postgres integration now completes with no underrun:

[info] Writing 8 byte(s).
[info] Reading 1 byte(s).
[info] Writing 132 byte(s).
[info] wrap result: Status = OK HandshakeStatus = NEED_UNWRAP
[info] bytesConsumed = 0 bytesProduced = 204
[info] unwrapHandshake result: Status = OK HandshakeStatus = NEED_TASK
[info] bytesConsumed = 66 bytesProduced = 0
[info] unwrapHandshake result: Status = OK HandshakeStatus = NEED_TASK
[info] bytesConsumed = 695 bytesProduced = 0
[info] unwrapHandshake result: Status = OK HandshakeStatus = NEED_TASK
[info] bytesConsumed = 338 bytesProduced = 0
[info] unwrapHandshake result: Status = OK HandshakeStatus = NEED_TASK
[info] bytesConsumed = 9 bytesProduced = 0
[info] unwrapHandshake result: Status = OK HandshakeStatus = NEED_WRAP
[info] bytesConsumed = 0 bytesProduced = 0
[info] wrapHandshake result: Status = OK HandshakeStatus = NEED_WRAP
[info] bytesConsumed = 0 bytesProduced = 75
[info] wrapHandshake result: Status = OK HandshakeStatus = NEED_WRAP
[info] bytesConsumed = 0 bytesProduced = 6
[info] wrapHandshake result: Status = OK HandshakeStatus = NEED_UNWRAP
[info] bytesConsumed = 0 bytesProduced = 45
[info] unwrapHandshake result: Status = OK HandshakeStatus = NEED_UNWRAP
[info] bytesConsumed = 6 bytesProduced = 0
[info] unwrapHandshake result: Status = OK HandshakeStatus = FINISHED
[info] bytesConsumed = 45 bytesProduced = 0
[info] wrap result: Status = OK HandshakeStatus = NOT_HANDSHAKING
[info] bytesConsumed = 132 bytesProduced = 161
[info] Reading 1 byte(s).
[info] unwrap result: Status = OK HandshakeStatus = NOT_HANDSHAKING
[info] bytesConsumed = 346 bytesProduced = 317
[info] Reading 4 byte(s).
[info] Reading 8 byte(s).
[info] <- 'R' (8 bytes)
[info] Terminating.

Copy link
Member

@tpolecat tpolecat left a comment

Choose a reason for hiding this comment

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

Very nice! 👍

@mpilquist mpilquist merged commit 37baaf4 into master Jun 12, 2020
@mpilquist mpilquist deleted the topic/tls-socket-read-size branch June 15, 2020 23:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants