use thread pool to cap sync concurrency in setDaemonConnection()

This commit is contained in:
woodser 2023-02-09 11:23:53 -05:00
parent 190003b5ba
commit b69a7c1b48
2 changed files with 23 additions and 5 deletions

View File

@ -67,6 +67,10 @@ public class HavenoUtils {
public static BigInteger CENTINEROS_AU_MULTIPLIER = new BigInteger("10000"); public static BigInteger CENTINEROS_AU_MULTIPLIER = new BigInteger("10000");
private static BigInteger XMR_AU_MULTIPLIER = new BigInteger("1000000000000"); private static BigInteger XMR_AU_MULTIPLIER = new BigInteger("1000000000000");
// global thread pool
private static final int POOL_SIZE = 10;
private static final ExecutorService POOL = Executors.newFixedThreadPool(POOL_SIZE);
// TODO: better way to share reference? // TODO: better way to share reference?
public static ArbitrationManager arbitrationManager; public static ArbitrationManager arbitrationManager;
@ -349,14 +353,28 @@ public class HavenoUtils {
} }
} }
// TODO: replace with GenUtils.executeTasks() /**
* Submit tasks to a global thread pool.
*/
public static Future<?> submitTask(Runnable task) {
return POOL.submit(task);
}
public static List<Future<?>> submitTasks(List<Runnable> tasks) {
List<Future<?>> futures = new ArrayList<Future<?>>();
for (Runnable task : tasks) futures.add(submitTask(task));
return futures;
}
// TODO: replace with GenUtils.executeTasks() once monero-java updated
public static void executeTasks(Collection<Runnable> tasks) { public static void executeTasks(Collection<Runnable> tasks) {
executeTasks(tasks, tasks.size()); executeTasks(tasks, tasks.size());
} }
public static void executeTasks(Collection<Runnable> tasks, int poolSize) { public static void executeTasks(Collection<Runnable> tasks, int maxConcurrency) {
if (tasks.isEmpty()) return; if (tasks.isEmpty()) return;
ExecutorService pool = Executors.newFixedThreadPool(poolSize); ExecutorService pool = Executors.newFixedThreadPool(maxConcurrency);
List<Future<?>> futures = new ArrayList<Future<?>>(); List<Future<?>> futures = new ArrayList<Future<?>>();
for (Runnable task : tasks) futures.add(pool.submit(task)); for (Runnable task : tasks) futures.add(pool.submit(task));
pool.shutdown(); pool.shutdown();

View File

@ -1590,13 +1590,13 @@ public abstract class Trade implements Tradable, Model {
wallet.setDaemonConnection(connection); wallet.setDaemonConnection(connection);
// sync and reprocess messages on new thread // sync and reprocess messages on new thread
new Thread(() -> { HavenoUtils.submitTask(() -> {
updateSyncing(); updateSyncing();
// reprocess pending payout messages // reprocess pending payout messages
this.getProtocol().maybeReprocessPaymentReceivedMessage(false); this.getProtocol().maybeReprocessPaymentReceivedMessage(false);
HavenoUtils.arbitrationManager.maybeReprocessDisputeClosedMessage(this, false); HavenoUtils.arbitrationManager.maybeReprocessDisputeClosedMessage(this, false);
}).start(); });
} }
private void updateSyncing() { private void updateSyncing() {