KAFKA-2817; Check if socketChannel is connected in `SslTransportLayer.close`

This avoids spurious log warning messages. Also tweak log message
if wrapResult.getStatus != CLOSED.

Author: Ismael Juma <ismael@juma.me.uk>

Reviewers: Rajini Sivaram <rajinisivaram@googlemail.com>, Jun Rao <junrao@gmail.com>

Closes #511 from ijuma/kafka-2817-unconnected-ssl-transport-layer-close
This commit is contained in:
Ismael Juma 2015-11-13 08:29:28 -08:00 committed by Jun Rao
parent 4170847f12
commit 528c78fd30
1 changed files with 15 additions and 12 deletions

View File

@ -144,19 +144,22 @@ public class SslTransportLayer implements TransportLayer {
closing = true;
sslEngine.closeOutbound();
try {
if (!flush(netWriteBuffer)) {
throw new IOException("Remaining data in the network buffer, can't send SSL close message.");
if (isConnected()) {
if (!flush(netWriteBuffer)) {
throw new IOException("Remaining data in the network buffer, can't send SSL close message.");
}
//prep the buffer for the close message
netWriteBuffer.clear();
//perform the close, since we called sslEngine.closeOutbound
SSLEngineResult wrapResult = sslEngine.wrap(emptyBuf, netWriteBuffer);
//we should be in a close state
if (wrapResult.getStatus() != SSLEngineResult.Status.CLOSED) {
throw new IOException("Unexpected status returned by SSLEngine.wrap, expected CLOSED, received " +
wrapResult.getStatus() + ". Will not send close message to peer.");
}
netWriteBuffer.flip();
flush(netWriteBuffer);
}
//prep the buffer for the close message
netWriteBuffer.clear();
//perform the close, since we called sslEngine.closeOutbound
SSLEngineResult handshake = sslEngine.wrap(emptyBuf, netWriteBuffer);
//we should be in a close state
if (handshake.getStatus() != SSLEngineResult.Status.CLOSED) {
throw new IOException("Invalid close state, will not send network data.");
}
netWriteBuffer.flip();
flush(netWriteBuffer);
} catch (IOException ie) {
log.warn("Failed to send SSL Close message ", ie);
} finally {