use of ch.cyberduck.core.threading.ThreadPool in project cyberduck by iterate-ch.
the class BrickUploadFeature method upload.
@Override
public FileEntity upload(final Path file, final Local local, final BandwidthThrottle throttle, final StreamListener listener, final TransferStatus status, final ConnectionCallback callback) throws BackgroundException {
final ThreadPool pool = ThreadPoolFactory.get("multipart", concurrency);
try {
// Full size of file
final long size = status.getLength() + status.getOffset();
final List<Future<TransferStatus>> parts = new ArrayList<>();
final List<TransferStatus> checksums = new ArrayList<>();
long offset = 0;
long remaining = status.getLength();
String ref = null;
for (int partNumber = 1; remaining > 0; partNumber++) {
final FileUploadPartEntity uploadPartEntity = this.continueUpload(file, ref, partNumber);
final long length;
if (uploadPartEntity.isParallelParts()) {
length = Math.min(Math.max(size / (MAXIMUM_UPLOAD_PARTS - 1), partsize), remaining);
} else {
length = remaining;
}
parts.add(this.submit(pool, file, local, throttle, listener, status, uploadPartEntity.getUploadUri(), partNumber, offset, length, callback));
remaining -= length;
offset += length;
ref = uploadPartEntity.getRef();
}
for (Future<TransferStatus> future : parts) {
try {
checksums.add(future.get());
} catch (InterruptedException e) {
log.error("Part upload failed with interrupt failure");
status.setCanceled();
throw new ConnectionCanceledException(e);
} catch (ExecutionException e) {
log.warn(String.format("Part upload failed with execution failure %s", e.getMessage()));
if (e.getCause() instanceof BackgroundException) {
throw (BackgroundException) e.getCause();
}
throw new BackgroundException(e.getCause());
}
}
final FileEntity entity = this.completeUpload(file, ref, status, checksums);
// Mark parent status as complete
status.withResponse(new BrickAttributesFinderFeature(session).toAttributes(entity)).setComplete();
return entity;
} finally {
// Cancel future tasks
pool.shutdown(false);
}
}
Aggregations