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

(closed) Fix cases where Connection Establishment Timed Out is incorrectly logged (1.6.x) (SimplePool) #2175

Closed
wants to merge 5 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
correctly classify connection-creation timeouts by checking all futur…
…e statuses (only WAITING means a timeout). Also update to use new custom exceptions
Michael Christoff committed Mar 14, 2024
commit 3952bf0d68e888832b5c3bc55f8c8811466c2b35
Original file line number Diff line number Diff line change
@@ -22,6 +22,7 @@
import com.networknt.client.ClientConfig;
import com.networknt.client.simplepool.SimpleConnection;
import com.networknt.client.simplepool.SimpleConnectionMaker;
import com.networknt.client.simplepool.exceptions.*;
import io.undertow.Undertow;
import io.undertow.UndertowOptions;
import io.undertow.client.ClientCallback;
@@ -88,7 +89,7 @@ public void completed(ClientConnection connection) {

@Override
public void failed(IOException e) {
if(logger.isDebugEnabled()) logger.debug("Failed to establish new connection for uri: {}", uri);
logger.error("Failed to establish new connection for uri {}: {}", uri, exceptionDetails(e));
result.setException(e);
}
};
@@ -105,25 +106,34 @@ public void failed(IOException e) {
/***
* Never returns null
*
* @param timeoutSeconds
* @param future
* @return
* @param timeoutSeconds connection timeout in seconds
* @param future contains future response containing new connection
* @return the new Undertow connection wrapped in a SimpleConnection
* @throws RuntimeException if connection fails
*/
private static SimpleConnection safeConnect(long timeoutSeconds, IoFuture<SimpleConnection> future)
private static SimpleConnection safeConnect(long timeoutSeconds, IoFuture<SimpleConnection> future) throws RuntimeException
{
SimpleConnection connection = null;

if(future.await(timeoutSeconds, TimeUnit.SECONDS) != org.xnio.IoFuture.Status.DONE)
throw new RuntimeException("Connection establishment timed out");
switch(future.await(timeoutSeconds, TimeUnit.SECONDS)) {
case DONE:
break;
case WAITING:
throw new SimplePoolConnectionFailureException("Connection establishment timed out after " + timeoutSeconds + " second(s)");
case FAILED:
Exception e = future.getException();
throw new SimplePoolConnectionFailureException("Connection establishment failed: " + exceptionDetails(e), e);
default:
throw new SimplePoolConnectionFailureException("Connection establishment failed");
}

SimpleConnection connection = null;
try {
connection = future.get();
} catch (IOException e) {
throw new RuntimeException("Connection establishment generated I/O exception", e);
throw new SimplePoolConnectionFailureException("Connection establishment generated I/O exception: " + exceptionDetails(e), e);
}

if(connection == null)
throw new RuntimeException("Connection establishment failed (null) - Full connection terminated");
throw new SimplePoolConnectionFailureException("Connection establishment failed (null) - Full connection terminated");

return connection;
}
@@ -196,6 +206,19 @@ private static XnioSsl getSSL(boolean isHttps)
return SSL.get();
}

/***
* Handles empty Exception messages for printing in logs (to avoid having "null" in logs for empty Exception messages)
*
* @param e Exception to look for a detail message in
* @return the Exception message, or "" if the Exception does not contain a message
*/
public static String exceptionDetails(Exception e) {
if(e == null || e.getMessage() == null)
return "";
else
return e.getMessage();
}

public static String port(ClientConnection connection) {
if(connection == null) return "NULL";
String url = connection.getLocalAddress().toString();