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

Socketnetwork timeout and polling #411

Merged
merged 2 commits into from
Sep 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ private Map<Integer, Socket> connectClient(final NetworkConfiguration conf)
} catch (ConnectException e) {
// A ConnectionException is expected if the opposing side is not listening for our
// connection attempt yet. We ignore this and try again.
Thread.sleep(1L << ++attempts);
Thread.sleep(Math.min(1 << attempts++, 5_000));
Copy link
Author

Choose a reason for hiding this comment

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

The choice of an upper bound is based on the assumption that we always want to attempt to connect frequently - and 5 seconds was picked based on our own observations.

Alternatively this upper bound could be made configurable, or the entire calculation could be configurable - e.g. some Function<Integer, Long> pollInterval with the default being attempts -> 1 << attemps

Copy link
Member

Choose a reason for hiding this comment

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

It should probably just keep waiting the same amount, scale linearly, or max out after a period. We probably never want to wait 24 days (2^31 ms) between retries. But the existing implementation is a bit weird. 5 seconds is probably a good amount.

// This should probably not busy-wait for each party
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,17 @@ public SocketNetwork(NetworkConfiguration conf, Map<Integer, Socket> socketMap)
* @param conf the configuration to load the network from.
*/
public SocketNetwork(NetworkConfiguration conf) {
this(conf, new Connector(conf, Duration.of(1, ChronoUnit.MINUTES)).getSocketMap());
this(conf, Duration.of(1, ChronoUnit.MINUTES));
}

/**
* Construct a socket network with a specific timeout.
*
* @param conf the configuration to load the network from.
* @param timeout duration to wait until timeout when connecting the network.
*/
public SocketNetwork(NetworkConfiguration conf, Duration timeout) {
this(conf, new Connector(conf, timeout).getSocketMap());
}

/**
Expand Down