limit http upload / download to 4 parallel connections

This commit is contained in:
Daniel Gultsch 2021-02-22 09:24:41 +01:00
parent e98ec40b7f
commit 24f2f52512
4 changed files with 21 additions and 9 deletions

View file

@ -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<HttpDownloadConnection> downloadConnections = new ArrayList<>();
private final List<HttpUploadConnection> uploadConnections = new ArrayList<>();
public static final Executor EXECUTOR = Executors.newFixedThreadPool(4);
public HttpConnectionManager(XmppConnectionService service) {
super(service);
}

View file

@ -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()) {

View file

@ -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<String> 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();

View file

@ -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);
}