From 24f2f525128ed353349efb9564d85c3676be6f4b Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Mon, 22 Feb 2021 09:24:41 +0100 Subject: [PATCH] limit http upload / download to 4 parallel connections --- .../conversations/http/HttpConnectionManager.java | 4 ++++ .../conversations/http/HttpDownloadConnection.java | 12 +++++++----- .../conversations/http/HttpUploadConnection.java | 6 ++++-- .../services/AbstractConnectionManager.java | 8 ++++++-- 4 files changed, 21 insertions(+), 9 deletions(-) diff --git a/src/main/java/eu/siacs/conversations/http/HttpConnectionManager.java b/src/main/java/eu/siacs/conversations/http/HttpConnectionManager.java index d96bb4deb..6a4e05f58 100644 --- a/src/main/java/eu/siacs/conversations/http/HttpConnectionManager.java +++ b/src/main/java/eu/siacs/conversations/http/HttpConnectionManager.java @@ -13,6 +13,8 @@ import java.security.KeyManagementException; import java.security.NoSuchAlgorithmException; import java.util.ArrayList; import java.util.List; +import java.util.concurrent.Executor; +import java.util.concurrent.Executors; import javax.net.ssl.HostnameVerifier; import javax.net.ssl.HttpsURLConnection; @@ -31,6 +33,8 @@ public class HttpConnectionManager extends AbstractConnectionManager { private final List downloadConnections = new ArrayList<>(); private final List uploadConnections = new ArrayList<>(); + public static final Executor EXECUTOR = Executors.newFixedThreadPool(4); + public HttpConnectionManager(XmppConnectionService service) { super(service); } diff --git a/src/main/java/eu/siacs/conversations/http/HttpDownloadConnection.java b/src/main/java/eu/siacs/conversations/http/HttpDownloadConnection.java index fdacad9eb..e55de385a 100644 --- a/src/main/java/eu/siacs/conversations/http/HttpDownloadConnection.java +++ b/src/main/java/eu/siacs/conversations/http/HttpDownloadConnection.java @@ -36,6 +36,8 @@ import eu.siacs.conversations.utils.MimeUtils; import eu.siacs.conversations.utils.WakeLockHelper; import eu.siacs.conversations.xmpp.stanzas.IqPacket; +import static eu.siacs.conversations.http.HttpConnectionManager.EXECUTOR; + public class HttpDownloadConnection implements Transferable { private final Message message; @@ -133,12 +135,12 @@ public class HttpDownloadConnection implements Transferable { } } - private void download(boolean interactive) { - new Thread(new FileDownloader(interactive)).start(); + private void download(final boolean interactive) { + EXECUTOR.execute(new FileDownloader(interactive)); } - private void checkFileSize(boolean interactive) { - new Thread(new FileSizeChecker(interactive)).start(); + private void checkFileSize(final boolean interactive) { + EXECUTOR.execute(new FileSizeChecker(interactive)); } @Override @@ -425,7 +427,7 @@ public class HttpDownloadConnection implements Transferable { private void download() throws Exception { InputStream is = null; HttpURLConnection connection = null; - final PowerManager.WakeLock wakeLock = mHttpConnectionManager.createWakeLock("http_download_" + message.getUuid()); + final PowerManager.WakeLock wakeLock = mHttpConnectionManager.createWakeLock(Thread.currentThread()); try { wakeLock.acquire(); if (mUseTor || message.getConversation().getAccount().isOnion()) { diff --git a/src/main/java/eu/siacs/conversations/http/HttpUploadConnection.java b/src/main/java/eu/siacs/conversations/http/HttpUploadConnection.java index 430c7532b..a6161701f 100644 --- a/src/main/java/eu/siacs/conversations/http/HttpUploadConnection.java +++ b/src/main/java/eu/siacs/conversations/http/HttpUploadConnection.java @@ -27,6 +27,8 @@ import eu.siacs.conversations.utils.Checksum; import eu.siacs.conversations.utils.CryptoHelper; import eu.siacs.conversations.utils.WakeLockHelper; +import static eu.siacs.conversations.http.HttpConnectionManager.EXECUTOR; + public class HttpUploadConnection implements Transferable { static final List WHITE_LISTED_HEADERS = Arrays.asList( @@ -136,7 +138,7 @@ public class HttpUploadConnection implements Transferable { public void success(SlotRequester.Slot slot) { if (!cancelled) { HttpUploadConnection.this.slot = slot; - new Thread(HttpUploadConnection.this::upload).start(); + EXECUTOR.execute(HttpUploadConnection.this::upload); } } @@ -153,7 +155,7 @@ public class HttpUploadConnection implements Transferable { OutputStream os = null; InputStream fileInputStream = null; HttpURLConnection connection = null; - PowerManager.WakeLock wakeLock = mHttpConnectionManager.createWakeLock("http_upload_"+message.getUuid()); + final PowerManager.WakeLock wakeLock = mHttpConnectionManager.createWakeLock(Thread.currentThread()); try { fileInputStream = new FileInputStream(file); final String slotHostname = slot.getPutUrl().getHost(); diff --git a/src/main/java/eu/siacs/conversations/services/AbstractConnectionManager.java b/src/main/java/eu/siacs/conversations/services/AbstractConnectionManager.java index 87dffab29..dcf8848bf 100644 --- a/src/main/java/eu/siacs/conversations/services/AbstractConnectionManager.java +++ b/src/main/java/eu/siacs/conversations/services/AbstractConnectionManager.java @@ -94,8 +94,12 @@ public class AbstractConnectionManager { } } - public PowerManager.WakeLock createWakeLock(String name) { - PowerManager powerManager = (PowerManager) mXmppConnectionService.getSystemService(Context.POWER_SERVICE); + public PowerManager.WakeLock createWakeLock(final Thread thread) { + return createWakeLock("conversations:" + thread.getName()); + } + + public PowerManager.WakeLock createWakeLock(final String name) { + final PowerManager powerManager = (PowerManager) mXmppConnectionService.getSystemService(Context.POWER_SERVICE); return powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, name); }