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

Introduce SocketConnectTimeout configuration option #638

Merged
merged 8 commits into from
Jun 29, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,13 @@ <H3>QuickFIX Settings</H3>
<TD> valid IP address in the format of x.x.x.x or a domain name </TD>
<TD>&nbsp; </TD>
</TR>
<TR ALIGN="left" VALIGN="middle">
<TD> <I>SocketConnectTimeout</I> </TD>

<TD> Connection timeout in seconds. Only used with a SocketInitiator </TD>
<TD> positive integer </TD>
<TD> 60 </TD>
</TR>
<TR ALIGN="left" VALIGN="middle">

<TD> <I>SocketConnectProtocol</I> </TD>
Expand Down
8 changes: 8 additions & 0 deletions quickfixj-core/src/main/java/quickfix/Initiator.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@ public interface Initiator extends Connector {
*/
String SETTING_SOCKET_CONNECT_PORT = "SocketConnectPort";

/**
* Initiator setting for connection timeout. Only valid when session connection
* type is "initiator".
*
* @see quickfix.SessionFactory#SETTING_CONNECTION_TYPE
*/
String SETTING_SOCKET_CONNECT_TIMEOUT = "SocketConnectTimeout";

/**
* Initiator setting for local/bind host. Only valid when session connection
* type is "initiator".
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,9 @@ private void createInitiator(final Session session, final boolean continueInitOn
throw new ConfigError("Must specify at least one socket address");
}

// 1 minute by default, matches MINA
int connectTimeout = getSettings().getIntOrDefault(sessionID, Initiator.SETTING_SOCKET_CONNECT_TIMEOUT, 60);

SocketAddress localAddress = getLocalAddress(settings, sessionID);

final NetworkingOptions networkingOptions = new NetworkingOptions(getSettings()
Expand Down Expand Up @@ -174,7 +177,7 @@ && getSettings().isSetting(sessionID, Initiator.SETTING_PROXY_DOMAIN)) {
ScheduledExecutorService scheduledExecutorService = (scheduledReconnectExecutor != null ? scheduledReconnectExecutor : getScheduledExecutorService());
try {
final IoSessionInitiator ioSessionInitiator = new IoSessionInitiator(session,
socketAddresses, localAddress, reconnectingIntervals,
socketAddresses, localAddress, connectTimeout, reconnectingIntervals,
chrjohn marked this conversation as resolved.
Show resolved Hide resolved
scheduledExecutorService, settings, networkingOptions,
getEventHandlingStrategy(), getIoFilterChainBuilder(), sslEnabled, sslConfig,
proxyType, proxyVersion, proxyHost, proxyPort, proxyUser, proxyPassword, proxyDomain, proxyWorkstation);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,20 +64,23 @@ public class IoSessionInitiator {
private Future<?> reconnectFuture;

public IoSessionInitiator(Session fixSession, SocketAddress[] socketAddresses,
SocketAddress localAddress, int[] reconnectIntervalInSeconds,
SocketAddress localAddress, int connectTimeout, int[] reconnectIntervalInSeconds,
ScheduledExecutorService executor, SessionSettings sessionSettings, NetworkingOptions networkingOptions,
EventHandlingStrategy eventHandlingStrategy,
IoFilterChainBuilder userIoFilterChainBuilder, boolean sslEnabled, SSLConfig sslConfig,
String proxyType, String proxyVersion, String proxyHost, int proxyPort,
String proxyUser, String proxyPassword, String proxyDomain, String proxyWorkstation) throws ConfigError {
this.executor = executor;

final long connectTimeoutMillis = connectTimeout * 1000L;
final long[] reconnectIntervalInMillis = new long[reconnectIntervalInSeconds.length];
for (int ii = 0; ii != reconnectIntervalInSeconds.length; ++ii) {
reconnectIntervalInMillis[ii] = reconnectIntervalInSeconds[ii] * 1000L;
}

try {
reconnectTask = new ConnectTask(sslEnabled, socketAddresses, localAddress,
userIoFilterChainBuilder, fixSession, reconnectIntervalInMillis,
userIoFilterChainBuilder, fixSession, connectTimeoutMillis, reconnectIntervalInMillis,
sessionSettings, networkingOptions, eventHandlingStrategy, sslConfig,
proxyType, proxyVersion, proxyHost, proxyPort, proxyUser, proxyPassword, proxyDomain, proxyWorkstation, log);
} catch (GeneralSecurityException e) {
Expand All @@ -94,6 +97,7 @@ private static class ConnectTask implements Runnable {
private final IoFilterChainBuilder userIoFilterChainBuilder;
private IoConnector ioConnector;
private final Session fixSession;
private final long connectTimeoutMillis;
private final long[] reconnectIntervalInMillis;
private final SessionSettings sessionSettings;
private final NetworkingOptions networkingOptions;
Expand All @@ -119,7 +123,7 @@ private static class ConnectTask implements Runnable {

public ConnectTask(boolean sslEnabled, SocketAddress[] socketAddresses,
SocketAddress localAddress, IoFilterChainBuilder userIoFilterChainBuilder,
Session fixSession, long[] reconnectIntervalInMillis,
Session fixSession, long connectTimeoutMillis, long[] reconnectIntervalInMillis,
SessionSettings sessionSettings, NetworkingOptions networkingOptions, EventHandlingStrategy eventHandlingStrategy, SSLConfig sslConfig,
String proxyType, String proxyVersion, String proxyHost,
int proxyPort, String proxyUser, String proxyPassword, String proxyDomain,
Expand All @@ -129,6 +133,7 @@ public ConnectTask(boolean sslEnabled, SocketAddress[] socketAddresses,
this.localAddress = localAddress;
this.userIoFilterChainBuilder = userIoFilterChainBuilder;
this.fixSession = fixSession;
this.connectTimeoutMillis = connectTimeoutMillis;
this.reconnectIntervalInMillis = reconnectIntervalInMillis;
this.sessionSettings = sessionSettings;
this.networkingOptions = networkingOptions;
Expand Down Expand Up @@ -160,9 +165,9 @@ private void setupIoConnector() throws ConfigError, GeneralSecurityException {

ioFilterChainBuilder.addLast(FIXProtocolCodecFactory.FILTER_NAME, new ProtocolCodecFilter(new FIXProtocolCodecFactory()));

IoConnector newConnector;
newConnector = ProtocolFactory.createIoConnector(socketAddresses[nextSocketAddressIndex]);
IoConnector newConnector = ProtocolFactory.createIoConnector(socketAddresses[nextSocketAddressIndex]);
networkingOptions.apply(newConnector);
newConnector.setConnectTimeoutMillis(connectTimeoutMillis);
newConnector.setHandler(new InitiatorIoHandler(fixSession, sessionSettings, networkingOptions, eventHandlingStrategy));
newConnector.setFilterChainBuilder(ioFilterChainBuilder);

Expand Down