Skip to content
This repository has been archived by the owner on May 30, 2022. It is now read-only.

Block HTTP calls when thread pool is entirely busy #71

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 39 additions & 30 deletions android/src/main/java/com/ledger/reactnative/HttpClientImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,14 @@
public class HttpClientImpl extends co.ledger.core.HttpClient {
private ReactApplicationContext reactContext;

private static ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(2, 10,25, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(25), Executors.defaultThreadFactory(), new RejectedExecutionHandler() {
@Override
public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
System.out.println(r.toString() + " is rejected");
}
});
private static ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
2,
10,
25,
TimeUnit.SECONDS,
new ArrayBlockingQueue<Runnable>(25),
Executors.defaultThreadFactory(),
new ThreadPoolExecutor.CallerRunsPolicy());

public HttpClientImpl(ReactApplicationContext reactContext) {
this.reactContext = reactContext;
Expand All @@ -47,32 +49,39 @@ public void run() {
try {
URL url = new URL(request.getUrl());
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
HashMap<String, String> headers = request.getHeaders();
for (String hr : headers.keySet()) {
connection.setRequestProperty(hr, headers.get(hr));
}
try {
HashMap<String, String> headers = request.getHeaders();
for (String hr : headers.keySet()) {
connection.setRequestProperty(hr, headers.get(hr));
}

byte[] body = request.getBody();
if (body.length > 0) {
connection.setRequestMethod( "POST" );
if (!headers.containsKey("Content-Type")) {
connection.setRequestProperty( "Content-Type", "application/json");
}
if (!headers.containsKey("Content-Encoding") && !headers.containsValue("application/x-binary")) {
connection.setRequestProperty( "Content-Encoding", "UTF-8");
}
connection.setRequestProperty( "Content-Length", Integer.toString(body.length));
OutputStream os = connection.getOutputStream();
os.write(body);
os.flush();
os.close();
}
byte[] body = request.getBody();
if (body.length > 0) {
connection.setRequestMethod( "POST" );
if (!headers.containsKey("Content-Type")) {
connection.setRequestProperty( "Content-Type", "application/json");
}
if (!headers.containsKey("Content-Encoding") && !headers.containsValue("application/x-binary")) {
connection.setRequestProperty( "Content-Encoding", "UTF-8");
}
connection.setRequestProperty( "Content-Length", Integer.toString(body.length));
OutputStream os = connection.getOutputStream();
os.write(body);
os.flush();
os.close();
}

int httpCode = connection.getResponseCode();
String response = getString(httpCode<400 ? connection.getInputStream() : connection.getErrorStream(), "UTF-8");
com.ledger.reactnative.HttpUrlConnectionImpl urlConnection = new com.ledger.reactnative.HttpUrlConnectionImpl(reactContext, response, httpCode, headers, null);
request.complete(urlConnection, null);
connection.disconnect();
int httpCode = connection.getResponseCode();
String response = getString(httpCode<400 ? connection.getInputStream() : connection.getErrorStream(), "UTF-8");
com.ledger.reactnative.HttpUrlConnectionImpl urlConnection = new com.ledger.reactnative.HttpUrlConnectionImpl(reactContext, response, httpCode, headers, null);
request.complete(urlConnection, null);
} catch (Exception ex) {
Error error = new Error(co.ledger.core.ErrorCode.HTTP_ERROR, ex.getMessage());
com.ledger.reactnative.HttpUrlConnectionImpl urlConnection = new com.ledger.reactnative.HttpUrlConnectionImpl(reactContext, ex.toString(), ex.hashCode(), null, error);
request.complete(urlConnection, error);
} finally {
connection.disconnect();
}
} catch (Exception ex) {
Error error = new Error(co.ledger.core.ErrorCode.HTTP_ERROR, ex.getMessage());
com.ledger.reactnative.HttpUrlConnectionImpl urlConnection = new com.ledger.reactnative.HttpUrlConnectionImpl(reactContext, ex.toString(), ex.hashCode(), null, error);
Expand Down