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.security.NoSuchAlgorithmException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import javax.net.ssl.HostnameVerifier; import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection; import javax.net.ssl.HttpsURLConnection;
@ -31,6 +33,8 @@ public class HttpConnectionManager extends AbstractConnectionManager {
private final List<HttpDownloadConnection> downloadConnections = new ArrayList<>(); private final List<HttpDownloadConnection> downloadConnections = new ArrayList<>();
private final List<HttpUploadConnection> uploadConnections = new ArrayList<>(); private final List<HttpUploadConnection> uploadConnections = new ArrayList<>();
public static final Executor EXECUTOR = Executors.newFixedThreadPool(4);
public HttpConnectionManager(XmppConnectionService service) { public HttpConnectionManager(XmppConnectionService service) {
super(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.utils.WakeLockHelper;
import eu.siacs.conversations.xmpp.stanzas.IqPacket; import eu.siacs.conversations.xmpp.stanzas.IqPacket;
import static eu.siacs.conversations.http.HttpConnectionManager.EXECUTOR;
public class HttpDownloadConnection implements Transferable { public class HttpDownloadConnection implements Transferable {
private final Message message; private final Message message;
@ -133,12 +135,12 @@ public class HttpDownloadConnection implements Transferable {
} }
} }
private void download(boolean interactive) { private void download(final boolean interactive) {
new Thread(new FileDownloader(interactive)).start(); EXECUTOR.execute(new FileDownloader(interactive));
} }
private void checkFileSize(boolean interactive) { private void checkFileSize(final boolean interactive) {
new Thread(new FileSizeChecker(interactive)).start(); EXECUTOR.execute(new FileSizeChecker(interactive));
} }
@Override @Override
@ -425,7 +427,7 @@ public class HttpDownloadConnection implements Transferable {
private void download() throws Exception { private void download() throws Exception {
InputStream is = null; InputStream is = null;
HttpURLConnection connection = null; HttpURLConnection connection = null;
final PowerManager.WakeLock wakeLock = mHttpConnectionManager.createWakeLock("http_download_" + message.getUuid()); final PowerManager.WakeLock wakeLock = mHttpConnectionManager.createWakeLock(Thread.currentThread());
try { try {
wakeLock.acquire(); wakeLock.acquire();
if (mUseTor || message.getConversation().getAccount().isOnion()) { 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.CryptoHelper;
import eu.siacs.conversations.utils.WakeLockHelper; import eu.siacs.conversations.utils.WakeLockHelper;
import static eu.siacs.conversations.http.HttpConnectionManager.EXECUTOR;
public class HttpUploadConnection implements Transferable { public class HttpUploadConnection implements Transferable {
static final List<String> WHITE_LISTED_HEADERS = Arrays.asList( static final List<String> WHITE_LISTED_HEADERS = Arrays.asList(
@ -136,7 +138,7 @@ public class HttpUploadConnection implements Transferable {
public void success(SlotRequester.Slot slot) { public void success(SlotRequester.Slot slot) {
if (!cancelled) { if (!cancelled) {
HttpUploadConnection.this.slot = slot; 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; OutputStream os = null;
InputStream fileInputStream = null; InputStream fileInputStream = null;
HttpURLConnection connection = null; HttpURLConnection connection = null;
PowerManager.WakeLock wakeLock = mHttpConnectionManager.createWakeLock("http_upload_"+message.getUuid()); final PowerManager.WakeLock wakeLock = mHttpConnectionManager.createWakeLock(Thread.currentThread());
try { try {
fileInputStream = new FileInputStream(file); fileInputStream = new FileInputStream(file);
final String slotHostname = slot.getPutUrl().getHost(); final String slotHostname = slot.getPutUrl().getHost();

View File

@ -94,8 +94,12 @@ public class AbstractConnectionManager {
} }
} }
public PowerManager.WakeLock createWakeLock(String name) { public PowerManager.WakeLock createWakeLock(final Thread thread) {
PowerManager powerManager = (PowerManager) mXmppConnectionService.getSystemService(Context.POWER_SERVICE); 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); return powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, name);
} }