我有一个用 Armeria 制作的代理服务器,它应该从源开始下载并同时将其向外流式传输到目的地。该文件应该是流式传输的,因为这是一个代理服务器,我不想让它存储和转发。这个应用程序应该处理大量的流量。
我应该在哪里寻找纯粹在 Armeria 做的任何提示?
我尝试使用 AsyncHttpClient 但我认为它正在锁定主线程或其他东西,因为请求开始累积。
下面的所有代码都在里面CompletableFuture.supplyAsync()
。
有没有办法可以解决这个问题,或者更好的是,纯粹在 Armeria 中解决这个问题?
public HttpResponse doLogic(URL getUrl, String requestId, String key, Lock lock, String token) {
// ----
PipedOutputStream pout = new PipedOutputStream();
BodyDeferringAsyncHandler bodyDeferringAsyncHandler = new BodyDeferringAsyncHandler(pout);
Future<Response> respFuture = asyncHttpClient.prepareGet(getUrl.toString()).execute(bodyDeferringAsyncHandler);
CompletableFuture<HttpResponse> r = CompletableFuture.supplyAsync(() -> {
try (PipedInputStream pin = new PipedInputStream(pout)) {
Response resp = bodyDeferringAsyncHandler.getResponse();
if (resp.getStatusCode() == 200) {
try (InputStream is = new BodyDeferringAsyncHandler.BodyDeferringInputStream(respFuture, bodyDeferringAsyncHandler, pin)) {
HttpHeaders headers = translateHeaders(resp.getHeaders());
if (headers.get("etag").contains("-")) {
// We remove the etag, since it comes from a multipart upload, it isn't an md5 hash of the file.
headers.remove("etag");
}
PipedOutputStream pout2 = new PipedOutputStream();
BodyDeferringAsyncHandler bodyDeferringAsyncHandler2 = new BodyDeferringAsyncHandler(pout2);
Future<Response> putResp = asyncHttpClient.preparePut(Config.getString("swift.proxy.base.url") + "v1/" + key)
.setHeaders(headers)
.addHeader("x-auth-token", token)
.setBody(is).execute(bodyDeferringAsyncHandler2);
Response putR = null;
try {
putR = bodyDeferringAsyncHandler2.getResponse();
if (putR.getStatusCode() != 201 && putR.getStatusCode() != 202) {
log.error("[requestId: " + requestId + "] swift returned " + putR.getStatusCode());
return unlockingResponse(putR.getStatusCode(), lock, key);
}
log.info("[requestId: " + requestId + "] File: " + key + " rollbacked!");
return unlockingResponse(200, lock, key);
} catch (InterruptedException e) {
e.printStackTrace();
}
return unlockingResponse(422, lock, key);
}
} else {
log.warn("[requestId: " + requestId + "] s3 came back with status code: " + resp.getStatusCode());
return unlockingResponse(200, lock, key);
}
} catch (InterruptedException | IOException e) {
e.printStackTrace();
return unlockingResponse(500, lock, key);
}
});
return HttpResponse.from(r);
}
我希望它将文件从源流传输到目标并能够处理大量流量。