Skip to content

Commit de3b140

Browse files
committed
add timeout and use java bz
1 parent 84d5d04 commit de3b140

File tree

1 file changed

+43
-35
lines changed

1 file changed

+43
-35
lines changed

src/main/java/opendota/Main.java

Lines changed: 43 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@
1919
import java.util.Map;
2020
import java.util.Timer;
2121
import java.util.TimerTask;
22+
import java.util.concurrent.ExecutorService;
23+
import java.util.concurrent.Executors;
24+
import java.util.concurrent.Future;
25+
import java.util.concurrent.TimeUnit;
26+
27+
import org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream;
2228

2329
import com.sun.net.httpserver.HttpExchange;
2430
import com.sun.net.httpserver.HttpHandler;
@@ -77,49 +83,51 @@ public void handle(HttpExchange t) throws IOException {
7783
URI replayUrl = URI.create(query.get("replay_url"));
7884
// Get the replay as a byte[]
7985
long tStart = System.currentTimeMillis();
80-
HttpClient client = HttpClient.newHttpClient();
81-
HttpRequest request = HttpRequest.newBuilder()
82-
.timeout(Duration.ofSeconds(145))
83-
.uri(replayUrl)
84-
.build();
85-
HttpResponse<byte[]> response = client.send(request,
86-
HttpResponse.BodyHandlers.ofByteArray());
86+
ExecutorService executor = Executors.newSingleThreadExecutor();
87+
Future<byte[]> future = executor.submit(() -> {
88+
HttpClient client = HttpClient.newHttpClient();
89+
HttpRequest request = HttpRequest.newBuilder()
90+
.uri(replayUrl)
91+
.build();
92+
HttpResponse<byte[]> response = client.send(request,
93+
HttpResponse.BodyHandlers.ofByteArray());
94+
return response.body();
95+
});
96+
byte[] bzIn = future.get(800, TimeUnit.SECONDS);
8797
long tEnd = System.currentTimeMillis();
8898
System.err.format("download: %dms\n", tEnd - tStart);
8999

90-
byte[] bzIn = response.body();
91100
byte[] bzOut = bzIn;
92-
93101
if (replayUrl.toString().endsWith(".bz2")) {
94102
tStart = System.currentTimeMillis();
95103
// Write byte[] to bunzip, get back decompressed byte[]
96104
// The C decompressor is a bit faster than Java, 4.3 vs 4.8s
97-
// BZip2CompressorInputStream bz = new BZip2CompressorInputStream(new ByteArrayInputStream(bzIn));
98-
// bzOut = bz.readAllBytes();
99-
// bz.close();
100-
101-
Process bz = new ProcessBuilder(new String[] { "bunzip2" }).start();
102-
// Start separate thread so we can consume output while sending input
103-
new Thread(() -> {
104-
try {
105-
bz.getOutputStream().write(bzIn);
106-
bz.getOutputStream().close();
107-
} catch (IOException ex) {
108-
ex.printStackTrace();
109-
}
110-
}).start();
111-
112-
bzOut = bz.getInputStream().readAllBytes();
113-
bz.getInputStream().close();
114-
String bzError = new String(bz.getErrorStream().readAllBytes());
115-
bz.getErrorStream().close();
116-
System.err.println(bzError);
117-
if (bzError.toString().contains("bunzip2: Data integrity error when decompressing") || bzError.contains("bunzip2: Compressed file ends unexpectedly")) {
118-
// Corrupted replay, don't retry
119-
t.sendResponseHeaders(204, 0);
120-
t.getResponseBody().close();
121-
return;
122-
}
105+
BZip2CompressorInputStream bz = new BZip2CompressorInputStream(new ByteArrayInputStream(bzIn));
106+
bzOut = bz.readAllBytes();
107+
bz.close();
108+
109+
// Process bz = new ProcessBuilder(new String[] { "bunzip2" }).start();
110+
// // Start separate thread so we can consume output while sending input
111+
// new Thread(() -> {
112+
// try {
113+
// bz.getOutputStream().write(bzIn);
114+
// bz.getOutputStream().close();
115+
// } catch (IOException ex) {
116+
// ex.printStackTrace();
117+
// }
118+
// }).start();
119+
120+
// bzOut = bz.getInputStream().readAllBytes();
121+
// bz.getInputStream().close();
122+
// String bzError = new String(bz.getErrorStream().readAllBytes());
123+
// bz.getErrorStream().close();
124+
// System.err.println(bzError);
125+
// if (bzError.toString().contains("bunzip2: Data integrity error when decompressing") || bzError.contains("bunzip2: Compressed file ends unexpectedly")) {
126+
// // Corrupted replay, don't retry
127+
// t.sendResponseHeaders(204, 0);
128+
// t.getResponseBody().close();
129+
// return;
130+
// }
123131
tEnd = System.currentTimeMillis();
124132
System.err.format("bunzip2: %dms\n", tEnd - tStart);
125133
}

0 commit comments

Comments
 (0)