remove lock synchronization in connection service to avoid blocking

This commit is contained in:
woodser 2024-01-19 08:32:23 -05:00
parent 93c87462c6
commit 036f40e861

View file

@ -125,15 +125,12 @@ public final class XmrConnectionService {
public void onShutDownStarted() { public void onShutDownStarted() {
log.info("{}.onShutDownStarted()", getClass().getSimpleName()); log.info("{}.onShutDownStarted()", getClass().getSimpleName());
isShutDownStarted = true; isShutDownStarted = true;
synchronized (lock) {
// ensures request not in progress
}
} }
public void shutDown() { public void shutDown() {
log.info("Shutting down started for {}", getClass().getSimpleName()); log.info("Shutting down {}", getClass().getSimpleName());
isInitialized = false;
synchronized (lock) { synchronized (lock) {
isInitialized = false;
if (daemonPollLooper != null) daemonPollLooper.stop(); if (daemonPollLooper != null) daemonPollLooper.stop();
connectionManager.stopPolling(); connectionManager.stopPolling();
daemon = null; daemon = null;
@ -162,94 +159,70 @@ public final class XmrConnectionService {
} }
public void addConnection(MoneroRpcConnection connection) { public void addConnection(MoneroRpcConnection connection) {
synchronized (lock) { accountService.checkAccountOpen();
accountService.checkAccountOpen(); if (coreContext.isApiUser()) connectionList.addConnection(connection);
if (coreContext.isApiUser()) connectionList.addConnection(connection); connectionManager.addConnection(connection);
connectionManager.addConnection(connection);
}
} }
public void removeConnection(String uri) { public void removeConnection(String uri) {
synchronized (lock) { accountService.checkAccountOpen();
accountService.checkAccountOpen(); connectionList.removeConnection(uri);
connectionList.removeConnection(uri); connectionManager.removeConnection(uri);
connectionManager.removeConnection(uri);
}
} }
public MoneroRpcConnection getConnection() { public MoneroRpcConnection getConnection() {
synchronized (lock) { accountService.checkAccountOpen();
accountService.checkAccountOpen(); return connectionManager.getConnection();
return connectionManager.getConnection();
}
} }
public List<MoneroRpcConnection> getConnections() { public List<MoneroRpcConnection> getConnections() {
synchronized (lock) { accountService.checkAccountOpen();
accountService.checkAccountOpen(); return connectionManager.getConnections();
return connectionManager.getConnections();
}
} }
public void setConnection(String connectionUri) { public void setConnection(String connectionUri) {
synchronized (lock) { accountService.checkAccountOpen();
accountService.checkAccountOpen(); connectionManager.setConnection(connectionUri); // listener will update connection list
connectionManager.setConnection(connectionUri); // listener will update connection list
}
} }
public void setConnection(MoneroRpcConnection connection) { public void setConnection(MoneroRpcConnection connection) {
synchronized (lock) { accountService.checkAccountOpen();
accountService.checkAccountOpen(); connectionManager.setConnection(connection); // listener will update connection list
connectionManager.setConnection(connection); // listener will update connection list
}
} }
public MoneroRpcConnection checkConnection() { public MoneroRpcConnection checkConnection() {
synchronized (lock) { accountService.checkAccountOpen();
accountService.checkAccountOpen(); connectionManager.checkConnection();
connectionManager.checkConnection(); return getConnection();
return getConnection();
}
} }
public List<MoneroRpcConnection> checkConnections() { public List<MoneroRpcConnection> checkConnections() {
synchronized (lock) { accountService.checkAccountOpen();
accountService.checkAccountOpen(); connectionManager.checkConnections();
connectionManager.checkConnections(); return getConnections();
return getConnections();
}
} }
public void startCheckingConnection(Long refreshPeriod) { public void startCheckingConnection(Long refreshPeriod) {
synchronized (lock) { accountService.checkAccountOpen();
accountService.checkAccountOpen(); connectionList.setRefreshPeriod(refreshPeriod);
connectionList.setRefreshPeriod(refreshPeriod); updatePolling();
updatePolling();
}
} }
public void stopCheckingConnection() { public void stopCheckingConnection() {
synchronized (lock) { accountService.checkAccountOpen();
accountService.checkAccountOpen(); connectionManager.stopPolling();
connectionManager.stopPolling(); connectionList.setRefreshPeriod(-1L);
connectionList.setRefreshPeriod(-1L);
}
} }
public MoneroRpcConnection getBestAvailableConnection() { public MoneroRpcConnection getBestAvailableConnection() {
synchronized (lock) { accountService.checkAccountOpen();
accountService.checkAccountOpen(); return connectionManager.getBestAvailableConnection();
return connectionManager.getBestAvailableConnection();
}
} }
public void setAutoSwitch(boolean autoSwitch) { public void setAutoSwitch(boolean autoSwitch) {
synchronized (lock) { accountService.checkAccountOpen();
accountService.checkAccountOpen(); connectionManager.setAutoSwitch(autoSwitch);
connectionManager.setAutoSwitch(autoSwitch); connectionList.setAutoSwitch(autoSwitch);
connectionList.setAutoSwitch(autoSwitch);
}
} }
public boolean isConnectionLocal() { public boolean isConnectionLocal() {
@ -665,6 +638,7 @@ public final class XmrConnectionService {
} }
new Thread(() -> { new Thread(() -> {
if (isShutDownStarted) return;
if (!isFixedConnection() && connectionManager.getAutoSwitch()) { if (!isFixedConnection() && connectionManager.getAutoSwitch()) {
MoneroRpcConnection bestConnection = getBestAvailableConnection(); MoneroRpcConnection bestConnection = getBestAvailableConnection();
if (bestConnection != null) connectionManager.setConnection(bestConnection); if (bestConnection != null) connectionManager.setConnection(bestConnection);