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

Set peer connect to a dedicated thread pool for non-blocking I/O (Thanks to RAZ) #223

Merged
merged 1 commit into from
Nov 26, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<ciyam-at.version>1.4.2</ciyam-at.version>
<commons-net.version>3.8.0</commons-net.version>
<commons-text.version>1.12.0</commons-text.version>
<commons-io.version>2.17.0</commons-io.version>
<commons-io.version>2.18.0</commons-io.version>
<commons-compress.version>1.27.1</commons-compress.version>
<commons-lang3.version>3.17.0</commons-lang3.version>
<dagger.version>1.2.2</dagger.version>
Expand All @@ -28,7 +28,7 @@
<homoglyph.version>1.2.1</homoglyph.version>
<hsqldb.version>2.7.4</hsqldb.version>
<icu4j.version>76.1</icu4j.version>
<java-diff-utils.version>4.12</java-diff-utils.version>
<java-diff-utils.version>4.15</java-diff-utils.version>
<javax.servlet-api.version>4.0.1</javax.servlet-api.version>
<jaxb-runtime.version>2.3.9</jaxb-runtime.version>
<jersey.version>2.42</jersey.version>
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/org/qortal/block/Block.java
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@ public static class ExpandedAccount {

private final Account recipientAccount;
private final AccountData recipientAccountData;

final BlockChain blockChain = BlockChain.getInstance();

ExpandedAccount(Repository repository, RewardShareData rewardShareData) throws DataException {
this.rewardShareData = rewardShareData;
Expand Down Expand Up @@ -191,13 +193,12 @@ public AccountLevelShareBin getShareBin(int blockHeight) {
if (accountLevel <= 0)
return null; // level 0 isn't included in any share bins

if (blockHeight >= BlockChain.getInstance().getFixBatchRewardHeight()) {
if (blockHeight >= blockChain.getFixBatchRewardHeight()) {
if (!this.isMinterMember)
return null; // not member of minter group isn't included in any share bins
}

// Select the correct set of share bins based on block height
final BlockChain blockChain = BlockChain.getInstance();
final AccountLevelShareBin[] shareBinsByLevel = (blockHeight >= blockChain.getSharesByLevelV2Height()) ?
blockChain.getShareBinsByAccountLevelV2() : blockChain.getShareBinsByAccountLevelV1();

Expand Down
25 changes: 24 additions & 1 deletion src/main/java/org/qortal/network/task/PeerConnectTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@
import org.apache.logging.log4j.Logger;
import org.qortal.network.Network;
import org.qortal.network.Peer;
import org.qortal.utils.DaemonThreadFactory;
import org.qortal.utils.ExecuteProduceConsume.Task;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class PeerConnectTask implements Task {
private static final Logger LOGGER = LogManager.getLogger(PeerConnectTask.class);
private static final ExecutorService connectionExecutor = Executors.newCachedThreadPool(new DaemonThreadFactory(8));

private final Peer peer;
private final String name;
Expand All @@ -24,6 +29,24 @@ public String getName() {

@Override
public void perform() throws InterruptedException {
Network.getInstance().connectPeer(peer);
// Submit connection task to a dedicated thread pool for non-blocking I/O
connectionExecutor.submit(() -> {
try {
connectPeerAsync(peer);
} catch (InterruptedException e) {
LOGGER.error("Connection attempt interrupted for peer {}", peer, e);
Thread.currentThread().interrupt(); // Reset interrupt flag
}
});
}

private void connectPeerAsync(Peer peer) throws InterruptedException {
// Perform peer connection in a separate thread to avoid blocking main task execution
try {
Network.getInstance().connectPeer(peer);
LOGGER.trace("Successfully connected to peer {}", peer);
} catch (Exception e) {
LOGGER.error("Error connecting to peer {}", peer, e);
}
}
}
Loading